# Voice and TURN

> How calls reach your server, the three media ports, the TURN relay over UDP, and what doesn’t work yet for people behind firewalls that only allow HTTPS.

## How a call connects

Voice, video and screen sharing go through **LiveKit**, which runs in the kit as the `livekit`
service. Calls are end-to-end encrypted: LiveKit forwards the media but can’t read it.

- The **signalling** (who’s in the call, the keys to join) goes over HTTPS to
  `KLANG_VOICE_DOMAIN`, through Caddy. That hostname must point straight at the server: not
  through Cloudflare’s proxy, and not through a Cloudflare Tunnel, which only carry HTTP. The TURN
  relay uses the same hostname over UDP.
- The **media** go straight to LiveKit’s own ports, not through Caddy. A browser tries them in
  this order:

| Port                       | Protocol | When it’s used                                |
| -------------------------- | -------- | --------------------------------------------- |
| 7882 (`LIVEKIT_UDP_PORT`)  | UDP      | Normally: every call on one port              |
| 3478 (`LIVEKIT_TURN_PORT`) | UDP      | The TURN relay, for people behind strict NATs |
| 7881 (`LIVEKIT_TCP_PORT`)  | TCP      | When UDP is blocked                           |

The API hands out a short-lived token for one room at a time; LiveKit’s key and secret never
leave the server.

## The TURN relay

The kit turns on LiveKit’s built-in TURN server over UDP, on the voice hostname. It isn’t an open
relay: only people with a token for a call get its credentials, and LiveKit doesn’t relay to
private addresses or over TCP.

## Known limitation: no TURN over TLS

Some networks only let HTTPS out (port 443 over TCP): many offices, schools, hotels and public
Wi-Fi. There, UDP and port 7881 are blocked, and **calls don’t connect**: chat and everything
else work, but voice, video and screen sharing don’t.

The usual fix is TURN over TLS on port 443, which looks like HTTPS to the firewall. The kit
doesn’t set it up, because on one machine with one IP address it can’t be done with the kit’s
pieces:

- **LiveKit always tells browsers to use TURN over TLS on port 443** of its TURN name
  (`turns:<hostname>:443`). Another port (5349, say) is never announced.
- **Caddy already holds port 443** for the app, the panel, files and voice signalling. Sharing
  it would take something in front that sends each TLS connection by its hostname (SNI) to Caddy or
  to LiveKit, which the official Caddy image can’t do (it needs a third-party plugin and a build
  of Caddy of your own).
- LiveKit reads its TURN certificate once, when it starts: when Caddy renews it, LiveKit would
  have to restart, which ends every call in progress.

If many of your people are behind such networks, the way to add it is a **second public IP** (or
a second machine) for a TURN hostname of its own, with a TURN server listening on 443 there: for
example coturn, which LiveKit can hand out to browsers (`rtc.turn_servers` in its
configuration). It’s outside the kit.

## Checking it works

1. Two people join a voice channel, from different networks if you can (a phone on mobile data
   is a good second one).
2. If they see each other in the channel but hear nothing, the media ports aren’t reachable:
   check the firewall (and the router’s port forwarding at home), and the address LiveKit
   announces:

```bash
docker compose logs livekit | grep -i -E "nodeip|nat1to1|external"
```

The `nodeIP` of `starting LiveKit server` must be the public IP. If it isn’t, set `LIVEKIT_USE_EXTERNAL_IP=false` and
`LIVEKIT_NODE_IP=<the public IP>` in `.env` and run `docker compose up -d`.

More in [Troubleshooting](https://docs.klang.gg/self-hosting/troubleshooting/#voice-doesnt-connect).
