> For the complete documentation index, see [llms.txt](https://zyno-studio.gitbook.io/discord-audio/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zyno-studio.gitbook.io/discord-audio/classes/connection.md).

# Connection

### Creating a connection

A connection can easily be created by using the Connection class. The Connection class works different than the [Player](broken://pages/-MjtGAvCw91E8hzJhUwy) and the [AudioManager](broken://pages/-MjtPCb5l4WrXIBrN0R4) class. The Connection class will make the bot join the voice channel immediately when the Connection class is being called. The Player and AudioManager classes make you join a voice channel when you start playing a song.

The Connection class requires the `channel` parameter and has an optional `options` parameter. The `channel` parameter is the voice channel that the bot needs to join. The `options` parameter has the following options:

* selfDeaf: If the bot should deaf itself. **(default is `true`)**
* selfMute: If the bot should mute itself. **(default is `false`)**

```javascript
const {Connection} = require('discordaudio');
const discord = require('discord.js');

const client = new discord.Client({intents: [discord.Intents.FLAGS.GUILDS]});

const channel = client.channels.cache.get('0123456789');

const connection = new Connection(channel, {
    selfDeaf: true,
    selfMute: false
});
```

### Play a stream

You can play a stream by using the play function and returns a Promise. The function requires the `stream` parameter and has an optional `options` parameter. The stream parameter is the `stream` that you want to play. The `options` parameter has the following options:

* noListeners: If there are no members listening in the voice channel.
  * play: Continues playing the stream. **(default and recommended)**
  * pause: Pauses the stream until someone joins again.
  * stop: Stops the player and leaves the voice channel.
* volume: The volume of the stream. Max 1, min 0.
* audiotype: The audio type of the stream.
  * 'arbitrary': For mp3 audio **(default)**
  * 'ogg/opus': For ogg audio
  * 'webm/opus': For webm audio
  * 'opus': For opus audio
  * 'raw': For raw audio

```javascript
const {Connection} = require('discordaudio');
const discord = require('discord.js');

const client = new discord.Client({intents: [discord.Intents.FLAGS.GUILDS]});

const channel = client.channels.cache.get('0123456789');

const connection = new Connection(channel, {
    selfDeaf: true,
    selfMute: false
});

connection.play(`https://somecoolsite.com/somecoolstream.mp3`, {
    noListeners: 'play',
    volume: 1,
    audiotype: 'arbitrary'
}).then(stream => console.log(`Playing ${stream}`)).catch(console.log);
```

### Subscribe to a broadcast

You can subscribe to a [Broadcast](broken://pages/-MjtPNfFXNxn9mww90z5) by using the subscribe function. A Broadcast makes you able to create one source and play it in multiple guilds. The subscribe function only requires the `broadcast` parameter which is the Broadcast itself which you can create with the Broadcast class. The function returns a Promise which will be fullfilled if the connection successfully subscribed to the broadcast.

```javascript
const {Connection, Broadcast} = require('discordaudio');
const discord = require('discord.js');

const client = new discord.Client({intents: [discord.Intents.FLAGS.GUILDS]});

const channel = client.channels.cache.get('0123456789');

const connection = new Connection(channel, {
    selfDeaf: true,
    selfMute: false
});

const broadcast = new Broadcast(`https://somecoolsite.com/somecoolstream.mp3`, {
    noListeners: 'play',
    volume: 1,
    audiotype: 'arbitrary'
});

connection.subscribe(broadcast).then(() => console.log(`Playing the broadcast!`).catch(console.log);
```

### Unsubscribe to a broadcast

You can easily unsubscribe by using the unsubscribe function.

```javascript
const {Connection, Broadcast} = require('discordaudio');
const discord = require('discord.js');

const client = new discord.Client({intents: [discord.Intents.FLAGS.GUILDS]});

const channel = client.channels.cache.get('0123456789');

const connection = new Connection(channel, {
    selfDeaf: true,
    selfMute: false
});
...
connection.unsubcribe();
```

### Changing the volume

You can change the volume by using the volume function. It requires the `volume` parameter which will be the volume of the stream that's playing. The volume must be at least 0 and may not be higher than 1.

```javascript
const {Connection} = require('discordaudio');
const discord = require('discord.js');

const client = new discord.Client({intents: [discord.Intents.FLAGS.GUILDS]});

const channel = client.channels.cache.get('0123456789');

const connection = new Connection(channel, {
    selfDeaf: true,
    selfMute: false
});
...
connection.volume(1);
```

{% hint style="danger" %}
**This function does not work for broadcasts!** Broadcasts have their [own volume](broken://pages/-MjtPNfFXNxn9mww90z5#changing-the-broadcast-volume) function.
{% endhint %}

### Disconnect from a connection

You can disconnect from a connection by using the disconnect function.

```javascript
const {Connection} = require('discordaudio');
const discord = require('discord.js');

const client = new discord.Client({intents: [discord.Intents.FLAGS.GUILDS]});

const channel = client.channels.cache.get('0123456789');

const connection = new Connection(channel, {
    selfDeaf: true,
    selfMute: false
});
...
connection.disconnect();
```

{% hint style="info" %}
If the connection is subscribed to a broadcast the disconnect function will automatically unsubscribe from the broadcast.
{% endhint %}

### Destroy a connection

You can destroy a connection by using the destroy function.

```javascript
const {Connection} = require('discordaudio');
const discord = require('discord.js');

const client = new discord.Client({intents: [discord.Intents.FLAGS.GUILDS]});

const channel = client.channels.cache.get('0123456789');

const connection = new Connection(channel, {
    selfDeaf: true,
    selfMute: false
});
...
connection.destroy();
```

{% hint style="info" %}
If the connection is subscribed to a broadcast, the destroy function will automatically unsubscribe from the broadcast.
{% endhint %}

### Pause a stream

You can pause a stream by using the pause function.

```javascript
const {Connection} = require('discordaudio');
const discord = require('discord.js');

const client = new discord.Client({intents: [discord.Intents.FLAGS.GUILDS]});

const channel = client.channels.cache.get('0123456789');

const connection = new Connection(channel, {
    selfDeaf: true,
    selfMute: false
});
...
connection.pause();
```

{% hint style="danger" %}
**This function does not work for broadcasts!** Broadcasts have their [own pause](broken://pages/-MjtPNfFXNxn9mww90z5#pausing-a-broadcast) function.
{% endhint %}

### Resuming a stream

You can resume a stream by using the resume function.

```javascript
const {Connection} = require('discordaudio');
const discord = require('discord.js');

const client = new discord.Client({intents: [discord.Intents.FLAGS.GUILDS]});

const channel = client.channels.cache.get('0123456789');

const connection = new Connection(channel, {
    selfDeaf: true,
    selfMute: false
});
...
connection.resume();
```

{% hint style="danger" %}
**This function does not work for broadcasts!** Broadcasts have their [own resume](broken://pages/-MjtPNfFXNxn9mww90z5#resuming-a-broadcast) function.
{% endhint %}

### Event listeners

The Connection class also has 6 events:

* play: When the stream of the connection starts playing.
  * Returns the stream that plays (not for broadcasts)
* end: When the stream of the connection ended.
  * Returns the stream that ended (not for broadcasts)
* disconnect: When the bot got disconnected from the connection.
  * Returns the channel id
* destroy: When the connection got destroyed.
  * Returns the channel id
* subscribe: When the connection got subscribed to a broadcast.
* unsubscribe: When the connection got unsubscribed from a broadcast.

```javascript
...
connection.on('play', stream => {
    if(stream) console.log(`Started playing ${stream}`);
    else console.log(`Started playing a broadcast`);
});
connection.on('end', stream => {
    if(stream) console.log(`The stream ${stream} ended`);
    else console.log(`The broadcast ended`);
});

connection.on('disconnect', channelid => console.log(`Disconnected from the channel with the id ${channelid}`));

connection.on('destroy', channelid => console.log(`Destroyed the connection with the channel witht the id ${channelid}`));

connection.on('subscribe', () => console.log(`Subscribed to a broadcast`));

connection.on('unsubscribe', () => console.log(`Unsubscribed to a broadcast`));
```
