WebSocket
A custom WebSocket implementation that unifies behavior across Node.js and Bun environments, featuring automatic heartbeats and latency tracking.
The WebSocket class is an internal utility used by the Node class to communicate with Lavalink. It abstracts away the differences between Node.js native http/https requests and Bun's native WebSocket implementation.
Extends: EventEmitter
Constructor
new WebSocket(url, options)
Creates a new WebSocket connection.
The URL to connect to.
Connection options.
options
Custom headers to send with the handshake.
Properties
WebSocket Properties
Connection state and metrics.
State
The current state of the connection (1 = Open, 3 = Closed).
The current round-trip latency in milliseconds.
Events
WebSocket Events
Events emitted by the socket during its lifecycle.
Lifecycle
Emitted when the connection is established.
Emitted when the connection is closed.
Emitted when an error occurs.
Data & Debug
Emitted when a message is received.
Emitted when a ping frame is received.
Emitted when a pong frame is received.
Emitted for debug logs (Node.js only).
Methods
send(data)
send(data)#
→voidSends data to the WebSocket server.
The data to send.
close(code, reason)
close(code, reason)#
→voidCloses the WebSocket connection.
Status code (default: 1000).
Reason for closing.
ping(data)
ping(data)#
→Promise<number>Sends a ping frame and waits for a pong. Returns the latency in milliseconds. Note: Not supported in Bun environments.
Optional payload for the ping.
on(event, listener)
on(event, listener)#
→thisAdds a listener for the specified event. Inherited from EventEmitter.
The event name.
The callback function.
once(event, listener)
Adds a one-time listener for the specified event. Inherited from EventEmitter.
The event name.
The callback function.
off(event, listener)
Removes a listener for the specified event. Inherited from EventEmitter.
The event name.
The callback function to remove.
emit(event, ...args)
emit(event, ...args)#
→booleanSynchronously calls each of the listeners registered for the event. Inherited from EventEmitter.
The event name.
Arguments to pass to the listeners.