This article was originally published on the Metered blog: WebRTC Data Channels: A guide.
In this article we are going to learn about the WebRTC data channels. Here is what we are going to learn
Understanding WebRTC Data Channels
What is WebRTC?
WebRTC Data Channels Explained
Functionality and features of WebRTC Data Channels
Use-cases of WebRTC Data Channels
Advantages and Benefits of WebRTC Data Channels
How does WebRTC Data Channels Work
Peer-to-Peer Communication protocols
Data transmission Protocols
Role of TURN Servers in WebRTC Data Channels
Metered TURN servers
Understanding WebRTC Data Channels
What is WebRTC
WebRTC also known as Web Real Time communication is an open source project that enables real time communication between devices on the web
WebRTC provides simple JavaScript APIs that developers can use to develop peer-to-peer audio, video and data transfer applications
WebRTC was released by google as an open source project in 2011, the primary goal was to enable RTC (that is real time communication) apps to be developed for the browser, mobile devices and IoT devices through a common set of protocols
This technology has beed standardized by the W3 foundation and the IETF thus ensuring broad compatibility across devices
WebRTC Data Channels Explained
WebRTC apart from enabling audio and video applications can also transfer data between devices across the world, securly through the internet using WebRTC Data Channels
WebRTC data channels provide a way for devices to send arbitrary data securely and efficiently between devices.
Functionality and Features of WebRTC Data Channels
Bi-Directional Data transfer capabilities
Bi directional data transfer is the core feature of webrtc data channels. It means that once the webrtc data connection is established, all the clients that are connected can transfer the data to each other
The data channel uses Stream Control Transmission Protocol (SCTP) that is layered on top of the Datagram Transport Layer Security Protocol (DTLS) and User Datagram Protocol (UDP) to enable this type of data transfer
Key Features
Peer-to-Peer: Data transfer between connected devices
Security: All the data that is transferred using the webrtc data channels is end to end encrypted and is complete secure against eavesdropping and tampering
Flexibility in transmission modes
Reliability delivery: This Guarantees that the data arrives in order and intact that is there is no loss of data, it is like TCP
Unreliable delivery: This uses UDP and allows data to arrive out of order or not at all but reduces latency.
Ordered and unordered messages: Developers can specify whether the messages need to arrive in order or they can arrive in any order. This provides control over speed and latency in sending messages
Low Latency: The data transfer using the WebRTC Data channels is of low latency and high speed
Use-Cases of WebRTC Data Channels
There are a lot of use-cases of WebRTC Data Channels, for real time data transfer between various devices
File Sharing:
Large File Transfers: Users can send large files to other client devices using WebRTC data channels
Privacy: All the files that are transferred using WebRTC data chaneels are fully and end to end encrypted
Example Applications
Web Based file sharing
Collaborative whitebords and other web applications
Gaming: Real time multiplayer gaming can be done using WebRTC data channels
Low Latency Communication: Using the WebRTC data channels there is low latency data transfer which is essential for gaming
Efficient state synchronisation: This keeps games state the same across all the players that are playing the game at the same time
Example applications
Browser based multiplayer apps
Real time strategy games
Virtual Reality and Augmented reality apps
IoT that is Internet of things devices
Direct communication between devices: helps devices communicate with each other using turn servers
Energy efficient: It is quite energy efficient, as compared to other applications basically just built in javascript api. So it is less resource intensive and thus energy efficient
Example applications
Smart home devices
AR and VR devices
Wearable devices
Collaborative applications:
Shared whiteboards and code editors: Multiple users can use the shared whiteboards and code editors using data synchronization with WebRTC Data channels and real time data transfer
Data Visualization: Real time data visualization for analytics can be implemented with real time data transfer
Chat and Messaging services:
Text Messaging: Real time exchange of messages can be implemented
Rich Media Sharing: Not only text but any kind of data can be shared with WebRTC data channels thus enabling rich media sharing
Live Data feeds: Live data feeds can be implemented with WebRTC Data channels these include
Financial tickers: Stock market prices and market data instantly
Sports Scores: Live sports scores and statistics during events for entertainment
Advantages and Benefits of WebRTC Data Channels
Reduced Latency
WebRTC data channels have very low latency especially if you are using a global turn server provider such as Metered.ca TURN servers
Optimized Protocols: The WebRTC data channels use UDP for transport which is much faster than the TCP protocol and this protocol avoids the overhead of connection and error correction
Improved efficiency
Bandwidth Optimization: WebRTC data channels are bandwidth efficient making use of core internet protocols such as UDP to transfer data from and to devices
Resource efficiency: These protocols are resource efficient as well, taking less CPU and memory resources to transfer data
Enhanced User Experience
Real time interactivity: Users experience immidiate responses which leads to applications like online gaming and live collaboration
Seamless integration: The Data Channels are integrated in web browsers and are a core part of WebRTC and as such seamlessly integrated into all modern devices
Security
End to End Encryption: The WebRTC data channels have end to end encryption, authentication and integrety protection built in
Reduced attack surface: Since the data channels are end to end encrypted the attack surface is verymuch reduced with webrtc data channels
Flexibility and control
Customizable delivery options: There are customizable delivery options available with WebRTC Data Channels, if the UDP protocol is blicked by the firewall for some reason the Data channels can work over TCP as well
Protocol agnostic data transfer: WebRTC data channels support various data formats providing versatility in application design
How does WebRTC Data Channels work
Understanding how webrtc data channels work is important in order to implement efficient and reliable real time communiation and data transfer funcitonalities in web applications
In thsi section we are going to learn about the protocols that enable webrtc to function across devices and network environments
Peer to Peer Communication Protocols
WebRTC relies on a combination of protolcs to maintain peer to peer communications between devices
The two most important protocols used in webrtc communications are the ICE or Interactive Connectivity Establishment protocol / framework and the Session description protocol or SDP along with signalling mechanisms.
Interactive Connectivity Establishment ICE Framework
The ICE framework is a methodoloty used by WebRTC to overcome the challenges posed by Netwrok Address Translators or NAT and firewalls
these impede direct peer to peer connectivity thus ICE is responsable for finding the most efficient path to cross agaist these hurdles and ensure smooth connectivity between devices
A TURN server is required for smooth connectivity between devices. Let us learn how the ICE framework really works
How ICE works
Candidate Gathering
Each peer in the network gathers a list of candidates or IP addresses which can be used to establish a connection between devices.
Candidates include
Host Candidates
Server Reflexive Candidates
Relayed Candidates
Candidate exchange
Peers exchange the list of candidates with each other through a signaling mechanism
This exchange lets the peers know of a possible way to access the other peers
Connectivity Check:
Candidate check the connectivity using the candidates shared by the other peers
These checks include STUN and TURN servers that are used to check the accessibility of the peers
Candidate Pair selection:
- The best Candidates pair is selected on a predefined criteria depending on network connectivity performance and success
Components of ICE
STUN Servers
Helps devices to detect their own public IP address
Used primarily to obtain server reflexive candidates
To learn more about STUN server you can visit our article: Stun Server: What is Session Traversal Utilities for NAT?
TURN servers
TURN servers relay traffic between peers when direct connection is not possible due to NAT or firewall restrictions
TURN servers need to be near the peer devices that are connecting to each other for low latency and best connectivity. Consider Metered GLobal TURN server service providers with TURN servers all over the globle
If you are looking for a Free TURN server service then consider the Open Relay Project
ICE Agents
these are software components that handle the ICE process. These components are within the peers that are trying to connect with each other
Session Description Protocol (SDP) and Signaling
The session description protocol is a standardized format that is used to describe communication sessions
In WebRTC, SDP is used in the session negatiation process. It is used to convey information about media capabilities and preferences
Role of SDP
Media configuration
The SDP describes the types of media that is audio, video or data and their associated parameteres, these include encryption methids, codecss and bandwidth limittions
These allows the peers to understand each other's capabilities and configurations and thus these peers can agree on compatible settings
Transport details
- This provides information about network addresses and transport protocols that can be used for communication sessions
Session Negotiation
- A mututal understanding ofsession parameteres takes place by the exchange of offers and answers between peers
Signalling mechanim
What is it
- Signalling is the process of exchanging information about controls, this includes SDP blobs and ICE candidates. This information is required to setup, manage and terminate communication sessions
Not specified by the WebRTC
WebRTC protocol has intentionally left blank the Signalling mechanism, this is to allow developers to utilize whatever method they prefer to signal.
Various methods are available to signal in webrtc, some of the popular ones include WebSockets, SIP and HTTP
Data Transmission Protocols
When talking about WebRTC data channels, the data transmissions protocols are essential for a reliable, secure and efficient communication
The primary protocols used for data transmission in webrtc are Stream Control Transmission Protocol (SCTP) over Datagram Transport Layer Security and User Datagram Protocol UDP
Stream Control Transmission Protocol (SCTP) over Datagram Transport Layer Security (DTLS)
These ensure Secure and Reliable Data transfer
The SCTP is a transport layer protocol thai is used to transmit multiple streams of data between two or more peers that have establsihed a connection
The SCP combines features of both the TCP as well as the UDP protocol like so
Reliability
Message oriented
Multi Streaming
Multihoming
What is DTLS?
Datagram Transport Layer Security is a security and encryption protocol that works with TLS that is Transport Layer Security protocol to encrypted the data end to end and these protocols are designed to work over the UDP
SCTP over DTLS in WebRTC
In WebRTC the SCTP encapsulates DTLS which in turn runs over the UDP protocol. This layered approced combines the advantages of all these protocols
UDP: this provides low latency and fast data transfer speeds with minimal resource overhead
DTLS This provides security by encryption, integrity protection and authentication
SCTP This ensures proper delivery of messages along with support of multiple streams
+----------------------+
| WebRTC Data API |
+----------------------+
| SCTP |
+----------------------+
| DTLS |
+----------------------+
| UDP |
+----------------------+
| IP Network |
+----------------------+
User Datagram Protocol UDP
The User Datagram protocol is a core internet protocol that offers simplicity and minimal overhead for data transfer as compared to other transport protocols such as TCP
Key features of UDP
Connectionless
Unreliable
Low Overhead
No congesetion control
Why UDP is used in WebRTC
Low Latency
Flexibility
Firewall traversal
Role of TURN Servers in WebRTC
NAT or Network address traversal are devices that are found in routers and forewalls that enable multiple devices that are on a local network share a single public IP address
the NAT modify the IP address information that is in teh IP packet headers when they are in transit from WAN to the local network
To learn more about what NATs are how they work refer to this article NAT traversal: How does it work?
Types of NAT include
full cone nat
restricted cone nat
port restricted cone nat
symmetric nat
Each type of NAT handles the translation of UP address and ports differently, thus affecting inbound and outbound traffic
What are firewalls?
Firewalls are security systems that monitor and manage incoming and outgoing traffic based on predetermined secuiruty rules
Firewalls act as barriers betweens trusted internal traffic and untrusted external traffic from the internet
Challenges Posed to direct Peer-to-Peer Connections
Firewalls hide IP address
Port Blocking
Dynamic Port Mapping
Packet filtering
Techniques for NAT traversal
STUN Serves
TURN servers
How TURN Servers Enhance Connectivity
Facilitating Reliable Data Exchange
Rol of TURN servers in WebRTC Data Channels
Ultimate fallback Options
- When STUN mediated connections fail, which they often do because of NAT and firewalls. TURN servers ensure communication can still take place
Seamless User experience
- Users are unaware of the underlying connection methodology thus experiencing uninterrupted connection
Metered TURN servers
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.
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
Servers in all the Regions of the world: Toronto, Miami, San Francisco, Amsterdam, London, Frankfurt, Bangalore, Singapore,Sydney, Seoul, Dallas, New York
Low Latency: less than 50 ms latency, anywhere across the world.
Cost-Effective: pay-as-you-go pricing with bandwidth and volume discounts available.
Easy Administration: Get usage logs, emails when accounts reach threshold limits, billing records and email and phone support.
Standards Compliant: Conforms to RFCs 5389, 5769, 5780, 5766, 6062, 6156, 5245, 5768, 6336, 6544, 5928 over UDP, TCP, TLS, and DTLS.
Multi‑Tenancy: Create multiple credentials and separate the usage by customer, or different apps. Get Usage logs, billing records and threshold alerts.
Enterprise Reliability: 99.999% Uptime with SLA.
Enterprise Scale: With no limit on concurrent traffic or total traffic. Metered TURN Servers provide Enterprise Scalability
5 GB/mo Free: Get 5 GB every month free TURN server usage with the Free Plan
Runs on port 80 and 443
Support TURNS + SSL to allow connections through deep packet inspection firewalls.
Supports both TCP and UDP
Free Unlimited STUN