Skip to main content
Version: 8.6.0

Class: WebSocketChannel

Manages a WebSocket connection to a Starknet node for receiving real-time updates. This class handles subscriptions, automatic reconnection, and request queueing.

Example

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

const sub = await channel.subscribeNewHeads();
sub.on((data) => {
console.log('New Block:', data);
});

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

Constructors​

constructor​

• new WebSocketChannel(options): WebSocketChannel

Creates an instance of WebSocketChannel.

Parameters​

NameTypeDescription
optionsWebSocketOptionsThe options for configuring the channel.

Returns​

WebSocketChannel

Defined in​

src/channel/ws/ws_0_9.ts:218

Properties​

nodeUrl​

• nodeUrl: string

The URL of the WebSocket RPC Node.

Example

'wss://starknet-sepolia.public.blastapi.io/rpc/v0_8';

Defined in​

src/channel/ws/ws_0_9.ts:162


websocket​

• websocket: WebSocket

The underlying WebSocket instance.

Defined in​

src/channel/ws/ws_0_9.ts:167


WsImplementation​

• Private WsImplementation: WebSocketModule

Defined in​

src/channel/ws/ws_0_9.ts:170


activeSubscriptions​

• Private activeSubscriptions: Map<string, Subscription<any>>

Defined in​

src/channel/ws/ws_0_9.ts:173


maxBufferSize​

• Private Readonly maxBufferSize: number

Defined in​

src/channel/ws/ws_0_9.ts:175


autoReconnect​

• Private Readonly autoReconnect: boolean

Defined in​

src/channel/ws/ws_0_9.ts:177


reconnectOptions​

• Private Readonly reconnectOptions: Required<ReconnectOptions>

Defined in​

src/channel/ws/ws_0_9.ts:179


requestTimeout​

• Private Readonly requestTimeout: number

Defined in​

src/channel/ws/ws_0_9.ts:181


isReconnecting​

• Private isReconnecting: boolean = false

Defined in​

src/channel/ws/ws_0_9.ts:183


reconnectAttempts​

• Private reconnectAttempts: number = 0

Defined in​

src/channel/ws/ws_0_9.ts:185


userInitiatedClose​

• Private userInitiatedClose: boolean = false

Defined in​

src/channel/ws/ws_0_9.ts:187


reconnectTimeoutId​

• Private reconnectTimeoutId: null | Timeout = null

Defined in​

src/channel/ws/ws_0_9.ts:189


requestQueue​

• Private requestQueue: { method: string ; params?: object ; resolve: (value: any) => void ; reject: (reason?: any) => void }[] = []

Defined in​

src/channel/ws/ws_0_9.ts:191


events​

• Private events: EventEmitter<WebSocketChannelEvents>

Defined in​

src/channel/ws/ws_0_9.ts:198


closeListener​

• Private closeListener: (ev: CloseEvent) => void

Type declaration​

▸ (ev): void

Parameters​
NameType
evCloseEvent
Returns​

void

Defined in​

src/channel/ws/ws_0_9.ts:202


messageListener​

• Private messageListener: (event: MessageEvent<any>) => void

Type declaration​

▸ (event): void

Parameters​
NameType
eventMessageEvent<any>
Returns​

void

Defined in​

src/channel/ws/ws_0_9.ts:204


sendId​

• Private sendId: number = 0

JSON RPC latest sent message ID. The receiving message is expected to contain the same ID.

Defined in​

src/channel/ws/ws_0_9.ts:212

Methods​

openListener​

▸ openListener(ev): void

Parameters​

NameType
evEvent

Returns​

void

Defined in​

src/channel/ws/ws_0_9.ts:200


errorListener​

▸ errorListener(ev): void

Parameters​

NameType
evEvent

Returns​

void

Defined in​

src/channel/ws/ws_0_9.ts:206


idResolver​

▸ idResolver(id?): number

Parameters​

NameType
id?number

Returns​

number

Defined in​

src/channel/ws/ws_0_9.ts:238


send​

▸ send(method, params?, id?): number

Sends a JSON-RPC request over the WebSocket connection without waiting for a response. This is a low-level method. Prefer sendReceive for most use cases.

Parameters​

NameTypeDescription
methodstringThe RPC method name.
params?objectThe parameters for the RPC method.
id?numberA specific request ID. If not provided, an auto-incrementing ID is used.

Returns​

number

The ID of the sent request.

Throws

If the WebSocket is not connected.

Defined in​

src/channel/ws/ws_0_9.ts:255


sendReceive​

▸ sendReceive<T>(method, params?): Promise<T>

Sends a JSON-RPC request and returns a Promise that resolves with the result. This method abstracts the request/response cycle over WebSockets. If the connection is lost, it will queue the request and send it upon reconnection.

Type parameters​

NameTypeDescription
TanyThe expected type of the result.

Parameters​

NameTypeDescription
methodstringThe RPC method name.
params?objectThe parameters for the RPC method.

Returns​

Promise<T>

A Promise that resolves with the RPC response result.

Throws

If the request does not receive a response within the configured requestTimeout.

Throws

If the WebSocket is not connected and auto-reconnect is disabled.

Defined in​

src/channel/ws/ws_0_9.ts:284


isConnected​

▸ isConnected(): boolean

Checks if the WebSocket connection is currently open.

Returns​

boolean

true if the connection is open, false otherwise.

Defined in​

src/channel/ws/ws_0_9.ts:359


waitForConnection​

▸ waitForConnection(): Promise<number>

Returns a Promise that resolves when the WebSocket connection is open. Can be used to block execution until the connection is established.

Returns​

Promise<number>

A Promise that resolves with the WebSocket's readyState when connected.

Example

const channel = new WebSocketChannel({ nodeUrl: '...' });
await channel.waitForConnection();
console.log('Connected!');

Defined in​

src/channel/ws/ws_0_9.ts:374


disconnect​

▸ disconnect(code?, reason?): void

Closes the WebSocket connection. This method is user-initiated and will prevent automatic reconnection for this closure.

Parameters​

NameTypeDescription
code?numberThe WebSocket connection close code.
reason?stringThe WebSocket connection close reason.

Returns​

void

Defined in​

src/channel/ws/ws_0_9.ts:395


waitForDisconnection​

▸ waitForDisconnection(): Promise<number | Event>

Returns a Promise that resolves when the WebSocket connection is closed.

Returns​

Promise<number | Event>

A Promise that resolves with the WebSocket's readyState or a CloseEvent when disconnected.

Defined in​

src/channel/ws/ws_0_9.ts:408


unsubscribe​

▸ unsubscribe(subscriptionId): Promise<boolean>

Unsubscribes from a Starknet subscription. It is recommended to use the unsubscribe() method on the Subscription object instead.

Parameters​

NameTypeDescription
subscriptionIdstringThe ID of the subscription to unsubscribe from.

Returns​

Promise<boolean>

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

Defined in​

src/channel/ws/ws_0_9.ts:428


waitForUnsubscription​

▸ waitForUnsubscription(targetId): Promise<void>

Returns a Promise that resolves when a specific subscription is successfully unsubscribed.

Parameters​

NameTypeDescription
targetIdstringThe ID of the subscription to wait for.

Returns​

Promise<void>

Example

await channel.waitForUnsubscription(mySubscription.id);
console.log('Successfully unsubscribed.');

Defined in​

src/channel/ws/ws_0_9.ts:448


reconnect​

▸ reconnect(): void

Manually initiates a reconnection attempt. This creates a new WebSocket instance and re-establishes listeners.

Returns​

void

Defined in​

src/channel/ws/ws_0_9.ts:464


_processRequestQueue​

▸ _processRequestQueue(): void

Returns​

void

Defined in​

src/channel/ws/ws_0_9.ts:474


_restoreSubscriptions​

▸ _restoreSubscriptions(): Promise<void>

Returns​

Promise<void>

Defined in​

src/channel/ws/ws_0_9.ts:482


_startReconnect​

▸ _startReconnect(): void

Returns​

void

Defined in​

src/channel/ws/ws_0_9.ts:502


onCloseProxy​

▸ onCloseProxy(ev): void

Parameters​

NameType
evCloseEvent

Returns​

void

Defined in​

src/channel/ws/ws_0_9.ts:546


onMessageProxy​

▸ onMessageProxy(event): void

Parameters​

NameType
eventMessageEvent<any>

Returns​

void

Defined in​

src/channel/ws/ws_0_9.ts:558


subscribeNewHeads​

▸ subscribeNewHeads(params?): Promise<SubscriptionNewHeadsEvent>

Subscribes to new block headers.

Parameters​

NameTypeDescription
paramsSubscribeNewHeadsParamsThe parameters for the subscription.

Returns​

Promise<SubscriptionNewHeadsEvent>

A Promise that resolves with a Subscription object for new block headers.

Defined in​

src/channel/ws/ws_0_9.ts:597


subscribeEvents​

▸ subscribeEvents(params?): Promise<SubscriptionStarknetEventsEvent>

Subscribes to events matching a given filter.

Parameters​

NameTypeDescription
paramsSubscribeEventsParamsThe parameters for the subscription.

Returns​

Promise<SubscriptionStarknetEventsEvent>

A Promise that resolves with a Subscription object for the specified events.

Defined in​

src/channel/ws/ws_0_9.ts:621


subscribeTransactionStatus​

▸ subscribeTransactionStatus(params): Promise<SubscriptionTransactionStatusEvent>

Subscribes to status updates for a specific transaction.

Parameters​

NameTypeDescription
paramsSubscribeTransactionStatusParamsThe parameters for the subscription.

Returns​

Promise<SubscriptionTransactionStatusEvent>

A Promise that resolves with a Subscription object for the transaction's status.

Defined in​

src/channel/ws/ws_0_9.ts:648


subscribeNewTransactionReceipts​

▸ subscribeNewTransactionReceipts(params?): Promise<SubscriptionNewTransactionReceiptsEvent>

Subscribes to new transaction receipts.

Parameters​

NameTypeDescription
paramsSubscribeNewTransactionReceiptsParamsThe parameters for the subscription.

Returns​

Promise<SubscriptionNewTransactionReceiptsEvent>

A Promise that resolves with a Subscription object for new transaction receipts.

Defined in​

src/channel/ws/ws_0_9.ts:673


subscribeNewTransactions​

▸ subscribeNewTransactions(params?): Promise<SubscriptionNewTransactionEvent>

Subscribes to new transactions.

Parameters​

NameTypeDescription
paramsSubscribeNewTransactionsParamsThe parameters for the subscription.

Returns​

Promise<SubscriptionNewTransactionEvent>

A Promise that resolves with a Subscription object for new transactions.

Defined in​

src/channel/ws/ws_0_9.ts:699


removeSubscription​

▸ removeSubscription(id): void

Internal method to remove subscription from active map.

Parameters​

NameType
idstring

Returns​

void

Defined in​

src/channel/ws/ws_0_9.ts:724


on​

▸ on<K>(event, listener): void

Adds a listener for a given event.

Type parameters​

NameType
Kextends keyof WebSocketChannelEvents

Parameters​

NameTypeDescription
eventKThe event name.
listener(data: WebSocketChannelEvents[K]) => voidThe listener function to add.

Returns​

void

Defined in​

src/channel/ws/ws_0_9.ts:733


off​

▸ off<K>(event, listener): void

Removes a listener for a given event.

Type parameters​

NameType
Kextends keyof WebSocketChannelEvents

Parameters​

NameTypeDescription
eventKThe event name.
listener(data: WebSocketChannelEvents[K]) => voidThe listener function to remove.

Returns​

void

Defined in​

src/channel/ws/ws_0_9.ts:745