Skip to content

Session resumption

Lavalink can keep a session alive for a configurable window after your bot disconnects. If you reconnect within that window and present the original session ID, Lavalink resumes — queues keep playing, no interruption.

Discolink doesn’t implement this automatically. The right approach depends on your scale and persistence layer.

  1. Opt in and persist the session ID

    On a fresh connection, tell Lavalink to keep the session alive and store the session ID:

    player.on("nodeReady", async (node, resumed) => {
    if (!resumed) {
    // Enable resumption with a 60-second window
    await node.rest.updateSession({ resuming: true, timeout: 60 });
    // Persist the session ID — use your preferred storage
    await db.set(`session:${node.name}`, node.sessionId);
    }
    });
  2. Pass the session ID on reconnect

    Disable auto init and initialize nodes with their session IDs:

    const player = new Player({
    autoInit: false,
    // don't pass your nodes here anymore
    // ...
    });
    // Retrieve the stored session ID, then initialize
    const storedSessionId = await db.get("session:main");
    await player.init(clientId, [
    {
    name: "main",
    origin: "http://localhost:2333",
    password: "youshallnotpass",
    sessionId: storedSessionId ?? undefined,
    },
    ]);
  3. Sync local state after a successful resume

    After a node reconnects, update your queues:

    player.on("nodeReady", async (node, resumed) => {
    if (!resumed) {
    // Clear data associated with the node
    await db.del(`session:${node.name}`);
    await db.del(`queues:${node.name}`);
    return;
    }
    // Re-create queues from saved data
    const queues = await db.get(`queues:${node.name}`);
    for (const q of unpackQueues(queues)) await player.queues.create(q);
    // Note: syncing does not add a (current) track
    // Note: if there are too many queues, sync them individually not in bulk
    await player.queues.sync(node);
    });