Video in progress

For a notification feed, would you pick SSE or WebSocket? What do you base the decision on?

The main question is whether data flows in both directions often. If it's only server to client, SSE is simpler: plain HTTP and automatic reconnect. If the client also writes constantly (chat, games), use WebSocket.

📖 Answer

In a notification feed the client sends almost nothing; it only receives. So I usually start with SSE.

For SSE:

  • there's no separate protocol, and in Spring MVC it takes a few lines with SseEmitter;
  • EventSource reconnects on its own after a drop and sends Last-Event-ID;
  • authentication works with a plain cookie.

For WebSocket:

  • the client also writes often (chat, a "typing..." indicator, game moves);
  • you need binary data;
  • opening an HTTP request for every message is expensive.

One trap: on HTTP/1.1 the browser opens at most 6 connections per domain. If a user opens 7 tabs, the SSE connections can block other requests. On HTTP/2 this problem almost disappears, because everything is multiplexed over one connection.