Setting Up a TURN-Only WebRTC Connection Between Two Browsers
WebRTC (Web Real-Time Communication) is a powerful technology that enables browsers and mobile apps to communicate with each other directly using peer-to-peer connections. This makes it possible to share video, audio, and data streams without relying on centralized servers for the actual media transfer.
However, in real-world scenarios, things aren’t always so straightforward. Firewalls, NATs (Network Address Translators), and corporate networks can block or restrict direct peer-to-peer connections. That’s where TURN servers come in — they act as a relay when a direct connection isn’t possible.
In this article, you’ll learn how to set up a TURN-only WebRTC connection between two browsers using the turnix-js library and the free turnix.io service. The project demonstrates a simple one-way video stream from a sender to a receiver, routed entirely through a TURN server.
Why TURN Servers Are Needed
When two browsers attempt to establish a WebRTC connection, they exchange ICE candidates — possible network paths that can be used for communication. In most cases, direct connections (using STUN servers to discover public IPs) work fine.
But in stricter network environments, these direct paths fail. Without a fallback, the call simply won’t connect. A TURN server solves this by relaying all traffic between peers, ensuring reliability even when both participants are behind restrictive firewalls.
Forcing WebRTC to use TURN only is also useful in testing environments, demos, or situations where privacy and predictable connectivity are important.
What You’ll Build
You’ll set up a small demo project that includes:
- A signaling server (built with Node.js, Express, and WebSocket)
- A Sender page that captures the user’s camera and streams it
- A Receiver page that plays back the stream
- TURN-only enforced using
iceTransportPolicy: "relay"
By the end, you’ll be able to open two browser tabs (or two devices) and see a video stream pass through a TURN server.
Prerequisites
Before you begin, make sure you have:
- A free account at turnix.io
- A Bearer API token from your TURNIX dashboard
- Node.js installed on your computer
Step 1 — Clone the Demo Project
Clone the repository and install its dependencies:
git clone https://github.com/turnixio/turnix-js-demo.git
cd turnix-js-demo
npm install
This gives you a working project with everything already set up.
Step 2 — Configure the API Token
Open the server.js
file and set your TURNIX API token:
const BEARER_TOKEN = 'your-turnix-api-token-here';
This token allows the signaling server to request temporary ICE credentials from TURNIX. These credentials will then be shared with the Sender so it can establish a TURN-only connection.
Step 3 — Run the Signaling Server
Start the server:
node server.js
Two things happen when you do this:
- An Express server starts on http://localhost:3000, serving the demo pages
- A WebSocket signaling service starts at
ws://localhost:3000?room=room1
, handling offer/answer exchange between peers
Step 4 — Test the Connection in the Browser
Open the following URLs in two separate tabs (or on two different devices):
First, open the Receiver page:
http://localhost:3000/receiver.html
Then open the Sender page:
http://localhost:3000/sender.html
Here’s what happens:
- The Sender requests fresh TURN credentials from the signaling server.
- The Sender captures the user’s camera and sends the stream.
- The Receiver connects using the credentials and plays the video.
Because iceTransportPolicy
is set to "relay"
, all traffic is forced through the TURN server. No direct peer-to-peer connection is attempted.
How the Demo Enforces TURN-Only Mode
The project has a couple of safeguards to make sure traffic always goes through the relay:
- ICE server filtering: only
turn:
andturns:
URLs are passed to the peer connection. - Policy enforcement:
iceTransportPolicy: "relay"
is used when creating the RTCPeerConnection object.
This combination ensures that if a TURN server is unavailable, the connection will fail — guaranteeing you’re always testing TURN-only mode.
Browser Support
The demo has been tested with:
- Google Chrome — works without modification
- Mozilla Firefox — works, but you may need muted video for autoplay policies
Other browsers that support WebRTC should also work, but behavior can vary slightly.
Where to Go From Here
With this demo, you’ve set up a working TURN-only WebRTC connection. This foundation can be expanded in several ways:
- Add two-way video/audio instead of one-way streaming
- Implement chat messaging alongside video
- Deploy the signaling server to a cloud host for remote access
- Integrate with your own TURN server infrastructure
Resources
Conclusion
WebRTC is an essential technology for real-time applications, but NATs and firewalls can make direct connections unreliable. By enforcing TURN-only mode, you can guarantee that two browsers can always connect, regardless of network restrictions.
This demo provides a simple, working foundation to understand how TURN servers fit into WebRTC and gives you the tools to build more advanced real-time applications on top.