Client Events
Overview
The events package provides a client interface to subscribe to chain event messages. It abstracts the underlying connection mechanisms and offers a clear and easy-to-use way to get events from the chain. Highlights:
Offers subscription to chain event messages matching a given query.
Uses the Gorilla WebSockets package for underlying connection operations.
Provides a modular structure with interfaces allowing for mock implementations and testing.
Offers considerations for potential improvements and replacements, such as integration with the cometbft RPC client.
Offers a generic client to decode on chain event bytes into the desired event type
Architecture Diagrams
The following section contains numerous diagrams that detail the architecture of the different aspects of the events package.
Components
The following legend describes how to read the following component diagrams.
A uses B via B#MethodName()
A returns C from A#MethodName()
B uses D via network IO
(Component diagrams and legends retained as-is in original docs.)
Events Query Client
(EventsQueryClient component diagram retained as-is in original docs.)
Events Replay Client
(EventsReplayClient component diagram retained as-is in original docs.)
Subscriptions
(TODO_DOCUMENT(@bryanchriswhite): Add Legend)
(Event subscription diagrams and flow retained as-is in original docs.)
Installation
Features
Websocket Connection: Uses the Gorilla WebSockets for implementing the connection interface.
Events Subscription: Subscribe to chain event messages using a simple query mechanism.
Dialer Interface: Offers a
Dialerinterface for constructing connections, which can be easily mocked for tests.Observable Pattern: Integrates the observable pattern, making it easier to react to chain events.
Generic Replay Client: Offers a generic typed replay client to listen for specifc events on chain, and handles reconnection and subscription on error, if the
EventsQueryClientreturns an error or is unexpectedly closed.
Usage (EventsQueryClient)
Basic Example
Advanced Usage - Query Client
Configuration
WithDialer: Configure the client to use a custom dialer for connections.
Usage (EventsReplayClient)
Basic Usage
- Replay Client
The EventsReplayClient can be lightly wrapped to define a custom client for a respective type. Examples of these include the client.BlockClient and client.DelegationClient interfaces which under-the-hood are wrappers for the EventsReplayClient.
TODO(@bryanchriswhite): Update links for BlockClient and DelegationClient when they are added to the documentation.
See: BlockClient and DelegationClient for more detailed examples on how to wrap and use the EventsReplayClient in a more advanced setting.
Best Practices
Connection Handling: Ensure to close the
EventsQueryClientwhen done to free up resources and avoid potential leaks.Error Handling: Always check both the synchronous error returned by
EventsBytesas well as asynchronous errors sent over the observable.
FAQ
Last updated
Was this helpful?
