WebRTC Signaling over HTTP Long Polling

While WebSockets are the industry standard for WebRTC signaling, HTTP Long Polling can indeed be successfully used as an alternative transport mechanism. This article explores how HTTP Long Polling functions for WebRTC connection establishment, compares its performance with WebSockets, and outlines the specific scenarios where opting for long polling makes practical sense.

How WebRTC Signaling Works

WebRTC requires a signaling channel to allow two peers to find each other, negotiate media configurations, and share network addresses (ICE candidates). Crucially, the WebRTC specification does not define a mandatory signaling protocol. As long as the two peers can exchange Session Description Protocol (SDP) offers, answers, and ICE candidates, the signaling mechanism is considered successful.

Because WebRTC is transport-agnostic regarding signaling, any bidirectional communication channel can be used. This includes WebSockets, Server-Sent Events (SSE), gRPC, and HTTP Long Polling.

Implementing Signaling via HTTP Long Polling

In an HTTP Long Polling setup, the client initiates an HTTP request to the signaling server. Instead of responding immediately, the server holds the request open until new signaling data (such as an incoming SDP offer or an ICE candidate from the remote peer) is available.

Once the data is ready, the server sends the HTTP response, and the connection closes. The client immediately opens a new long-polling request to wait for the next piece of signaling data. To send data back to the server, the client simply issues a standard, separate HTTP POST request.

To successfully implement this, developers must manage several technical challenges:

WebSockets vs. HTTP Long Polling

While both protocols can achieve the same end result, they have different operational characteristics:

Feature WebSockets HTTP Long Polling
Connection Overhead Low (Single handshake, minimal frame headers) High (New HTTP headers for every request/response cycle)
Latency Extremely Low (Real-time bidirectional) Moderate (Dependent on network round-trip time for new requests)
Server Resource Usage High memory (Keeps persistent TCP connections open) High CPU/Network (Frequent connection setup and teardown)
Firewall Compatibility Can be blocked by strict enterprise firewalls Highly compatible (Uses standard HTTP/HTTPS ports 80/443)

When to Choose HTTP Long Polling

Although WebSockets offer lower latency and less overhead, HTTP Long Polling is a highly viable choice under specific constraints:

  1. Strict Enterprise Firewalls: Some corporate networks block non-standard protocols or WebSocket connections. HTTP Long Polling easily bypasses these restrictions because it uses standard HTTP traffic.
  2. Serverless Architectures: In serverless environments (like AWS Lambda or Google Cloud Functions), keeping a persistent WebSocket connection open is difficult or expensive. HTTP Long Polling aligns perfectly with the stateless, request-response nature of serverless computing.
  3. Fallback Mechanisms: Many robust WebRTC applications use WebSockets as their primary signaling channel and seamlessly fall back to HTTP Long Polling if the WebSocket connection fails to establish.