Despliegue y protección de su propio servidor TURN
Aprenda a alojar su propio servidor TURN con coturn, configurar credenciales de forma segura mediante tokens con duración limitada y decidir entre un servicio TURN propio o gestionado.
Despliegue y protección de su propio servidor TURN es una lección gratuita de Real-Time Streaming Systems (WebRTC + Live Data) en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Real-Time Streaming Systems (WebRTC + Live Data), y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Real-Time Streaming Systems (WebRTC + Live Data) incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
From Theory to Operation
You understand NAT challenges, STUN, and what TURN does. Now you will actually run a TURN server, secure it, and connect WebRTC to it. The most common open-source choice is coturn.
Why Self-Host TURN
Public STUN is free, but TURN relays media and consumes bandwidth, so it is rarely free. Running your own TURN server gives you control over capacity, cost, and privacy.
Installing coturn
On a Linux server you install coturn from the package manager. It runs as a background service.
sudo apt-get update
sudo apt-get install -y coturn
sudo systemctl enable coturnBasic Configuration
coturn reads /etc/turnserver.conf. A minimal config sets the realm and listening ports.
listening-port=3478
tls-listening-port=5349
realm=turn.example.com
fingerprintThe Credential Problem
TURN requires authentication or anyone could relay traffic through your server at your expense. Hardcoding a static username and password is risky because clients can leak them.
Time-Limited Credentials
The secure approach is the REST/ephemeral credential mechanism. Your server generates short-lived usernames and passwords derived from a shared secret, so leaked credentials expire quickly.
use-auth-secret
static-auth-secret=your_long_shared_secretGenerating a Credential
Your backend creates a username as an expiry timestamp and signs it with HMAC-SHA1 using the shared secret. The signature becomes the password.
const crypto = require('crypto');
function turnCredential(secret, ttl) {
const username = String(Math.floor(Date.now() / 1000) + ttl);
const hmac = crypto.createHmac('sha1', secret);
hmac.update(username);
const password = hmac.digest('base64');
return { username, password };
}Wiring It Into WebRTC
Pass the TURN URL and ephemeral credentials into the peer connection's ICE server list. WebRTC uses them when direct paths fail.
const pc = new RTCPeerConnection({
iceServers: [{
urls: 'turn:turn.example.com:3478',
username: cred.username,
credential: cred.password
}]
});Use TLS and TCP Fallback
Some restrictive networks block UDP entirely. Offer turns: over TCP on port 443 so media can tunnel through firewalls that only allow HTTPS traffic.
// add a TLS/TCP TURN entry alongside the UDP one
urls: 'turns:turn.example.com:443?transport=tcp'Self-Host vs Managed
Self-hosting coturn is cheaper at scale but means you handle uptime, bandwidth, and security. Managed TURN providers cost more per GB but remove operational burden. Pick based on your team and traffic.
Operating Responsibly
Monitor bandwidth, rotate the shared secret periodically, restrict relay to authenticated users, and place the server geographically near your users to minimize latency. A well-run TURN server is the safety net that makes calls connect everywhere.
Quick Check
Test your understanding of TURN deployment.
Recap
You learned to deploy and secure TURN:
- Install and configure coturn with a realm and ports
- Use
use-auth-secretwith HMAC-based ephemeral credentials - Wire credentials into the ICE server list
- Offer TLS/TCP on 443 for restrictive networks
- Weigh self-hosting against managed services
A secure TURN server ensures calls connect even behind tough NATs.
Preguntas frecuentes
¿La lección «Despliegue y protección de su propio servidor TURN» es gratis?
Sí — el texto completo de «Despliegue y protección de su propio servidor TURN» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Real-Time Streaming Systems (WebRTC + Live Data), actualiza a CoddyKit PRO. El curso de Real-Time Streaming Systems (WebRTC + Live Data) incluye 4 lecciones en total.
¿Qué aprenderé en «Despliegue y protección de su propio servidor TURN»?
Aprenda a alojar su propio servidor TURN con coturn, configurar credenciales de forma segura mediante tokens con duración limitada y decidir entre un servicio TURN propio o gestionado. Practicas Real-Time Streaming Systems (WebRTC + Live Data) con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Real-Time Streaming Systems (WebRTC + Live Data)?
No se requiere experiencia previa. Real-Time Streaming Systems (WebRTC + Live Data) en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.
¿Cuánto tiempo toma la lección «Despliegue y protección de su propio servidor TURN»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Real-Time Streaming Systems (WebRTC + Live Data)?
Sí. Cada lección de Real-Time Streaming Systems (WebRTC + Live Data) incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Desafíos de NAT y los cortafuegos
- Funcionamiento de los servidores STUN
- Servidor TURN para conexiones retransmitidas
- Despliegue y protección de su propio servidor TURN