Under Construction
Backend••16 min•104 views••

Polling and WebSocket what’s the difference?

Minh Khoa

Minh Khoa

Author

image.pngIn a multiplayer lobby, player B clicks Ready. The server records the new state, but player A’s screen also needs to be updated. Polling and WebSocket are two common ways to bring that information from the server to the client.

Polling — the client sends periodic requests

With polling every 2 seconds, client A continuously calls API to fetch the room state. The server returns the player list along with the current Ready status.

Suppose A sends requests at seconds 0, 2, and 4. The server records that B became Ready at second 2,1. A will receive the new state in the response to the check at second 4. The waiting time alone until the next polling cycle has taken about 1.9 seconds, plus network transmission and processing time.

Reducing the interval between polling rounds makes updates faster, but it also increases the number of requests. Even when the room has no changes, clients still continue checking.

Polling is suitable for data that changes infrequently or can be updated a few seconds late, such as a leaderboard. The system can be implemented with HTTP API commonly.

WebSocket — the server sends updates over an open connection

WebSocket maintains a two-way connection between the client and the server. In the lobby example, when the server records that B is Ready, the server application sends that event to the clients in the room. A receives the notification and updates the interface immediately after processing the message.

WebSocket removes the waiting time until the next polling cycle. Network latency and processing time still exist, but the client does not need to send a separate request for each update it receives. MDN — WebSocket API

In return, the application must manage the connection lifecycle. If a player loses the network connection and then reconnects, the client needs to fetch the latest room state to update the changes that occurred during the interruption. Reconnection should also use increasing backoff intervals to avoid many clients repeatedly trying at the same time when the server has issues. RFC 6455 — Connection recovery

Comparison pointPeriodic pollingWebSocket
How data is receivedThe client sends requests to checkThe server can proactively send over an open connection
When the data has not changedRequests are still generated on scheduleNo need to resend the state just to check, but a heartbeat may be needed
Update latencyDepends additionally on the polling cycleNo need to wait for the polling cycle
Suitable exampleA leaderboard updated periodicallyChat, Ready status, players joining or leaving the room

With a lobby, Ready status needs timely feedback, so WebSocket is suitable. A leaderboard that only needs to be refreshed every 30 seconds can use polling and keep the implementation simpler.