Sobes.tech
Junior

What is the difference between SSE and WebSocket? How does SSE work?

sobes.tech AI

Answer from AI

SSE (Server-Sent Events) and WebSocket are technologies for real-time data exchange between client and server, but with different features.

Differences:

  • SSE — unidirectional channel: the server can send data to the client, and the client can only receive. The client opens an HTTP connection and listens for events.
  • WebSocket — bidirectional channel: both the client and server can send messages at any time over a single connection.

How SSE works:

  1. The client makes an HTTP request with the header Accept: text/event-stream.
  2. The server responds with the content type text/event-stream and starts sending data in event format.
  3. The client listens to the stream and processes incoming events.

SSE is well-suited for updates where the server simply sends data, such as news or notifications, without the need for two-way communication.