What are Long-Polling, Websockets, Server- Sent Events (SSE) and Comet?

Long-Polling, Websockets, Server-Sent Events (SSE), and Comet are real-time communication techniques that enable persistent connections between client and server. Each method offers different approaches to overcome the limitations of traditional HTTP request-response patterns for real-time applications.

Long Polling

Long polling is a technique where a client sends an HTTP request to the server and keeps the connection open until the server has data to send back. Instead of immediately responding with empty data, the server holds the request until new information becomes available or a timeout occurs.

Long Polling Flow Client Server 1. Request (held open) 2. Response (when data available) 3. New request immediately Connection held open until server has data to send

Long polling supports up to 6 simultaneous connections per browser and offers simple load balancing. It's supported by all web browsers but is resource-intensive on the server side and doesn't support automatic reconnection.

WebSockets

WebSocket is a communication protocol that provides full-duplex communication channels over a single TCP connection. Unlike HTTP, WebSockets maintain a persistent connection allowing both client and server to send data at any time.

The WebSocket protocol enables lightweight, real-time interaction between web browsers and servers. After an initial HTTP handshake, the connection is upgraded to WebSocket protocol, establishing a bidirectional communication channel that remains open until explicitly closed.

WebSockets support up to 1024 simultaneous connections and are widely supported by modern browsers including Chrome, Firefox, Safari, Opera, and Edge. They provide efficient real-time communication but require more complex load balancing and proxy configurations.

Server-Sent Events (SSE)

Server-Sent Events is a standard allowing servers to push data to web pages over HTTP. SSE enables automatic updates from server to client through a persistent HTTP connection using the EventSource JavaScript API.

Originally proposed by WHATWG and first implemented by Opera in 2006, SSE provides a simple way for servers to send continuous data streams to clients. The connection remains open, and the server can send formatted messages whenever new data is available.

SSE supports up to 6 simultaneous connections per browser and includes built-in reconnection handling. However, it's unidirectional (server-to-client only) and has limited browser support, particularly lacking support in Internet Explorer.

Comet

Comet is a web application model where the server pushes data to the client using long-held HTTP connections. Also known as "Ajax Push" or "Reverse Ajax," Comet reverses the traditional client-server request pattern.

The technique involves making a normal HTTP request, but the server doesn't immediately close the response. Instead, it keeps the connection open and streams data as it becomes available. The server sends a delimiter string to mark the end of each message update.

Comet enables bidirectional communication and eliminates the need for constant polling, but it can be resource-intensive and may face issues with proxy servers and firewalls that expect traditional HTTP behavior.

Comparison

Feature Long Polling WebSockets SSE Comet
Connection Type HTTP WebSocket Protocol HTTP HTTP
Communication Request-Response Full-duplex Server-to-client Server-to-client
Browser Support Universal Modern browsers Limited Universal
Reconnection Manual Manual Automatic Manual

Conclusion

Each real-time communication technique serves different use cases: Long Polling for universal compatibility, WebSockets for bidirectional real-time communication, SSE for server-to-client streaming, and Comet for legacy push applications. The choice depends on browser support requirements, communication patterns, and infrastructure constraints.

Updated on: 2026-03-16T23:36:12+05:30

848 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements