Class: WebSocketChannel
WebSocket channel provides communication with Starknet node over long-lived socket connection
Constructors​
constructor​
• new WebSocketChannel(options?
): WebSocketChannel
Construct class and event listeners
Parameters​
Name | Type | Description |
---|---|---|
options | WebSocketOptions | WebSocketOptions |
Returns​
Defined in​
Properties​
nodeUrl​
• nodeUrl: string
WebSocket RPC Node URL
Example
'wss://starknet-node.io/rpc/v0_8'
Defined in​
websocket​
• websocket: WebSocket
ws library object
Defined in​
onReorg​
• onReorg: (this
: WebSocketChannel
, data
: SubscriptionReorgResponse
) => any
Type declaration​
â–¸ (this
, data
): any
Assign implementation method to get 'on reorg event data'
Parameters​
Name | Type |
---|---|
this | WebSocketChannel |
data | SubscriptionReorgResponse |
Returns​
any
Example
webSocketChannel.onReorg = async function (data) {
// ... do something when reorg happens
}
Defined in​
onNewHeads​
• onNewHeads: (this
: WebSocketChannel
, data
: SubscriptionNewHeadsResponse
) => any
Type declaration​
â–¸ (this
, data
): any
Assign implementation method to get 'starknet block heads'
Parameters​
Name | Type |
---|---|
this | WebSocketChannel |
data | SubscriptionNewHeadsResponse |
Returns​
any
Example
webSocketChannel.onNewHeads = async function (data) {
// ... do something with head data
}
Defined in​
onEvents​
• onEvents: (this
: WebSocketChannel
, data
: SubscriptionEventsResponse
) => any
Type declaration​
â–¸ (this
, data
): any
Assign implementation method to get 'starknet events'
Parameters​
Name | Type |
---|---|
this | WebSocketChannel |
data | SubscriptionEventsResponse |
Returns​
any
Example
webSocketChannel.onEvents = async function (data) {
// ... do something with event data
}
Defined in​
onTransactionStatus​
• onTransactionStatus: (this
: WebSocketChannel
, data
: SubscriptionTransactionsStatusResponse
) => any
Type declaration​
â–¸ (this
, data
): any
Assign method to get 'starknet transactions status'
Parameters​
Name | Type |
---|---|
this | WebSocketChannel |
data | SubscriptionTransactionsStatusResponse |
Returns​
any
Example
webSocketChannel.onTransactionStatus = async function (data) {
// ... do something with tx status data
}
Defined in​
onPendingTransaction​
• onPendingTransaction: (this
: WebSocketChannel
, data
: SubscriptionPendingTransactionsResponse
) => any
Type declaration​
â–¸ (this
, data
): any
Assign implementation method to get 'starknet pending transactions (mempool)'
Parameters​
Name | Type |
---|---|
this | WebSocketChannel |
data | SubscriptionPendingTransactionsResponse |
Returns​
any
Example
webSocketChannel.onPendingTransaction = async function (data) {
// ... do something with pending tx data
}
Defined in​
onOpen​
• onOpen: (this
: WebSocketChannel
, ev
: Event
) => any
Type declaration​
â–¸ (this
, ev
): any
Assign implementation to this method to listen open Event
Parameters​
Name | Type |
---|---|
this | WebSocketChannel |
ev | Event |
Returns​
any
Defined in​
onClose​
• onClose: (this
: WebSocketChannel
, ev
: CloseEvent
) => any
Type declaration​
â–¸ (this
, ev
): any
Assign implementation to this method to listen close CloseEvent
Parameters​
Name | Type |
---|---|
this | WebSocketChannel |
ev | CloseEvent |
Returns​
any
Defined in​
onMessage​
• onMessage: (this
: WebSocketChannel
, ev
: MessageEvent
<any
>) => any
Type declaration​
â–¸ (this
, ev
): any
Assign implementation to this method to listen message MessageEvent
Parameters​
Name | Type |
---|---|
this | WebSocketChannel |
ev | MessageEvent <any > |
Returns​
any
Defined in​
onError​
• onError: (this
: WebSocketChannel
, ev
: Event
) => any
Type declaration​
â–¸ (this
, ev
): any
Assign implementation to this method to listen error Event
Parameters​
Name | Type |
---|---|
this | WebSocketChannel |
ev | Event |
Returns​
any
Defined in​
onUnsubscribe​
• onUnsubscribe: (this
: WebSocketChannel
, _subscriptionId
: number
) => any
Type declaration​
â–¸ (this
, _subscriptionId
): any
Assign implementation to this method to listen unsubscription
Parameters​
Name | Type |
---|---|
this | WebSocketChannel |
_subscriptionId | number |
Returns​
any
Defined in​
onUnsubscribeLocal​
• Private
onUnsubscribeLocal: (this
: WebSocketChannel
, _subscriptionId
: number
) => any
Type declaration​
â–¸ (this
, _subscriptionId
): any
Parameters​
Name | Type |
---|---|
this | WebSocketChannel |
_subscriptionId | number |
Returns​
any
Defined in​
sendId​
• Private
sendId: number
= 0
JSON RPC latest sent message id expecting receiving message to contain same id
Defined in​
subscriptions​
• Readonly
subscriptions: Map
<string
, number
>
subscriptions ids mapped by keys WSSubscriptions
Defined in​
Methods​
idResolver​
â–¸ idResolver(id?
): number
Parameters​
Name | Type |
---|---|
id? | number |
Returns​
number
Defined in​
send​
â–¸ send(method
, params?
, id?
): number
Send data over open ws connection
- this would only send data on the line without awaiting 'response message'
Parameters​
Name | Type |
---|---|
method | string |
params? | object |
id? | number |
Returns​
number
Example
const sentId = await this.send('starknet_method', params);
Defined in​
sendReceiveAny​
â–¸ sendReceiveAny(method
, params?
): Promise
<any
>
Any Starknet method not just websocket override
Parameters​
Name | Type |
---|---|
method | any |
params? | any |
Returns​
Promise
<any
>
Defined in​
sendReceive​
â–¸ sendReceive<T
>(method
, params?
): Promise
<any
>
Send request and receive response over ws line This method abstract ws messages into request/response model
Type parameters​
Name | Type |
---|---|
T | extends keyof WebSocketMethods |
Parameters​
Name | Type | Description |
---|---|---|
method | T | rpc method name |
params? | WebSocketMethods [T ][``"params"``] | rpc method parameters |
Returns​
Promise
<any
>
Example
const response = await this.sendReceive('starknet_method', params);
Defined in​
isConnected​
â–¸ isConnected(): boolean
Helper to check connection is open
Returns​
boolean
Defined in​
waitForConnection​
â–¸ waitForConnection(): Promise
<number
>
await while websocket is connected
- could be used to block the flow until websocket is open
Returns​
Promise
<number
>
Example
const readyState = await webSocketChannel.waitForConnection();
Defined in​
disconnect​
â–¸ disconnect(code?
, reason?
): void
Disconnect the WebSocket connection, optionally using code as the the WebSocket connection close code and reason as the the WebSocket connection close reason.
Parameters​
Name | Type |
---|---|
code? | number |
reason? | string |
Returns​
void
Defined in​
waitForDisconnection​
â–¸ waitForDisconnection(): Promise
<number
| Event
>
await while websocket is disconnected
Returns​
Promise
<number
| Event
>
Example
const readyState = await webSocketChannel.waitForDisconnection();
Defined in​
unsubscribe​
â–¸ unsubscribe(subscriptionId
, ref?
): Promise
<boolean
>
Unsubscribe from starknet subscription
Parameters​
Name | Type | Description |
---|---|---|
subscriptionId | number | |
ref? | string | internal usage, only for managed subscriptions |
Returns​
Promise
<boolean
>
Defined in​
waitForUnsubscription​
â–¸ waitForUnsubscription(forSubscriptionId?
): Promise
<unknown
>
await while subscription is unsubscribed
Parameters​
Name | Type | Description |
---|---|---|
forSubscriptionId? | number | if defined trigger on subscriptionId else trigger on any |
Returns​
Promise
<unknown
>
subscriptionId | onerror(Event)
Example
const subscriptionId = await webSocketChannel.waitForUnsubscription();
Defined in​
reconnect​
â–¸ reconnect(): void
Reconnect re-create this.websocket instance
Returns​
void
Defined in​
reconnectAndUpdate​
â–¸ reconnectAndUpdate(): void
Returns​
void
Defined in​
onCloseProxy​
â–¸ onCloseProxy(ev
): void
Parameters​
Name | Type |
---|---|
ev | CloseEvent |
Returns​
void
Defined in​
onMessageProxy​
â–¸ onMessageProxy(event
): void
Parameters​
Name | Type |
---|---|
event | MessageEvent <any > |
Returns​
void
Defined in​
subscribeNewHeadsUnmanaged​
â–¸ subscribeNewHeadsUnmanaged(blockIdentifier?
): Promise
<number
>
subscribe to new block heads
- you can subscribe to this event multiple times and you need to manage subscriptions manually
Parameters​
Name | Type |
---|---|
blockIdentifier? | SubscriptionBlockIdentifier |
Returns​
Promise
<number
>
Defined in​
subscribeNewHeads​
â–¸ subscribeNewHeads(blockIdentifier?
): Promise
<number
| false
>
subscribe to new block heads
Parameters​
Name | Type |
---|---|
blockIdentifier? | SubscriptionBlockIdentifier |
Returns​
Promise
<number
| false
>
Defined in​
unsubscribeNewHeads​
â–¸ unsubscribeNewHeads(): Promise
<boolean
>
Unsubscribe newHeads subscription
Returns​
Promise
<boolean
>
Defined in​
subscribeEventsUnmanaged​
â–¸ subscribeEventsUnmanaged(fromAddress?
, keys?
, blockIdentifier?
): Promise
<number
>
subscribe to 'starknet events'
- you can subscribe to this event multiple times and you need to manage subscriptions manually
Parameters​
Name | Type |
---|---|
fromAddress? | BigNumberish |
keys? | string [][] |
blockIdentifier? | SubscriptionBlockIdentifier |
Returns​
Promise
<number
>
Defined in​
subscribeEvents​
â–¸ subscribeEvents(fromAddress?
, keys?
, blockIdentifier?
): Promise
<number
| false
>
subscribe to 'starknet events'
Parameters​
Name | Type |
---|---|
fromAddress? | BigNumberish |
keys? | string [][] |
blockIdentifier? | SubscriptionBlockIdentifier |
Returns​
Promise
<number
| false
>
Defined in​
unsubscribeEvents​
â–¸ unsubscribeEvents(): Promise
<boolean
>
Unsubscribe 'starknet events' subscription
Returns​
Promise
<boolean
>
Defined in​
subscribeTransactionStatusUnmanaged​
â–¸ subscribeTransactionStatusUnmanaged(transactionHash
, blockIdentifier?
): Promise
<number
>
subscribe to transaction status
- you can subscribe to this event multiple times and you need to manage subscriptions manually
Parameters​
Name | Type |
---|---|
transactionHash | BigNumberish |
blockIdentifier? | SubscriptionBlockIdentifier |
Returns​
Promise
<number
>
Defined in​
subscribeTransactionStatus​
â–¸ subscribeTransactionStatus(transactionHash
): Promise
<number
| false
>
subscribe to transaction status
Parameters​
Name | Type |
---|---|
transactionHash | BigNumberish |
Returns​
Promise
<number
| false
>
Defined in​
unsubscribeTransactionStatus​
â–¸ unsubscribeTransactionStatus(): Promise
<boolean
>
unsubscribe 'transaction status' subscription
Returns​
Promise
<boolean
>
Defined in​
subscribePendingTransactionUnmanaged​
â–¸ subscribePendingTransactionUnmanaged(transactionDetails?
, senderAddress?
): Promise
<number
>
subscribe to pending transactions (mempool)
- you can subscribe to this event multiple times and you need to manage subscriptions manually
Parameters​
Name | Type |
---|---|
transactionDetails? | boolean |
senderAddress? | BigNumberish [] |
Returns​
Promise
<number
>
Defined in​
subscribePendingTransaction​
â–¸ subscribePendingTransaction(transactionDetails?
, senderAddress?
): Promise
<number
| false
>
subscribe to pending transactions (mempool)
Parameters​
Name | Type |
---|---|
transactionDetails? | boolean |
senderAddress? | BigNumberish [] |
Returns​
Promise
<number
| false
>
Defined in​
unsubscribePendingTransaction​
â–¸ unsubscribePendingTransaction(): Promise
<boolean
>
unsubscribe 'pending transaction' subscription
Returns​
Promise
<boolean
>