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
| Name | Type | Description |
|---|---|---|
options | WebSocketOptions | The options for configuring the channel. |
Returns
Defined in
Properties
nodeUrl
• nodeUrl: string
The URL of the WebSocket RPC Node.
Example
'wss://starknet-sepolia.public.blastapi.io/rpc/v0_8';
Defined in
websocket
• websocket: WebSocket
The underlying WebSocket instance.
Defined in
WsImplementation
• Private WsImplementation: WebSocketModule
Defined in
activeSubscriptions
• Private activeSubscriptions: Map<string, Subscription<any>>
Defined in
maxBufferSize
• Private Readonly maxBufferSize: number
Defined in
autoReconnect
• Private Readonly autoReconnect: boolean
Defined in
reconnectOptions
• Private Readonly reconnectOptions: Required<ReconnectOptions>
Defined in
requestTimeout
• Private Readonly requestTimeout: number
Defined in
isReconnecting
• Private isReconnecting: boolean = false
Defined in
reconnectAttempts
• Private reconnectAttempts: number = 0
Defined in
userInitiatedClose
• Private userInitiatedClose: boolean = false
Defined in
reconnectTimeoutId
• Private reconnectTimeoutId: null | Timeout = null
Defined in
requestQueue
• Private requestQueue: { method: string ; params?: object ; resolve: (value: any) => void ; reject: (reason?: any) => void }[] = []
Defined in
events
• Private events: EventEmitter<WebSocketChannelEvents>
Defined in
closeListener
• Private closeListener: (ev: CloseEvent) => void
Type declaration
▸ (ev): void
Parameters
| Name | Type |
|---|---|
ev | CloseEvent |
Returns
void
Defined in
messageListener
• Private messageListener: (event: MessageEvent<any>) => void
Type declaration
▸ (event): void
Parameters
| Name | Type |
|---|---|
event | MessageEvent<any> |
Returns
void
Defined in
sendId
• Private sendId: number = 0
JSON RPC latest sent message ID. The receiving message is expected to contain the same ID.
Defined in
Methods
openListener
▸ openListener(ev): void
Parameters
| Name | Type |
|---|---|
ev | Event |
Returns
void
Defined in
errorListener
▸ errorListener(ev): void
Parameters
| Name | Type |
|---|---|
ev | Event |
Returns
void
Defined in
idResolver
▸ idResolver(id?): number
Parameters
| Name | Type |
|---|---|
id? | number |
Returns
number
Defined in
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
| Name | Type | Description |
|---|---|---|
method | string | The RPC method name. |
params? | object | The parameters for the RPC method. |
id? | number | A 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
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
| Name | Type | Description |
|---|---|---|
T | any | The expected type of the result. |
Parameters
| Name | Type | Description |
|---|---|---|
method | string | The RPC method name. |
params? | object | The 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.