Common mistakes, security best practices, and troubleshooting for Cloudflare TURN.
| Issue | Solution | Details |
|---|---|---|
| Credentials not working | Check TTL ≤ 48hrs | See Troubleshooting |
| Connection drops after ~48hrs | Implement credential refresh | See Connection Drops |
| Port 53 fails in browser | Filter server-side | See Port 53 |
| High packet loss | Check rate limits | See Rate Limits |
| Connection fails after maintenance | Implement ICE restart | See ICE Restart |
| Constraint | Value | Consequence if Violated |
|---|---|---|
| Max credential TTL | 48 hours (172800s) | API rejects request |
| Credential revocation delay | ~seconds | Billing stops immediately, connection drops shortly |
| IP allowlist update window | 14 days (if IPs change) | Connection fails if IPs change |
| Packet rate | 5-10k pps per allocation | Packet drops |
| Data rate | 50-100 Mbps per allocation | Packet drops |
| Unique IP rate | >5 new IPs/sec | Packet drops |
Per user (not account-wide):
Exceeding limits results in packet drops.
// ❌ BAD: API will reject
const creds = await generate({ ttl: 604800 }); // 7 days
// ✅ GOOD:
const creds = await generate({ ttl: 86400 }); // 24 hours
// ❌ BAD: IPs can change with 14-day notice
const iceServers = [{ urls: 'turn:141.101.90.1:3478' }];
// ✅ GOOD: Use DNS
const iceServers = [{ urls: 'turn:turn.cloudflare.com:3478' }];
// ❌ BAD: Blocked by Chrome/Firefox
urls: ['turn:turn.cloudflare.com:53']
// ✅ GOOD: Filter port 53
urls: urls.filter(url => !url.includes(':53'))
// ❌ BAD: Credentials expire but call continues → connection drops
const creds = await fetchCreds();
const pc = new RTCPeerConnection({ iceServers: creds });
// ✅ GOOD: Refresh before expiry
setInterval(() => refreshCredentials(pc), 3000000); // 50 min
// ❌ BAD: No recovery from TURN maintenance
pc.addEventListener('iceconnectionstatechange', () => {
console.log('State changed:', pc.iceConnectionState);
});
// ✅ GOOD: Implement ICE restart
pc.addEventListener('iceconnectionstatechange', async () => {
if (pc.iceConnectionState === 'failed') {
await refreshCredentials(pc);
pc.restartIce();
}
});
// ❌ BAD: Secret exposed to client
const secret = 'your-turn-key-secret';
const response = await fetch(`https://rtc.live.cloudflare.com/v1/turn/...`, {
headers: { 'Authorization': `Bearer ${secret}` }
});
// ✅ GOOD: Generate credentials server-side
const response = await fetch('/api/turn-credentials');
These events require ICE restart (see patterns.md):
Implement in all production apps:
pc.addEventListener('iceconnectionstatechange', async () => {
if (pc.iceConnectionState === 'failed' ||
pc.iceConnectionState === 'disconnected') {
await refreshTURNCredentials(pc);
pc.restartIce();
const offer = await pc.createOffer({ iceRestart: true });
await pc.setLocalDescription(offer);
// Send offer to peer via signaling...
}
});
Reference: RFC 8445 Section 2.4
Check:
Solution:
// Validate before using
if (ttl > 172800) {
throw new Error('TTL cannot exceed 48 hours');
}
Solutions:
Check:
Cause: Credentials expired (48hr max)
Solution:
Use ICE restart if connection fails
// Refresh credentials before expiry
const refreshInterval = ttl * 1000 - 60000; // 1 min early
setInterval(async () => {
await refreshTURNCredentials(pc);
}, refreshInterval);
Cause: Chrome/Firefox block port 53
Solution: Filter port 53 URLs server-side:
const filtered = urls.filter(url => !url.includes(':53'));
Cause: Cloudflare changed IP addresses (14-day notice)
Solution:
turn.cloudflare.com)iceTransportPolicy: 'all' to try direct first (use 'relay' only when necessary)