Skip to main content
Version: Next

Class: Subscription<T>

Represents an active WebSocket subscription.

This class should not be instantiated directly. It is returned by the subscribe methods on the WebSocketChannel.

Example

const channel = new WebSocketChannel({ nodeUrl: 'YOUR_NODE_URL' });
await channel.waitForConnection();

// The 'sub' object is an instance of the Subscription class.
const sub = await channel.subscribeNewHeads();

sub.on((data) => {
console.log('Received new head:', data);
});

// ... later
await sub.unsubscribe();

Type parameters​

NameTypeDescription
TanyThe type of data expected from the subscription event.

Constructors​

constructor​

• new Subscription<T>(channel, method, params, id, maxBufferSize): Subscription<T>

Type parameters​

NameType
Tany

Parameters​

NameTypeDescription
channelWebSocketChannelThe WebSocketChannel instance.
methodstringThe RPC method used for the subscription.
paramsobjectThe parameters for the subscription.
idstringThe subscription ID.
maxBufferSizenumberThe maximum number of events to buffer.

Returns​

Subscription<T>

Defined in​

src/channel/ws/subscription.ts:79

Properties​

channel​

• channel: WebSocketChannel

The containing WebSocketChannel instance.

Defined in​

src/channel/ws/subscription.ts:41


method​

• method: string

The JSON-RPC method used to create this subscription.

Defined in​

src/channel/ws/subscription.ts:47


params​

• params: any

The parameters used to create this subscription.

Defined in​

src/channel/ws/subscription.ts:53


id​

• id: string

The unique identifier for this subscription.

Defined in​

src/channel/ws/subscription.ts:59


events​

• Private events: EventEmitter<SubscriptionEvents<T>>

Defined in​

src/channel/ws/subscription.ts:61


buffer​

• Private buffer: T[] = []

Defined in​

src/channel/ws/subscription.ts:63


maxBufferSize​

• Private maxBufferSize: number

Defined in​

src/channel/ws/subscription.ts:65


handler​

• Private handler: null | (data: T) => void = null

Defined in​

src/channel/ws/subscription.ts:67


_isClosed​

• Private _isClosed: boolean = false

Defined in​

src/channel/ws/subscription.ts:69

Accessors​

isClosed​

• get isClosed(): boolean

Indicates if the subscription has been closed.

Returns​

boolean

true if unsubscribed, false otherwise.

Defined in​

src/channel/ws/subscription.ts:97

Methods​

_handleEvent​

â–¸ _handleEvent(data): void

Internal method to handle incoming events from the WebSocket channel. If a handler is attached, it's invoked immediately. Otherwise, the event is buffered.

Parameters​

NameTypeDescription
dataTThe event data.

Returns​

void

Defined in​

src/channel/ws/subscription.ts:107


on​

â–¸ on(handler): void

Attaches a handler function to be called for each event.

When a handler is attached, any buffered events will be passed to it sequentially. Subsequent events will be passed directly as they arrive.

Parameters​

NameTypeDescription
handler(data: T) => voidThe function to call with event data.

Returns​

void

Throws

If a handler is already attached to this subscription.

Defined in​

src/channel/ws/subscription.ts:128


unsubscribe​

â–¸ unsubscribe(): Promise<boolean>

Sends an unsubscribe request to the node and cleans up local resources.

Returns​

Promise<boolean>

A Promise that resolves to true if the unsubscription was successful.

Defined in​

src/channel/ws/subscription.ts:149