Skip to content

WebSocket

WebSocket is a communication protocol that keeps a single, long-lived connection open between a client and a server so that either side can send messages to the other at any time. It runs over TCP and was standardized by the Internet Engineering Task Force (IETF) as RFC 6455 in 2011.

A connection starts out as an ordinary HTTP request. The client sends a GET request carrying an Upgrade: websocket header, and the server agrees by answering with the status 101 Switching Protocols. From then on, the same TCP connection carries WebSocket traffic instead of HTTP. By default, ws:// addresses use port 80 and encrypted wss:// addresses use port 443, the same ports as regular web traffic.

After the handshake, data travels in small units called frames. A frame holds either a whole message or one fragment of a larger message, and the protocol defines a few kinds:

  • Data frames: Carry UTF-8 text or arbitrary binary data.
  • Ping and pong frames: Check that the other side is still reachable and keep idle connections alive.
  • Close frames: Start and confirm an orderly shutdown that either side can initiate.

The result is a full-duplex channel, meaning data flows in both directions at once, and the server can push updates the moment they happen. This suits chat applications, multiplayer games, and live dashboards, which would otherwise need to poll the server repeatedly to learn whether anything changed.

The timeline below delivers the same five server-side updates over a WebSocket and through HTTP polling, which shows how many requests each approach sends and how long each update waits before it reaches the client:

Interactive diagram — enable JavaScript to view.

The cost of this design is server-side state. A server has to keep every open connection alive in memory, which makes load balancing and horizontal scaling harder than with short HTTP requests. Python servers often juggle many connections through asynchronous programming with asyncio, and third-party libraries such as websockets implement the protocol itself.

Python Socket Programming

Tutorial

Socket Programming in Python (Guide)

Build a Python socket server and client from scratch, then extend it to handle multiple connections at once without blocking.

advanced python stdlib web-dev

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Martin Breuss • Updated Nov. 1, 2026