Back
Web Fundamentals

Real-time Communication: WebSockets, SSE & Polling

On this page

mediumFrontend Architecture

Real-time Communication: WebSockets, SSE & Polling

TL;DRWebSockets for bidirectional low-latency. SSE for simple server-to-client push. Polling as fallback. Always plan for reconnection, ordering, and scaling.
High Signal
Google
Meta
Netflix
Agoda
30-Second Answerstart every interview with this

Real-time features require choosing the right transport based on directionality, latency needs, scaling complexity, and operational cost. WebSockets offer full bidirectional communication but are stateful and harder to scale. Server-Sent Events provide simple unidirectional push with built-in reconnection. Polling is easiest but least efficient.

WebSockets = a phone call (both can talk anytime). SSE = a radio broadcast (server talks, you listen). Polling = repeatedly calling to ask 'anything new?' The best choice depends on how often each side needs to speak and how reliably the connection must stay open.

Choose Transport
Bidirectional? → WebSockets
Server Push Only? → SSE
Simple & Reliable? → Polling (fallback)

1WebSockets

Persistent, bidirectional, full-duplex connection. Excellent for chat, collaborative editing, and multiplayer games. Requires careful connection management, reconnection logic, and horizontal scaling (often with Redis Pub/Sub).

useWebSocket.tsts
useEffect(() => {
  const ws = new WebSocket(WS_URL);
  ws.onmessage = (e) => handleMessage(JSON.parse(e.data));
  return () => ws.close();
}, []);

2Server-Sent Events (SSE)

Unidirectional server-to-client streaming over HTTP. Simpler than WebSockets with built-in reconnection and event IDs. Ideal for notifications, live feeds, and progress updates.

3Polling Strategies

Short polling (fixed interval) is simple but wasteful. Long polling holds the connection open until data arrives. Use only when real-time requirements are relaxed.

4Scaling, Reconnection & Consistency

Plan for reconnection with exponential backoff + jitter. Use message ordering, idempotency, and client-side reconciliation to handle missed or out-of-order events.

PropertyWebSocketsSSEPolling
LatencyVery LowLowMedium-High
ScalingComplex (stateful)Easier (HTTP)Simple
Best ForChat, games, collaborationNotifications, live feedsLow-frequency updates
DirectionBidirectionalServer → ClientRequest/Response

WebSockets

Latency

Very Low

Scaling

Complex (stateful)

Best For

Chat, games, collaboration

Direction

Bidirectional

SSE

Latency

Low

Scaling

Easier (HTTP)

Best For

Notifications, live feeds

Direction

Server → Client

Polling

Latency

Medium-High

Scaling

Simple

Best For

Low-frequency updates

Direction

Request/Response

Common questions

  • ›“When would you choose WebSockets over SSE?”
  • ›“How do you handle reconnection in real-time features?”
  • ›“How do you scale WebSockets horizontally?”
  • ›“What are the trade-offs between polling and push-based solutions?”

What interviewers look for

  • Clear understanding of directionality and latency needs
  • Knowledge of reconnection, ordering, and idempotency
  • Scaling awareness (Redis Pub/Sub, sticky sessions)
  • Balanced trade-off reasoning

Short answer (60 sec)

Use WebSockets for bidirectional low-latency needs (chat, games). SSE for simple server push (notifications, feeds). Polling as a simple fallback. Always implement reconnection with backoff + jitter and ensure event ordering/idempotency.

Detailed answer (senior level)

WebSockets provide true bidirectional communication but require stateful scaling and reconnection logic. SSE is simpler for unidirectional push with built-in reconnection. Polling is easiest but inefficient. For production, combine with deduplication, optimistic updates, and client-side reconciliation to handle disconnects and out-of-order events gracefully.

  • Using WebSockets when SSE would suffice
  • No reconnection strategy (users lose updates on disconnect)
  • Ignoring message ordering and idempotency
  • Creating retry storms during outages
  • Scaling WebSockets without proper pub/sub backend
Key Takeaways
  • ✓WebSockets for bidirectional real-time
  • ✓SSE for simple, reliable server push
  • ✓Polling as fallback or for low-frequency updates
  • ✓Always implement reconnection with backoff + jitter
  • ✓Ensure event ordering and idempotency on client
  • ✓Plan for horizontal scaling from day one
  • ✓Choose transport based on direction, latency, and complexity

Related web fundamentals topics

Continue with closely related browser and frontend engineering concepts.

Browser Storage: Cookies, SessionStorage, LocalStorage, IndexedDBWebRTC: Real-Time Communication in the BrowserHow Frontend Developers Can Handle Millions of API Requests Without Crashing EverythingRate Limiting & API Resilience: Retries, Backoff, Jitter, IdempotencyPagination: Offset vs Cursor-BasedAPI Versioning Strategies for Frontend Compatibility
Previous TopicBrowser Storage: Cookies, SessionStorage, LocalStorage, IndexedDBNext Topic WebRTC: Real-Time Communication in the Browser