← Back to all articles

WebSocket vs Polling: Choosing Real-Time Communication

NetworkBeginner

Three approaches

ApproachMechanismCost
Short pollingPoll on a timer for new dataWasted requests, high latency
Long pollingHold the request until data arrivesRebuilds each time, server holds connections
WebSocketFull-duplex long connection after one handshakeConnection needs upkeep, stateful

How to choose

  • Low frequency, tolerant delay: short polling is simplest;
  • Near-real-time, one-way push: long polling suffices and is proxy-friendly;
  • High-frequency two-way, chat or quotes: WebSocket fits best.

WebSocket notes

  1. Heartbeat: send pings or a proxy may drop the idle connection;
  2. Reconnect: on flaky networks, reconnect with exponential backoff to avoid hammering the server;
  3. Scale out: with multiple instances, a message bus broadcasts to the node holding that connection.