Card 07/ 10

ComparisonDifficulty: Intermediate1 min

Choosing How Updates Reach the Other Side

Who needs to send, how often, and who is on the other end decide it.

Choosing among polling, server-sent events, WebSockets and webhooks comes down to three questions about the feature: who is on the other end, who needs to send, and how soon the news must arrive. Answer them in that order, and one pattern is left.

Which pattern fits this feature?
  1. When The other end is a server, and it has the news

    , choose

    A webhook

  2. When Both sides send often

    , choose

    WebSockets

  3. When Only the server sends, and news must arrive at once

    , choose

    Server-sent events

  4. When Only the server sends, at once, but the path carries nothing but plain requests

    , choose

    Long polling

  5. When A delay of a few seconds is fine

    , choose

    Short polling

Four features through the decision
FeatureWho sendsHow soonPattern
Chat between usersBoth sidesAt onceWebSockets
Live match scoreServer onlyAt onceServer-sent events
Report export statusServer onlyWithin secondsShort polling
Payment confirmed by the providerAnother serverAt onceWebhook

The common mistake. WebSockets are chosen for every live feature because they can do everything. For a live score they add a connection the client never sends on, with no built-in resume, where server-sent events would reconnect and resume by themselves.

The other common mistake. Polling the payment provider every few seconds to learn whether a charge went through. The provider knows the moment it happens, and a webhook tells you then, with no wasted requests.

Mixing is normal. One product often uses several: WebSockets in the chat, server-sent events on a dashboard, webhooks from its payment provider, and polling on an export page. Each feature gets the cheapest pattern that meets its own need.

So choose per feature, by who is on the other end, who sends, and how soon, and pick the simplest pattern that answers all three.