Star ✨ on GitHub

WebSocket

API reference for the custom WebSocket client in Moonlink.js

WebSocket Class

The WebSocket class in Moonlink.js is a custom implementation of a WebSocket client, extending Node.js's EventEmitter. It handles the low-level WebSocket protocol for communication with Lavalink servers.

Properties

PropertyTypeDescription
urlURLThe URL of the WebSocket server.
headersRecord<string, string>Headers sent during the WebSocket handshake.
connectedbooleanIndicates whether the WebSocket is currently connected.

Methods

constructor

constructormethod

Constructor

Creates a new WebSocket instance and initiates the connection.

Parameters
urlrequiredstring
The WebSocket server URL (e.g., ws://localhost:2333/v4/websocket).
optionsObject
Optional: Configuration options.
options.headersRecord<string, string>
Optional: Custom headers to send during the handshake.

Returns & Example

ReturnsWebSocket

// Used internally by Node class
// const socket = new WebSocket('ws://localhost:2333/v4/websocket', { headers: { Authorization: 'youshallnotpass' } });

::

send

sendmethod

Send Data

Sends data over the WebSocket connection.

Parameters
datarequiredstring | Buffer
The data to send (string or Buffer).
cb(err?: Error) => void
Optional: A callback function to be called when the send operation completes.

Returns & Example

Returnsvoid

// Used internally by Node class
// socket.send(JSON.stringify({ op: 'play', /* ... */ }));

close

closemethod

Close Connection

Closes the WebSocket connection.

Parameters
codenumber
1000
Optional: The WebSocket close code.
reasonstring
''
Optional: A reason for closing the connection.

Returns & Example

Returnsvoid

// Used internally by Node class
// socket.close(1000, 'Normal Closure');

addEventListener

addEventListenermethod

Add Event Listener

Registers an event listener for WebSocket events.

Parameters
eventrequiredstring
The event name (e.g., open, message, close, error).
listenerrequired(...args: any[]) => void
The callback function for the event.
options{ once?: boolean }
Optional: Options for the listener (e.g., once: true for a one-time listener).

Returns & Example

Returnsvoid

// Used internally by Node class
// socket.addEventListener('message', (event) => console.log('Received:', event.data));

Events

The WebSocket class extends EventEmitter and emits the following events:

EventDescriptionParameters
openEmitted when the WebSocket connection is established.void
messageEmitted when a message is received from the server.`{ data: string
closeEmitted when the WebSocket connection is closed.{ code: number, reason: string }
errorEmitted when an error occurs.{ error: Error }
pingEmitted when a ping frame is received.void
pongEmitted when a pong frame is received.void

Usage Example

Basic Usage

Example of how the WebSocket class is used internally by the Node.

Code Example

// This is an internal class, direct usage is generally not recommended.
// Example of how it's used within the Node class:

// In Node's connect method:
// this.socket = new WebSocket(
//   `ws${this.secure ? "s" : ""}://${this.address}/${this.pathVersion}/websocket`,
//   {
//     headers: {
//       Authorization: this.password,
//       "User-Id": this.manager.options.clientId,
//       "Client-Name": this.manager.options.clientName,
//     },
//   }
// );
// this.socket.addEventListener('open', this.open.bind(this), { once: true });
// this.socket.addEventListener('close', this.close.bind(this), { once: true });
// this.socket.addEventListener('message', this.message.bind(this));
// this.socket.addEventListener('error', this.error.bind(this));

// In Node's message method (handling incoming data):
// this.socket.send(JSON.stringify({ op: 'pong' }));

// In Node's close method:
// this.socket.close();
This example demonstrates basic operations using the WebSocket class.