← Lessons

quiz vs the machine

Gold1390

Networking

WebSockets for Real Time Communication

Learn how an HTTP request upgrades into a full duplex channel.

4 min read · core · beat Gold to climb

Beyond Request and Response

Plain HTTP is request and response, which is awkward for live data like chat or trading prices. WebSockets provide a full duplex channel where either side can send messages at any time over a single long lived connection.

The Upgrade Handshake

A WebSocket connection starts as an ordinary HTTP request carrying an Upgrade header. If the server agrees, it responds with one hundred one Switching Protocols and the same TCP connection is reused for the WebSocket protocol.

  • The handshake reuses ports eighty and four hundred forty three, so it passes through most firewalls.
  • After upgrade, data flows as lightweight frames rather than HTTP messages.
  • Either peer may send a message without being prompted.

When to Use Them

WebSockets shine for bidirectional low latency streams. For one directional server updates, Server Sent Events or long polling may be simpler. For occasional updates, plain polling can be enough.

Trade Offs

Because the connection stays open, servers must track many concurrent sockets, and load balancers need sticky or connection aware routing.

Key idea

WebSockets upgrade an HTTP connection into a persistent full duplex channel, ideal for low latency bidirectional messaging.

Check yourself

Answer to earn rating on the learn ladder.

1. How does a WebSocket connection begin?

2. What key capability do WebSockets add over plain HTTP?