> For the complete documentation index, see [llms.txt](https://docs.edgegap.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.edgegap.com/docs/tools-and-integrations/eos-voice-chat.md).

# EOS Voice Chat

Add Epic Online Services voice chat to matched players in an Unreal Engine game, with the Edgegap dedicated server as the trusted voice server.

This guide shows how our [Unreal Engine sample project](https://github.com/edgegap/game-sample-unreal-engine-topdown) adds in-game voice chat with [EOS Voice](https://dev.epicgames.com/docs/epic-online-services/multiplayer/voice-and-rtc-interface/voice-interface/voice-overview) from Epic Online Services. Players matched by the [Edgegap Matchmaker](https://docs.edgegap.com/learn/matchmaking.md) talk with their teammates in a voice room, and your dedicated server on Edgegap decides who joins which room.

Voice chat is **disabled by default** in the sample. Until you enable it, the game works as described in [Top-Down Sample](https://docs.edgegap.com/docs/sample-projects/unreal-engine/top-down-sample.md).

{% hint style="success" %}
**We recommend starting with** [Unreal Engine](https://docs.edgegap.com/unreal-engine.md) **guide to build your game servers before proceeding.**
{% endhint %}

## 🎙️ How It Works

Your dedicated server is the EOS [trusted server](https://dev.epicgames.com/docs/epic-online-services/multiplayer/voice-and-rtc-interface/voice-interface/voice-overview#voice-with-a-trusted-server-application). Game clients never hold credentials that can create voice rooms.

### 1. Players Log into EOS

Game client logs the player into EOS with a device ID, so players don't need an Epic account. It then sends the server its Product User ID together with an EOS ID token.

### 2. Server Checks the Player

Server verifies the ID token against Epic's public keys, so a player can't claim someone else's ID. It also checks that the token was issued for your EOS deployment.

### 3. Server Picks the Room

Room names come from the match:

* base room is named after the matchmaker's match ID (`MM_MATCH_ID`), or after the deployment ID for a server you deployed by hand,
* with team rooms enabled (default, `bTeamRooms=True`), each team gets its own room, named after the match and team.

Server finds each player's team from [matchmaker tickets injected as environment variables](https://docs.edgegap.com/learn/matchmaking/matchmaker-in-depth.md#injected-variables) - it matches the IP the player connects from to the IP on each ticket, and reads the ticket's team. **It never trusts a team sent by the client.** Players without a matching ticket (for example players who connected by IP) stay in the match room. Set `bTeamRooms=False` to put the whole match in one room.

### 4. Server Hands Out a Room Token

Server asks the [EOS Voice Web API](https://dev.epicgames.com/docs/web-api-ref/voice-web-api) for a room token for that player, passing the player's IP so EOS uses a voice server near them. It sends the token to that player only, and the client joins the room with the engine's EOSVoiceChat plugin.

### 5. Server Removes Players Who Leave

When a player leaves the server, the server removes them from their voice room, which also revokes their token. Game code can also mute a player for everyone with `UEdgegapVoiceServerSubsystem::SetHardMuted`.

{% hint style="info" %}
Your game mode and player controller don't change. When a player logs in (`PostLogin`), the server adds a small replicated component, `UEdgegapVoiceComponent`, to their player controller. It replicates to its owning client only and carries the `ServerRegisterVoice` and `ClientJoinVoiceRoom` RPCs.
{% endhint %}

Server calls the EOS Web API over HTTPS rather than the EOS SDK, because the EOS SDK doesn't support voice on Linux. The client side builds for Windows and Mac, where the EOSVoiceChat plugin exists.

| File                          | What it does                                                            |
| ----------------------------- | ----------------------------------------------------------------------- |
| `EdgegapVoiceSettings.h`      | The on/off switch and options, from `DefaultGame.ini`                   |
| `EdgegapVoiceClientSubsystem` | EOS device ID login, ID token, joining and leaving rooms                |
| `EdgegapVoiceComponent`       | Added to every player by the server, carries the voice RPCs             |
| `EdgegapVoiceServerSubsystem` | ID token check, room choice, EOS Voice Web API calls, removal on logout |

## ⚙️ Setup

### 1. Create EOS Clients

☑️ Open your product in [Epic Developer Portal](https://dev.epicgames.com/portal), then **Product Settings**.

☑️ Note your Product ID, Sandbox ID and Deployment ID on tab **SDK Download & Credentials**.

☑️ Add two policies on tab **Clients**, under **Client policies**:

* **Game client** - type `GameClient`, **User required** checked, no Voice permission needed,
* **Trusted server** - **Create custom policy**, **User required** unchecked, with Voice actions `createRoomToken`, `kick` and `mute`.

☑️ Add one client for each policy, and note each Client ID and Client Secret.

{% hint style="danger" %}
Your trusted server's client can create a token for any room in your deployment. **Never put it in a game build.**
{% endhint %}

✅ You may now proceed to the next step.

### 2. Configure the Game Client

☑️ Fill in `[EOSVoiceChat]` in `MultiplayerGame/Config/UserEngine.ini` with your product, sandbox and deployment IDs, and your **game client's** ID and secret. This file is git-ignored and overrides `DefaultEngine.ini`, so your credentials stay out of version control.

☑️ Enable voice in `MultiplayerGame/Config/UserGame.ini` (also git-ignored):

{% code collapsedlinecount="10" %}

```ini
[/Script/MultiplayerGame.EdgegapVoiceSettings]
bEnabled=True
```

{% endcode %}

✅ You may now proceed to the next step.

### 3. Configure the Server

☑️ Add these environment variables to your [app version](https://app.edgegap.com/application-management/applications/list), and mark the secret as hidden:

| Variable                   | Value                                                                                                |
| -------------------------- | ---------------------------------------------------------------------------------------------------- |
| `EOS_VOICE_CLIENT_ID`      | Trusted server Client ID                                                                             |
| `EOS_VOICE_CLIENT_SECRET`  | Trusted server Client Secret                                                                         |
| `EOS_VOICE_DEPLOYMENT_ID`  | EOS Deployment ID (optional if it's already in `DefaultEngine.ini`)                                  |
| `EOS_VOICE_GAME_CLIENT_ID` | Game client's Client ID, optional: the server then only accepts ID tokens issued to your game client |

Voice is enabled on your server once these are set, so you don't need to rebuild your server image. For a server running locally, put `ServerClientId` and `ServerClientSecret` in `EdgegapVoiceSettings` section of `UserGame.ini` instead.

✅ You may now proceed to the next step.

### 4. Play

☑️ Package your server and client again, deploy, and play. With the matchmaker enabled, teammates share a room.

☑️ Type `VoiceMute` in console to mute or unmute your microphone.

🙌 Congratulations, your players can now talk to their teammates!

## 🧪 Testing with Two Clients on One PC

A device ID belongs to the Windows user, so two clients on the same PC would log in as the same EOS player. Add `-EOSVoiceNewDeviceId` to the second client's command line to give it a new identity.

{% hint style="warning" %}
`-EOSVoiceNewDeviceId` creates a new EOS user on every launch, **use it for testing only**. Start your second client only after the first logs `Voice: logged into EOS` - replacing the device ID while the first client is still logging in gives both clients the same identity.
{% endhint %}

To check that audio flows without talking into a microphone, start a client with `-ExecCmds="EOSVoiceChat.FakeAudioInputEnabled 1"` (Development builds). It sends a test signal, and the other client logs `Voice: <id> started talking in room <room>`.

With the matchmaker, both clients must queue within 10 seconds of each other, otherwise the `casual` profile starts a solo match. Clients start at different speeds, so launch both with the same `-FindMatchAt=<unix time>` (for example two minutes ahead) instead of `-FindMatch` - each queues at that moment.

{% hint style="info" %}
Log lines to look for (`LogEdgegapVoice`):

* server - `Voice: trusted voice server ready`, then `Voice: <id> gets room <room>` for each player, and `Voice: removed <id>` when a player leaves,
* client - `Voice: logged into EOS as <id>`, `Voice: joined room <room>`, and `Voice: <id> started talking`.
  {% endhint %}
