Home/Guides/TURN server for WebRTC games

guide · webrtc

TURN server for WebRTC games

WebRTC connects most players directly. For the rest — symmetric NATs, some corporate and mobile networks — you need TURN. Here is when, which provider, and the exact configuration.

Free sandbox · $19.99/mo to go live · cancel anytime

when

Do you need TURN?

Public STUN is enough for most home connections. Behind a symmetric NAT the direct path never opens, and without TURN those players fall back to the server relay: the game still works, but at a higher cost and latency. TURN brings them back to direct peer-to-peer.

Measure before you buy. The dashboard's Usage tab shows the share of ticks actually held peer-to-peer. If direct connections often fail, add TURN. In code, room.on('transport', mode => …) reports p2p, mixed or relay.

configuration

Configure it in one object

Two forms in the game configuration. Prefer the first: the secret never leaves the server, and each client receives an expiring credential following the coturn shared-secret convention. Per the reference, this is understood as-is by Cloudflare, Twilio, Xirsys and most managed TURN services.

// A) shared secret + expiring credentials (recommended)
{ "turn": { "urls": ["turn:turn.example.com:3478"], "secret": "...", "ttl": 3600 } }

// B) static ICE servers (if your provider hands out fixed credentials)
{ "ice": [ { "urls": "turn:turn.example.com:3478", "username": "...", "credential": "..." } ] }

The SDKs renew the list on their own; GET /v1/rt/ice returns it with its ttl.

choices

Self-host or buy?

OptionProsCons
coturn (self-host)Cheapest per GB, full controlYou run it: TLS, ports, monitoring, regions
Managed TURN (Xirsys, Twilio, Cloudflare and similar)Global footprint, no opsMetered bandwidth; check current pricing
Server relay only (default)Zero setupHigher cost and latency for the players who need it

Start with the default relay, watch the Usage tab, and add TURN only if a meaningful share of players is relayed. Background reading: WebRTC multiplayer without a server.

Questions
What is a TURN server and why do games need one?

A TURN server relays traffic between peers when a direct WebRTC connection is impossible, typically behind symmetric NATs. Without it, those players cannot connect peer-to-peer.

Does Multiplayer API work with Xirsys?

Yes. Declare the TURN URLs and either a shared secret with a TTL or static ICE credentials in the game configuration; clients receive them from the ICE endpoint.

What happens if I do not configure TURN?

Players who cannot connect directly are served through the server relay. The game continues; only that pair of peers is affected.

Make your game online today

Free sandbox keys in one call, no account and no card. $19.99/month when you ship, for every game you make.

Free sandbox · $19.99/mo to go live · cancel anytime