SteelReed

Search

Search posts, questions…

Web & REST APIQuestion 16 / 25

Junior

How do HTTP and WebSocket differ, and when should you use WebSocket?

Short answer

HTTP is ask and answer; the server can't send on its own. WebSocket upgrades from one HTTP request (101), and that connection becomes a long-lived two-way channel.

ServerClientServerClientGET with Upgrade websocketheader101 Switching Protocolsframe at any timeframe at any time

3 points to say:

  1. HTTP: the client always speaks first.
  2. WebSocket: two-way on the same TCP connection.
  3. Server-to-client only: use SSE.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.