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 Dialer interface 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 EventsQueryClient returns 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.

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 EventsQueryClient when done to free up resources and avoid potential leaks.

  • Error Handling: Always check both the synchronous error returned by EventsBytes as well as asynchronous errors sent over the observable.

FAQ

Why use `events` over directly using Gorilla WebSockets?

events abstracts many of the underlying details and provides a streamlined interface for subscribing to chain events. It also integrates the observable pattern and provides mockable interfaces for better testing.

How can I use a different connection mechanism other than WebSockets?

You can implement the Dialer and Connection interfaces and use the WithDialer configuration to provide your custom dialer.

Why use the `EventsReplayClient` over directly maintaining an `EventsQueryClient`?

The EventsReplayClient will automatically attempt to reconnect to the underlying EventsQueryClient in the event that it closes and publish the most recent observable.ReplayObservable that can be used to retrieve events. This means that the consumer does not need to maintain their own connection to the EventsQueryClient and can always call the EventsSequence and LastNEvents methods to retrieve the latest observable and slice of decoded events from an active EventsQueryClient.

Last updated

Was this helpful?