nostr-websocket-utils / NostrWSClient
WebSocket client implementation for Nostr protocol communication Extends EventEmitter to provide event-based message handling
EventEmitter
new NostrWSClient(
url
,options
):NostrWSClient
string
Partial
<NostrWSOptions
> = {}
EventEmitter.constructor
private
ws:null
|ExtendedWebSocket
=null
private
state:ConnectionState
=ConnectionState.DISCONNECTED
private
messageQueue:MessageQueue
private
reconnectTimeout:null
|Timeout
=null
private
heartbeatInterval:null
|Timeout
=null
private
heartbeatTimeout:null
|Timeout
=null
private
missedHeartbeats:number
=0
private
reconnectAttempts:number
=0
private
subscriptions:Map
<string
,NostrWSMessage
>
private
readonly
clientId:string
private
readonly
options:NostrWSOptions
private
readonly
url:string
get connectionState():
ConnectionState
Gets the current connection state
private
setState(newState
):void
Updates the connection state and notifies handlers
void
connect():
Promise
<void
>
Establishes a connection to the WebSocket server
Promise
<void
>
private
setupEventHandlers():void
Sets up event handlers for the WebSocket connection
void
private
startHeartbeat():void
Starts the heartbeat mechanism
void
private
handleHeartbeatResponse():void
Handles heartbeat responses
void
private
stopHeartbeat():void
Stops the heartbeat mechanism
void
private
handleConnectionError(error
):void
Handles connection errors
Error
void
private
handleDisconnection():void
Handles disconnection and cleanup
void
private
reconnect():void
Initiates reconnection with exponential backoff
void
subscribe(
channel
,filter
?):void
Subscribes to a channel with optional filter
string
unknown
void
private
resubscribeAll():void
Resubscribes to all active subscriptions
void
unsubscribe(
channel
):void
Unsubscribes from a channel
string
void
private
flushMessageQueue():Promise
<void
>
Flushes the message queue by sending pending messages
Promise
<void
>
private
sendImmediate(message
):Promise
<void
>
Sends a message immediately without queueing
Promise
<void
>
send(
message
):Promise
<void
>
Sends a message to the WebSocket server
Promise
<void
>
close():
void
Closes the WebSocket connection
void