Skip to main content

Node

A Node in Yukino represents a connection to a Lavalink server. It handles track loading, audio processing, and manages the WebSocket connection to the Lavalink server.

Node Configuration​

interface NodeOptions {
name: string; // Unique identifier for the node
url: string; // URL of the Lavalink server
auth: string; // Password for the Lavalink server
secure?: boolean; // Whether to use WSS/HTTPS
group?: string; // Group identifier for region-based routing
// Reconnection options (all are supported, fallback order shown)
reconnectTries?: number; // Number of reconnection attempts (legacy)
reconnectInterval?: number;// Delay between retries in ms (legacy)
retryAmount?: number; // Alias for reconnectTries
retryDelay?: number; // Alias for reconnectInterval
reconnectOptions?: { // Nested reconnection settings (docs style)
retryAmount?: number; // Number of reconnection attempts
retryDelay?: number; // Delay between retries in ms
};
resumeKey?: string | null;
resumeTimeout?: number;
version?: string;
debug?: boolean;
priority?: number;
region?: string;
}

Node States​

A node can be in one of these states:

  • CONNECTING - Initial connection attempt
  • CONNECTED - Successfully connected
  • DISCONNECTED - Connection lost or destroyed (destroyed nodes emit a destroyed event and set state to DISCONNECTED)
  • RECONNECTING - Attempting to reconnect

Note: There is no explicit DESTROYED state in the code, but the node emits a destroyed event when destroyed.

Connection Management​

Manual Connection​

// Connect to a node
await node.connect();

// Disconnect from a node
await node.disconnect();

// Destroy a node (permanent disconnect)
await node.destroy();

Auto-Reconnection​

Nodes automatically attempt to reconnect when disconnected:

// Configure reconnection behavior
const node = yukino.addNode({
name: 'main',
url: 'localhost:2333',
auth: 'password',
retryAmount: 5, // Try 5 times (or use reconnectTries)
retryDelay: 5000 // Wait 5 seconds between attempts (or use reconnectInterval)
});

// Listen for reconnection attempts
node.on('nodeReconnect', (node, attempt) => {
console.log(`Reconnection attempt ${attempt}`);
});

Track Loading​

Load Tracks​

// Load a single track
const track = await node.loadTrack('youtube:dQw4w9WgXcQ');

// Load a playlist
const playlist = await node.loadTrack('youtube:PLplaylist_id');

// Search for tracks
const results = await node.loadTrack('ytsearch:never gonna give you up');

Track Loading Response​

interface LoadTrackResponse {
loadType: 'TRACK_LOADED' | 'PLAYLIST_LOADED' | 'SEARCH_RESULT' | 'NO_MATCHES' | 'LOAD_FAILED';
playlistInfo?: {
name: string;
selectedTrack: number;
};
tracks: Track[];
exception?: {
message: string;
severity: 'COMMON' | 'SUSPICIOUS' | 'FAULT';
};
}

Resource Management​

Memory Management​

// Get node stats
const stats = node.stats;
console.log('Memory used:', stats.memory.used);
console.log('Memory allocated:', stats.memory.allocated);
console.log('Memory reservable:', stats.memory.reservable);

// Monitor memory usage
node.on('nodeStats', (node, stats) => {
const memoryUsage = (stats.memory.used / stats.memory.allocated) * 100;
if (memoryUsage > 90) {
console.warn(`High memory usage on node ${node.name}: ${memoryUsage}%`);
}
});

CPU Management​

// Get CPU stats
const stats = node.stats;
console.log('CPU cores:', stats.cpu.cores);
console.log('System load:', stats.cpu.systemLoad);
console.log('Lavalink load:', stats.cpu.lavalinkLoad);

// Monitor CPU usage
node.on('nodeStats', (node, stats) => {
if (stats.cpu.systemLoad > 80) {
console.warn(`High CPU usage on node ${node.name}`);
}
});

Error Handling​

Connection Errors​

node.on('error', (error) => {
if (error.code === 'ECONNREFUSED') {
console.error('Failed to connect to Lavalink server');
} else if (error.code === 'AUTHENTICATION_FAILED') {
console.error('Invalid password');
}
});

Track Load Errors​

try {
const result = await node.loadTrack('invalid:url');
} catch (error) {
if (error.loadType === 'LOAD_FAILED') {
console.error('Failed to load track:', error.exception.message);
}
}

Performance Monitoring​

Frame Statistics​

node.on('nodeStats', (node, stats) => {
// Monitor frame statistics
console.log('Frames sent:', stats.frames.sent);
console.log('Frames nulled:', stats.frames.nulled);
console.log('Frames deficit:', stats.frames.deficit);
});

Player Statistics​

node.on('nodeStats', (node, stats) => {
// Monitor player counts
console.log('Total players:', stats.players);
console.log('Playing players:', stats.playingPlayers);
});