Free How much of your firm runs on you? Take the 2-minute Key Man Score.
Lesson reading
0%

Lesson 9 / Unit 2: Self-Hosted Stack

TURN Relay

Some callers can only reach you on TCP 443. Without TURN their sessions never start, and no metric notices. The TURN config, the certificates, and five firewall rules.

A caller on hotel Wi-Fi opens your app. UDP is blocked. So is every TCP port except 443.

Without a working fallback that session may fail. Client-side connection telemetry should record it even when no room connection completes. Corporate firewalls and certain mobile carriers do the same. TURN is that relay path.

ICE Selection

ICE checks candidate pairs and selects a working path according to priority and policy. It is not a strict three-step waterfall. A relay candidate provides a path when direct connectivity fails or when policy requires relaying.

That ordering is ICE, specified in RFC 8445, and the relay itself is TURN, RFC 8656: the client allocates an address on the TURN server, and media flows through it. TURN/TLS on port 443 can traverse networks that allow that destination and protocol. It is not HTTPS and deep inspection or an explicit proxy can still block it.

TURN Configuration

LiveKit embeds a TURN server, so this is configuration, not a new deployment. Enable it, give it a domain, and put a TLS certificate on it, using the keys below from LiveKit’s deployment guide and config sample:

turn:
enabled: true
# must match the TLS certificate
domain: turn.example.com
# advertised to clients: set 443 when no load balancer sits in front
tls_port: 443
# optional TURN/UDP (default 3478)
udp_port: 3478
cert_file: /etc/livekit/turn.crt
key_file: /etc/livekit/turn.key

Three details catch people:

  • tls_port defaults to 5349, and that is the port clients are told to use. Leave it there without a load balancer and the restrictive networks you enabled TURN for still can’t reach it.
  • Give TURN a hostname covered by its trusted certificate. A certificate can cover multiple names; the listener and routing still need to be configured correctly.
  • If the same IP already terminates signalling TLS on 443, two listeners can’t share it. Give TURN its own address, or put a layer 4 load balancer in front and set external_tls: true so LiveKit expects unencrypted traffic on tls_port while still advertising it as TURN/TLS.

Some networks now pass UDP on 443 because of QUIC, so if that fits your callers, udp_port: 443 enables TURN/UDP there.

Certificate Renewal

The signalling endpoint and the TURN endpoint both need valid TLS from a trusted authority. Self-signed certificates do not work. Terminate at the server with certificates you renew automatically, and make sure the renewal job reaches both.

An expired certificate produces the worst class of outage. Web clients fail. Phone calls through the SIP path keep working. Put together, those two facts point everywhere except the certificate, which is why the expiry date of both certificates belongs on a dashboard next to call volume.

Firewall Rules

Every rule pairs with one config value, and the pair must cover the configured ports. Here is the full set for a stack with the SFU, TURN and the SIP service, using the defaults from earlier lessons:

RulePortProtocolMust match
Signalling443TCPTLS terminator in front of port: 7880
ICE TCP fallback7881TCPrtc.tcp_port
TURN over TLS443TCPturn.tls_port
SFU media50000-60000UDPrtc.port_range_start, rtc.port_range_end
SIP media10000-20000UDPrtp_port in the SIP service config

Two 443 rows means two addresses (or a load balancer), per the section above. Add 3478/UDP if you enable TURN/UDP, and 5060 for SIP signalling from the SIP lesson; LiveKit’s ports and firewall reference lists every port with its config key.

Each rule on the left has a twin in config on the right, and the SFU and SIP ranges are separate. We check all five pairs together, because a mismatch in any one drops traffic without an error.

We close unit 2 with this table because several of the failures in unit 5, starting with port exhaustion, trace back to a mismatch between what a config promised and what a firewall permitted.

Relay Testing

Force TURN in a test client and confirm audio flows; with the LiveKit JS SDK, that means restricting ICE to relay candidates when you connect:

await room.connect(url, token, {
rtcConfig: { iceTransportPolicy: 'relay' },
});

If audio flows both ways, the relay works end to end: domain, certificate, port and firewall. If it doesn’t, you’ve found the failure before a caller did. The relay is the path you never use on your own network and your users use daily, which makes it the path that breaks without anyone on the team noticing.

Relay Traffic

Relay traffic crosses the TURN node twice, in and out, so size for both ingress and egress; billing depends on the provider and network path. Relayed calls also come from your worst-network users, and once volume grows, the TURN node’s egress and its placement both matter.

Instrument what fraction of connections relay. The selected ICE candidate pair in WebRTC stats reports a candidate type of relay when a session goes through TURN. A rising relay share is an early signal of a network-quality shift in your caller population. Investigate the client networks and selected candidate pairs when that share changes.

Next: the agent worker lifecycle, where the stack you just built starts running your code, and where a job can end without ever saying so.

Common questions

Do I need a separate TURN server for self-hosted LiveKit?

No. LiveKit embeds a TURN server. You enable it in livekit.yaml, give it a domain that matches a TLS certificate, and open its port on the firewall.

Which port should LiveKit TURN over TLS use?

Port 443 when no load balancer sits in front of it, because turn.tls_port is the port advertised to clients and 443 is what restrictive networks let through. With a layer 4 load balancer terminating TLS, set external_tls: true and let the balancer own 443.

How do I test that TURN works?

Force a test client to use only relay candidates, for example by passing rtcConfig with iceTransportPolicy set to relay when connecting with the LiveKit JS SDK, and confirm audio flows both ways.

We do this work for teams shipping voice agents: Self-Hosted Voice Agent .

Edit this lesson on GitHub ↗

Discussion

Questions, corrections and war stories welcome. Sign in with GitHub to comment.

Free guide

Be The 10x Entrepreneur

Get the firm out of your head.

  • 8 chapters
  • 7 worksheets
  • 30 day plan