What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?
Categories:
Understanding Real-time Communication: Long-Polling, WebSockets, SSE, and Comet

Explore the fundamental differences and use cases for various client-server communication techniques, including Long-Polling, WebSockets, Server-Sent Events (SSE), and Comet, to build responsive web applications.
In the world of web development, achieving real-time or near real-time communication between a client (browser) and a server is crucial for many modern applications, such as chat applications, live dashboards, and gaming. Historically, web applications relied on simple HTTP request/response cycles, which are inherently stateless and pull-based. However, as user expectations evolved, so did the need for more dynamic and push-based communication methods. This article delves into several key techniques that enable persistent or semi-persistent connections: Long-Polling, WebSockets, Server-Sent Events (SSE), and the broader concept of Comet.
Traditional HTTP Polling vs. Long-Polling
Before diving into more advanced techniques, it's important to understand the limitations of traditional HTTP polling. In this model, the client repeatedly sends requests to the server at fixed intervals to check for new data. This is inefficient as most requests return no new information, leading to unnecessary network traffic and server load. Long-Polling, also known as 'hanging GET' or 'HTTP Streaming', improves upon this by keeping the HTTP connection open until new data is available or a timeout occurs. Once data is sent, the connection is closed, and the client immediately opens a new one.
sequenceDiagram participant Client participant Server Client->>Server: HTTP Request (Poll) Server-->>Client: No new data (after delay or timeout) Client->>Server: HTTP Request (Poll) Server-->>Client: New data available Client->>Server: HTTP Request (Poll) Server-->>Client: No new data (after delay or timeout)
Traditional HTTP Polling Sequence
sequenceDiagram participant Client participant Server Client->>Server: HTTP Request (Long-Poll) Note over Server: Server holds connection Server-->>Client: New data available (or timeout) Client->>Server: HTTP Request (Long-Poll) Note over Server: Server holds connection Server-->>Client: New data available (or timeout)
Long-Polling Sequence
WebSockets: Full-Duplex Communication
WebSockets represent a significant leap forward in real-time communication. Unlike HTTP, which is stateless and unidirectional (request-response), WebSockets provide a full-duplex, persistent connection between a client and a server. After an initial HTTP handshake, the connection is 'upgraded' to a WebSocket connection, allowing both client and server to send and receive data simultaneously over the same open connection. This drastically reduces overhead and latency, making it ideal for applications requiring very low-latency, high-frequency data exchange.
sequenceDiagram participant Client participant Server Client->>Server: HTTP Handshake (Upgrade: websocket) Server-->>Client: HTTP 101 Switching Protocols Note over Client,Server: WebSocket Connection Established Server->>Client: Data Push 1 Client->>Server: Client Message 1 Server->>Client: Data Push 2 Client->>Server: Client Message 2
WebSocket Communication Flow
const socket = new WebSocket('ws://localhost:8080');
socket.onopen = (event) => {
console.log('WebSocket connection opened:', event);
socket.send('Hello Server!');
};
socket.onmessage = (event) => {
console.log('Message from server:', event.data);
};
socket.onclose = (event) => {
console.log('WebSocket connection closed:', event);
};
socket.onerror = (error) => {
console.error('WebSocket error:', error);
};
Basic JavaScript WebSocket Client Example
Server-Sent Events (SSE): Unidirectional Push
Server-Sent Events (SSE) offer a simpler alternative to WebSockets when the communication requirement is primarily one-way: server to client. SSE uses a standard HTTP connection that remains open, allowing the server to push data to the client whenever new information is available. Unlike WebSockets, SSE is built on top of HTTP and does not require a protocol upgrade. It's particularly well-suited for scenarios like live news feeds, stock tickers, or real-time score updates where the client only needs to receive data and doesn't need to send frequent messages back to the server. SSE also includes built-in reconnection mechanisms.
sequenceDiagram participant Client participant Server Client->>Server: HTTP GET /events (Accept: text/event-stream) Note over Server: Server keeps connection open Server->>Client: event: message\ndata: First update\n\n Server->>Client: event: update\ndata: Second update\n\n Server->>Client: event: message\ndata: Third update\n\n
Server-Sent Events (SSE) Flow
const eventSource = new EventSource('/events');
eventSource.onmessage = (event) => {
console.log('Received event:', event.data);
};
eventSource.onerror = (error) => {
console.error('EventSource failed:', error);
eventSource.close();
};
eventSource.addEventListener('customEvent', (event) => {
console.log('Received custom event:', event.data);
});
Basic JavaScript Server-Sent Events Client Example
Comet: The Umbrella Term
Comet is not a specific technology but rather an umbrella term that encompasses various techniques used to achieve push-like functionality over HTTP, effectively simulating a persistent connection. It predates WebSockets and includes methods like Long-Polling and Streaming (where the server keeps the connection open indefinitely and continuously sends data). The goal of Comet applications is to allow the server to push data to the client without the client explicitly requesting it, overcoming the limitations of traditional HTTP. While WebSockets have largely superseded Comet for full-duplex communication, understanding Comet helps appreciate the evolution of real-time web technologies.
graph TD A[Comet] --> B[Long-Polling] A --> C[HTTP Streaming] B --> D[Client sends request] B --> E[Server holds connection] E --> F[Server sends data or timeout] F --> D C --> G[Client sends request] C --> H[Server keeps connection open] H --> I[Server continuously sends data] I --> J[Client processes data]
Comet Techniques Overview