What is a TURN Server?
Tags: webdev, javascript, programming, beginners
Published: 2024-04-27 18:58:34

TURN stands for Traversal Using Relays Around NAT. TURN is a protocol that is used to facilitate peer to peer connectivity and communication across devices that are behind different NAT

NAT is a method that is used by routers to send communication

## Explanation of How TURN servers work

TURN servers come into play when the WebRTC is unable to establish a connection using STUN.

This happens due to firewall or NAT restrictions between devices. This is how TURN servers work

1. Session Initialization

-     A device on the internet that wishes to establish a peer to peer connection first tries to know its public IP and port number and directly establish a connection with another peer.
-     The client can know what its public IP and Port number is through the STUN server
-     If the devices fails to establish a connection directly using the IP address and Port number it got from the STUN server then it switches over to the TURN server

2. Allocation

-     The TURN server allocates a public IP address and port number for the client. This allocation acts as a relay endpoint through which all the communication will be sent and received.
-     Basically TURN server relay the data and act as a intermediary, they basically take the data from one endpoint and relay it to another endpoint.

3. Communication

-     The client uses the IP address and Port number that is assigned by ther TURN server to send and receive data
-     All the data is sent to the TURN server first when then acts as an intermediary and relays all the traffic to the destination.

4. Session Termination

-     Once the session is complete the client send a request to the TURN server to close the allocation, thus freeing up the resources

## Differences Between TURN and STUN

Both TURN and STUN are involved in facilitating NAT traversal but their approaches and use cases differ significantly

    

- **TURN:** TURN is a more comprehensive and fool proof way to establish communication, it also provides great quality of video communication with low latency and high throughput
-    You need to choose a TURN server that is nearest to your client devices, for this purpose you need to choose a TURN server service with global reach and high reliability
-     One such TURN service is Metered Global TURN server service, with TURN servers all over the world and 99.999% Uptime
-     **STUN:** STUN is a lightweight protocol that is used to discover the Public IP address and Port number of a client. The client discovers its public IP address and port number and then tries to establish a direct connection with another device across the internet
-     A clients sends a request to the STUN server which goes through the router and hits the STUN server then the STUN server returns the public IP and port number through which it got the request and sends it back. Thus the clients gets to know its own public IP and port number using which it can try to establish a connection

### Key Differences:

- **Quality:** TURN servers provide greater quality video and data transfer as compared to STUN server, this is because the connection is relayed in TURN server and thus is of better quality
-   **Resource Intensity:** TURN is a lot mrore resource intensive as compared to STUN server. This is because TURN relays the data through the server while STUN just responds back with the IP addresss and port number and all the heavy lifting has to done by the device itself.
-   **Use Cases:** STUN is used to discovering the IP address and port number of a device and that’s it.
-   **Reliability:** TURN is a much more reliable way of communicating as compared to STUN server

## Why TURN Servers are critical for Real time communication

1. Guaranteed Connectivity

With TURN servers you get guaranteed connectivity, inspite of several NAT and firewall restrictions include deep packet inspection firewalls that block most traffic you get the connectivity with TURN servers

1. Enhanced Compatibility: TURN servers are compatible with all sorts of NAT and firewall security mechanisms.

2. Security and Control: With TURN servers you ger enhanced security and control because all the traffic going through the TURN servers is end-to-end encrypted

### Real World Scenarios where TURN servers are Important

* Corporate Networks: Many businesses have stringent corporate firewall networks and complex NAT that can block communications. TURN server. facilitate real time communications such as video conferencing

* Healthcare Telecommunications: In telemedicine, real time video and data communications are needed that are secure and reliable. TURN servers help healthcare providers connect with patients.

* Online Gaming
* Remote work and learning

## Impact on performance and reliability in P2P Connections

- Performance considerations:
- Reliability Enhancements
- Load Balancing and Sociability

### Choosing the Right TURN server software
Metered TURN servers are highly valued for modern communications and networking solutions

The benefits include automatic geo location targeting, enterprise reliability, multi tenancy, comprehensive API for TURN server management. Here we discuss these features in more detail

## API for TURN server Management

Metered TURN servers provide a comprehensive API for TURN server management that enables extensive control over server management such as

-     Get TURN credential
-     Enable TURN Credential
-     Disable TURN Credential
-     Get TURN Usage
-     Get Current Usage by Date
-     Get TURN Usage By User
-     Get Current Usage for User
-     Create TURN Credential
-     Delete TURN Credential
-     Update TURN Credential Label
-     Get TURN Credentials
-     You can learn more here: https://www.metered.ca/docs/turn-rest-api/get-current-usage

There are some TURN server guides also available on the Metered Docs:

-     How to Create Expiring TURN Credentials
-     Metered TURN Server Regions

**Benefits**

- Automation and Integration: The API leads to automation of TURN server management, thus allowing for seamless integration with other systems and software. This helps you in maintaining large scale deployments and reduce administrative burdens
-     Flexibility and Control: With precision control over the servers and user credentials, businesses can repidly adapt to changes in demand or application requirements. This level of control is important in maintaining optimal performance and handling of user management activity.

### Global Geo location targeting

Global geo location targeting automatically routes the traffic to the nearest server based on the geographical location of the user.

This is most important because it is nessessory for the TURN server to be near to the user, this reduces latency and improves the quality of the webrtc call or data transfer that is being done

**Benefits**  

-   **Reduced Latency:** By routing the traffic through the nearest server, Metered TURN servers can significantly reduce latency. Metered TURN server guarantee less than 50ms latency anywhere across the world.
-     **Enhanced Performance:** Improved routing efficiency not only reduces latency but enhances the overall performance.

**Enterprise Reliability**

Metered TURN servers provide enterprise grade reliability having highly reliable TURN servers.

These are underpinned by robust infrastructure and service level agreements that guarantee 99.999% Uptime

**Benefits**

 - High Availability: The high uptime guarantee ensures that the services are always available and that there is not downtime whatsoever
   - Trust and Security: Businesses relay on consistent performance and security of turn servers. Metered TURN server provide unprecedented Uptime reliability and security so that you can focus on your business and the TURN servers just work

## Multi Tenancy

Multi Tenancy refers to the creation of multiple credentials and configurations within a single TURN server setup, enabling users to manage different customers and applications separately

### Benefits

**Resource Efficiency:** Multi tenancy maximizes resources utilization by allowing different services to share the same account without compromising on the security and performance.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tnj9p9p7tmw1d7dvdxu1.png)

[Metered TURN servers](https://www.metered.ca/stun-turn)

1. API: TURN server management with powerful API. You can do things like Add/ Remove credentials via the API, Retrieve Per User / Credentials and User metrics via the API, Enable/ Disable credentials via the API, Retrieve Usage data by date via the API.
2. Global Geo-Location targeting: Automatically directs traffic to the nearest servers, for lowest possible latency and highest quality performance. less than 50 ms latency anywhere around the world
3. Servers in 12 Regions of the world: Toronto, Miami, San Francisco, Amsterdam, London, Frankfurt, Bangalore, Singapore,Sydney, Seoul
4. Low Latency: less than 50 ms latency, anywhere across the world.
5. Cost-Effective: pay-as-you-go pricing with bandwidth and volume discounts available.
6. Easy Administration: Get usage logs, emails when accounts reach threshold limits, billing records and email and phone support.
7. Standards Compliant: Conforms to RFCs 5389, 5769, 5780, 5766, 6062, 6156, 5245, 5768, 6336, 6544, 5928 over UDP, TCP, TLS, and DTLS.
8. Multi‑Tenancy: Create multiple credentials and separate the usage by customer, or different apps. Get Usage logs, billing records and threshold alerts.
9. Enterprise Reliability: 99.999% Uptime with SLA.
10. Enterprise Scale: With no limit on concurrent traffic or total traffic. Metered TURN Servers provide Enterprise Scalability
11. 5 GB/mo Free: Get 5 GB every month free TURN server usage with the Free Plan
12. Runs on port 80 and 443
13. Support TURNS + SSL to allow connections through deep packet inspection firewalls.
14. Support STUN
15. Supports both TCP and UDP