DEV Community

Cover image for WebRTC Data Channels: A guide.
alakkadshaw
alakkadshaw

Posted on • Originally published at metered.ca

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 Peer-to-Peer Communication Process<br>

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

WebRTC Data Transmission Protocol Stack<br>

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

  1. File Sharing:

    1. Large File Transfers: Users can send large files to other client devices using WebRTC data channels
    2. Privacy: All the files that are transferred using WebRTC data chaneels are fully and end to end encrypted
    3. Example Applications

      1. Web Based file sharing
      2. Collaborative whitebords and other web applications
  2. Gaming: Real time multiplayer gaming can be done using WebRTC data channels

    1. Low Latency Communication: Using the WebRTC data channels there is low latency data transfer which is essential for gaming
    2. Efficient state synchronisation: This keeps games state the same across all the players that are playing the game at the same time
    3. Example applications

      1. Browser based multiplayer apps
      2. Real time strategy games
      3. Virtual Reality and Augmented reality apps
  3. IoT that is Internet of things devices

    1. Direct communication between devices: helps devices communicate with each other using turn servers
    2. 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
    3. Example applications

      1. Smart home devices
      2. AR and VR devices
      3. Wearable devices
  4. Collaborative applications:

    1. 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
    2. Data Visualization: Real time data visualization for analytics can be implemented with real time data transfer
  5. Chat and Messaging services:

    1. Text Messaging: Real time exchange of messages can be implemented
    2. Rich Media Sharing: Not only text but any kind of data can be shared with WebRTC data channels thus enabling rich media sharing
  6. Live Data feeds: Live data feeds can be implemented with WebRTC Data channels these include

    1. Financial tickers: Stock market prices and market data instantly
    2. Sports Scores: Live sports scores and statistics during events for entertainment

Advantages and Benefits of WebRTC Data Channels

  1. Reduced Latency

    1. WebRTC data channels have very low latency especially if you are using a global turn server provider such as Metered.ca TURN servers
    2. 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
  2. Improved efficiency

    1. Bandwidth Optimization: WebRTC data channels are bandwidth efficient making use of core internet protocols such as UDP to transfer data from and to devices
    2. Resource efficiency: These protocols are resource efficient as well, taking less CPU and memory resources to transfer data
  3. Enhanced User Experience

    1. Real time interactivity: Users experience immidiate responses which leads to applications like online gaming and live collaboration
    2. 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
  4. Security

    1. End to End Encryption: The WebRTC data channels have end to end encryption, authentication and integrety protection built in
    2. Reduced attack surface: Since the data channels are end to end encrypted the attack surface is verymuch reduced with webrtc data channels
  5. Flexibility and control

    1. 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
    2. 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

  1. Candidate Gathering

    1. Each peer in the network gathers a list of candidates or IP addresses which can be used to establish a connection between devices.
    2. Candidates include

      1. Host Candidates
      2. Server Reflexive Candidates
      3. Relayed Candidates
  2. Candidate exchange

    1. Peers exchange the list of candidates with each other through a signaling mechanism
    2. This exchange lets the peers know of a possible way to access the other peers
  3. Connectivity Check:

    1. Candidate check the connectivity using the candidates shared by the other peers
    2. These checks include STUN and TURN servers that are used to check the accessibility of the peers
  4. Candidate Pair selection:

    1. The best Candidates pair is selected on a predefined criteria depending on network connectivity performance and success

Components of ICE

STUN Servers

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

  1. UDP: this provides low latency and fast data transfer speeds with minimal resource overhead

  2. DTLS This provides security by encryption, integrity protection and authentication

  3. SCTP This ensures proper delivery of messages along with support of multiple streams

+----------------------+
|    WebRTC Data API   |
+----------------------+
|         SCTP         |
+----------------------+
|         DTLS         |
+----------------------+
|         UDP          |
+----------------------+
|      IP Network      |
+----------------------+
Enter fullscreen mode Exit fullscreen mode

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

TURN server works

Role of TURN Servers in WebRTC

NAT or Network address traversal are devices that are found in routers and firewalls 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

  1. full cone nat

  2. restricted cone nat

  3. port restricted cone nat

  4. 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

  1. Firewalls hide IP address

  2. Port Blocking

  3. Dynamic Port Mapping

  4. Packet filtering

Techniques for NAT traversal

  1. STUN Serves

  2. TURN servers

NAT Traversal with STUN and TURN Servers<br>

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

Metered TURN servers

  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 all the Regions of the world: Toronto, Miami, San Francisco, Amsterdam, London, Frankfurt, Bangalore, Singapore,Sydney, Seoul, Dallas, New York
  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. - Supports both TCP and UDP
  15. - Free Unlimited STUN

Top comments (1)

Collapse
 
alakkadshaw profile image
alakkadshaw

Thank you for reading. I hope you liked the article