Web5 min read10 interview questionsNew · November

WebSocket vs Server-Sent Events

Two ways for a server to push live updates to a browser, and how to choose.

In one sentenceServer-Sent Events let a server stream updates one way to the browser, while WebSockets keep a two-way channel open.

Think of it like thisSSE is a radio broadcast you only listen to. A WebSocket is a phone call where both sides can talk.

The big picture

Tap any step to see where to use it, how to use it and an example result.

The problem

Plain HTTP is ask-and-answer. For live scores, chat or progress bars the server needs to speak first. Polling, asking again every few seconds, works but wastes requests and adds delay.

Server-Sent Events

The browser opens one normal HTTP request and the server keeps it open, sending text events. It is built into browsers as EventSource, reconnects on its own and works well with proxies. It is one way only: server to browser. It is how many AI chat tools stream text as it is written.

WebSockets

A request upgrades the connection to the WebSocket protocol. After that, either side can send text or binary messages at any time with little overhead. It suits chat, games and collaborative editing, but you handle reconnection and scaling yourself.

Choosing

Start with the simplest tool that fits:

  • Only the server talks (feeds, notifications, streaming text): SSE.
  • Both sides talk often or you need binary data: WebSocket.
  • Rare updates: plain polling may be enough.

Side by side

FeatureServer-Sent EventsWebSocket
DirectionServer to browser onlyBoth directions
DataTextText and binary
ReconnectAutomaticYou build it
Built onNormal HTTPUpgraded connection
Best forFeeds, notifications, streamed textChat, games, live collaboration

Common mistakes

  • Using WebSockets when one-way updates would do.
  • Forgetting to reconnect and resend missed messages.
  • Keeping thousands of idle connections without planning server capacity.
  • Sending sensitive data over ws:// instead of wss://.

Quick recap

  • SSE is one-way server to browser over normal HTTP.
  • WebSocket is two-way and supports binary.
  • SSE reconnects automatically; WebSockets do not.
  • Pick the simplest option that meets the need.

Try it yourself

Think of three live features, such as a chat, a stock ticker and a file upload progress bar. Decide which of the three options suits each and why.

Top 10 interview questions

Ordered from basic to advanced. Try answering out loud first, then open the answer.

  1. What problem do WebSockets and SSE solve?Basic

    They let the server send data to the browser as soon as it is ready, instead of the browser repeatedly asking for updates.

  2. What is Server-Sent Events?Basic

    A browser feature where the server keeps an HTTP response open and streams text events to the page. Communication goes in one direction, from server to client.

  3. What is a WebSocket?Basic

    A long-lived, two-way connection between browser and server that starts as an HTTP request and is then upgraded, allowing either side to send messages at any time.

  4. What is polling and when is it fine?Basic

    The client asks for updates at intervals. It is simple and fine for updates that are rare or not urgent, but wasteful and slower for live data.

  5. How does the WebSocket handshake work?Intermediate

    The browser sends an HTTP request with an Upgrade: websocket header. The server replies 101 Switching Protocols, and from then on the same connection carries WebSocket frames.

  6. What features does SSE give you for free?Intermediate

    Automatic reconnection, an event ID so the client can say where it left off using Last-Event-ID, and simple use through the EventSource API.

  7. Can SSE send binary data or messages from the client?Intermediate

    Not binary: it is text only. Client-to-server messages need a separate normal request, such as a POST.

  8. How do you secure a WebSocket connection?Intermediate

    Use wss:// so it is encrypted, authenticate during the handshake with a token or cookie, check the Origin header, validate every message and limit message sizes and rates.

  9. What makes scaling WebSockets harder than normal requests?Advanced

    Connections stay open, so servers hold state and memory per user. You need a way to route messages between servers, such as a pub/sub broker, plus sticky routing or shared state, and careful handling of deploys.

  10. What do HTTP/2 and proxies change for SSE?Advanced

    With HTTP/2 many streams share one connection, removing the old limit of about six per domain in HTTP/1.1. Proxies and load balancers must not buffer or time out long responses, so disable buffering and send periodic keep-alive comments.

Back to Learn Hub

Guides are simplified on purpose. Tool names and details change, so check the official documentation before relying on a detail.