Voice

Voice connections operate in a similar fashion to the Gateway connection. However, they use a different set of payloads and a separate UDP-based connection for voice data transmission. Because UDP is used for both receiving and transmitting voice data, your client must be able to receive UDP packets, even through a firewall or NAT (see UDP Hole Punching for more information). The Discord Voice servers implement functionality (see IP Discovery) for discovering the local machines remote UDP IP/Port, which can assist in some network configurations.

Voice Gateway Versioning

To ensure that you have the most up-to-date information, please use version 4. Otherwise, we cannot guarantee that the Opcodes documented here will reflect what you receive over the socket.

Gateway Versions
VersionStatusWebSocket URL Append
4recommended?v=4
3available?v=3
2available?v=2
1default?v=1 or omit

Connecting to Voice

Retrieving Voice Server Information

The first step in connecting to a voice server (and in turn, a guild's voice channel) is formulating a request that can be sent to the Gateway, which will return information about the voice server we will connect to. Because Discord's voice platform is widely distributed, users should never cache or save the results of this call. To inform the gateway of our intent to establish voice connectivity, we first send an Opcode 4 Gateway Voice State Update:

Gateway Voice State Update Example
json
{
"op": 4,
"d": {
"guild_id": "41771983423143937",
"channel_id": "127121515262115840",
"self_mute": false,
"self_deaf": false
}
}

If our request succeeded, the gateway will respond with two events—a Voice State Update event and a Voice Server Update event—meaning your library must properly wait for both events before continuing. The first will contain a new key, session_id, and the second will provide voice server information we can use to establish a new voice connection:

Example Voice Server Update Payload
json
{
"t": "VOICE_SERVER_UPDATE",
"s": 2,
"op": 0,
"d": {
"token": "my_token",
"guild_id": "41771983423143937",
"endpoint": "smart.loyal.discord.gg"
}
}

With this information, we can move on to establishing a voice WebSocket connection.

Establishing a Voice Websocket Connection

Once we retrieve a session_id, token, and endpoint information, we can connect and handshake with the voice server over another secure WebSocket. Unlike the gateway endpoint we receive in an HTTP Get Gateway request, the endpoint received from our Voice Server Update payload does not contain a URL protocol, so some libraries may require manually prepending it with "wss://" before connecting. Once connected to the voice WebSocket endpoint, we can send an Opcode 0 Identify payload with our server_id, user_id, session_id, and token:

Example Voice Identify Payload
json
{
"op": 0,
"d": {
"server_id": "41771983423143937",
"user_id": "104694319306248192",
"session_id": "my_session_id",
"token": "my_token"
}
}

The voice server should respond with an Opcode 2 Ready payload, which informs us of the SSRC, UDP IP/port, and supported encryption modes the voice server expects:

Example Voice Ready Payload
json
{
"op": 2,
"d": {
"ssrc": 1,
"ip": "127.0.0.1",
"port": 1234,
"modes": ["xsalsa20_poly1305", "xsalsa20_poly1305_suffix", "xsalsa20_poly1305_lite"],
"heartbeat_interval": 1
}
}

Heartbeating

In order to maintain your WebSocket connection, you need to continuously send heartbeats at the interval determined in Opcode 8 Hello:

Example Hello Payload below V3
json
{
"heartbeat_interval": 41250
}
Example Hello Payload since V3
json
{
"op": 8,
"d": {
"heartbeat_interval": 41250
}
}

This is sent at the start of the connection. Be warned that the Opcode 8 Hello structure differs by gateway version as shown in the above examples. Versions below v3 do not have an opcode or a data field denoted by d. V3 and above was updated to be structured like other payloads. Be sure to expect this different format based on your version.

After receiving Opcode 8 Hello, you should send Opcode 3 Heartbeat—which contains an integer nonce—every elapsed interval:

Example Heartbeat Payload
json
{
"op": 3,
"d": 1501184119561
}

In return, you will be sent back an Opcode 6 Heartbeat ACK that contains the previously sent nonce:

Example Heartbeat ACK Payload
json
{
"op": 6,
"d": 1501184119561
}

Establishing a Voice UDP Connection

Once we receive the properties of a UDP voice server from our Opcode 2 Ready payload, we can proceed to the final step of voice connections, which entails establishing and handshaking a UDP connection for voice data. First, we open a UDP connection to the IP and port provided in the Ready payload. If required, we can now perform an IP Discovery using this connection. Once we've fully discovered our external IP and UDP port, we can then tell the voice WebSocket what it is, and start receiving/sending data. We do this using Opcode 1 Select Protocol:

Example Select Protocol Payload
json
{
"op": 1,
"d": {
"protocol": "udp",
"data": {
"address": "127.0.0.1",
"port": 1337,
"mode": "xsalsa20_poly1305_lite"
}
}
}
Encryption Modes
ModeKeyNonce BytesGenerating Nonce
Normalxsalsa20_poly1305The nonce bytes are the RTP headerCopy the RTP header
Suffixxsalsa20_poly1305_suffixThe nonce bytes are 24 bytes appended to the payload of the RTP packetGenerate 24 random bytes
Litexsalsa20_poly1305_liteThe nonce bytes are 4 bytes appended to the payload of the RTP packetIncremental 4 bytes (32bit) int value

Finally, the voice server will respond with a Opcode 4 Session Description that includes the mode and secret_key, a 32 byte array used for encrypting and sending voice data:

Example Session Description Payload
json
{
"op": 4,
"d": {
"mode": "xsalsa20_poly1305_lite",
"secret_key": [ ...251, 100, 11...]
}
}

We can now start encrypting and sending voice data over the previously established UDP connection.

Encrypting and Sending Voice

Voice data sent to discord should be encoded with Opus, using two channels (stereo) and a sample rate of 48kHz. Voice Data is sent using a RTP Header, followed by encrypted Opus audio data. Voice encryption uses the key passed in Opcode 4 Session Description and the nonce formed with the 12 byte header appended with 12 null bytes to achieve the 24 required by xsalsa20_poly1305. Discord encrypts with the libsodium encryption library.

Voice Packet Structure
FieldTypeSize
Version + FlagsSingle byte value of 0x801 byte
Payload TypeSingle byte value of 0x781 byte
SequenceUnsigned short (big endian)2 bytes
TimestampUnsigned integer (big endian)4 bytes
SSRCUnsigned integer (big endian)4 bytes
Encrypted audioBinary datan bytes

Speaking

To notify clients that you are speaking or have stopped speaking, send an Opcode 5 Speaking payload:

The following flags can be used as a bitwise mask. For example 5 would be priority and voice.

FlagMeaningValue
MicrophoneNormal transmission of voice audio1 << 0
SoundshareTransmission of context audio for video, no speaking indicator1 << 1
PriorityPriority speaker, lowering audio of other speakers1 << 2
Example Speaking Payload
json
{
"op": 5,
"d": {
"speaking": 5,
"delay": 0,
"ssrc": 1
}
}

Voice Data Interpolation

When there's a break in the sent data, the packet transmission shouldn't simply stop. Instead, send five frames of silence (0xF8, 0xFF, 0xFE) before stopping to avoid unintended Opus interpolation with subsequent transmissions.

Resuming Voice Connection

When your client detects that its connection has been severed, it should open a new WebSocket connection. Once the new connection has been opened, your client should send an Opcode 7 Resume payload:

Example Resume Connection Payload
json
{
"op": 7,
"d": {
"server_id": "41771983423143937",
"session_id": "my_session_id",
"token": "my_token"
}
}

If successful, the Voice server will respond with an Opcode 9 Resumed to signal that your client is now resumed:

Example Resumed Payload
json
{
"op": 9,
"d": null
}

If the resume is unsuccessful—for example, due to an invalid session—the WebSocket connection will close with the appropriate close event code. You should then follow the Connecting flow to reconnect.

IP Discovery

Generally routers on the Internet mask or obfuscate UDP ports through a process called NAT. Most users who implement voice will want to utilize IP discovery to find their external IP and port which will then be used for receiving voice communications. To retrieve your external IP and port, send the following UDP packet to your voice port (all numeric are big endian):

FieldDescriptionSize
TypeValues 0x1 and 0x2 indicate request and response, respectively2 bytes
LengthMessage length excluding Type and Length fields (value 70)2 bytes
SSRCUnsigned integer4 bytes
AddressNull-terminated string in response64 bytes
PortUnsigned short2 bytes