nostr-websocket-utils / NostrWSClient
WebSocket client implementation for Nostr protocol communication Extends EventEmitter to provide event-based message handling
EventEmitternew NostrWSClient(
url,options):NostrWSClient
string
Partial<NostrWSOptions> = {}
EventEmitter.constructor
privatews:null|ExtendedWebSocket=null
privatestate:ConnectionState=ConnectionState.DISCONNECTED
privatemessageQueue:MessageQueue
privatereconnectTimeout:null|Timeout=null
privateheartbeatInterval:null|Timeout=null
privateheartbeatTimeout:null|Timeout=null
privatemissedHeartbeats:number=0
privatereconnectAttempts:number=0
privatesubscriptions:Map<string,NostrWSMessage>
privatereadonlyclientId:string
privatereadonlyoptions:NostrWSOptions
privatereadonlyurl:string
get connectionState():
ConnectionState
Gets the current connection state
privatesetState(newState):void
Updates the connection state and notifies handlers
void
connect():
Promise<void>
Establishes a connection to the WebSocket server
Promise<void>
privatesetupEventHandlers():void
Sets up event handlers for the WebSocket connection
void
privatestartHeartbeat():void
Starts the heartbeat mechanism
void
privatehandleHeartbeatResponse():void
Handles heartbeat responses
void
privatestopHeartbeat():void
Stops the heartbeat mechanism
void
privatehandleConnectionError(error):void
Handles connection errors
Error
void
privatehandleDisconnection():void
Handles disconnection and cleanup
void
privatereconnect():void
Initiates reconnection with exponential backoff
void
subscribe(
channel,filter?):void
Subscribes to a channel with optional filter
string
unknown
void
privateresubscribeAll():void
Resubscribes to all active subscriptions
void
unsubscribe(
channel):void
Unsubscribes from a channel
string
void
privateflushMessageQueue():Promise<void>
Flushes the message queue by sending pending messages
Promise<void>
privatesendImmediate(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