Skip to content

Connection

Coordinates a full Discord voice connection. Owns four sub-components:

  • webSocket - WebSocket lifecycle and heartbeating
  • udp - UDP socket, keepalive, and RTP audio send/receive
  • rtp - RTP encryption and sequence/timestamp/nonce counters
  • dave - DAVE E2EE MLS session and per-user key rotation

Connection handles voice signalling (Identify, Resume, SelectProtocol, Speaking), opcode dispatch, jitter buffering per SSRC, and gateway shard integration.

Consumer-facing events: onReady, onSpeaking, onClientConnect, onClientDisconnect, onDaveEpochTransition, onRawAudioFrame, onAudioFrame.

Summary

Connection.webSocket  :: WebSocketTransport
Connection.udp  :: UdpTransport
Connection.rtp  :: RtpCrypto
Connection.dave  :: DaveManager
Connection.jitters  :: {
{ [number]: voiceJitter.Jitter } }
Connection.shard  :: Shard
Connection.guildId  :: string
Connection.channelId  :: string
Connection.userId  :: string
Connection.sessionId  :: string
Connection.token  :: Secret<string>
Connection.endpoint  :: string
Connection.onConnected  :: Emitter<()>
Connection.onDisconnected  :: Emitter<number?, boolean?>
Connection.onReady  :: Emitter<()>
Connection.onHeartbeat  :: Emitter<number?>
Connection.onRawMessage  :: Payload<unknown>
Connection.onClientConnect  :: Emitter<{ userId: string }>
Connection.onClientDisconnect  :: Emitter<{ userId: string }>
Connection.onDaveEpochTransition  :: Emitter<number>
Connection.onRawAudioFrame  :: Emitter<{ userId: string?, ssrc: number, frame: buffer }>

Fires for each decoded audio frame before Opus decoding. frame is the fully decrypted RTP packet (12-byte RTP header + Opus payload) with all encryption layers (DAVE E2EE and RTP encryption) already removed. Wire-level ciphertext is never exposed.

Connection.onAudioFrame  :: Emitter<{ userId: string?, ssrc: number, frame: buffer }>
Connection.logger  :: Logger
Connection : sendAsync ( data )  -> ()

Sends a raw message to the voice gateway WebSocket.

Parameters

data: string | buffer
Connection : connectAsync ( )  -> ()

Opens the voice WebSocket connection. Session state is preserved when resuming.

Connection : forceClose ( code )  -> ()

Closes the voice WebSocket connection with an optional close code.

Parameters

code: number?
Connection : forceClose ( code )  -> ()

Synchronously closes the voice connection and cleans up all session state. Safe to call from a signal handler or any context where yielding is not permitted.

Parameters

code: number?
Connection : reconnectAsync ( )  -> ()

Disconnects and reconnects the voice WebSocket. Concurrent reconnect attempts are rejected.

Connection : requestVoiceAsync ( )  -> ()

Sends a VOICE_STATE_UPDATE to the gateway shard to request a voice server for this channel.

Connection : setSpeakingAsync ( speakingFlags )  -> ()

Sets the bot’s speaking state. Pass a bitmask of speaking flags (Microphone = 1, Soundshare = 2, Priority = 4).

Parameters

speakingFlags: number
Connection : sendOpusAsync ( audioFrame )  -> ()

Encrypts and sends a raw Opus audio frame over UDP.

Parameters

audioFrame: buffer