Networking

WebSockets

Normal HTTP is request-and-response: the browser asks, the server answers, done. WebSockets keep a single connection open so both sides can send messages at any time.

Why use them

They're ideal for chat, live dashboards, multiplayer games, and anything that needs instant updates without constantly re-asking the server.

How it starts

A WebSocket begins as an HTTP request that asks to upgrade the connection. Once accepted, the same TCP connection stays open for two-way messaging.

const ws = new WebSocket("wss://example.com/live");
ws.onmessage = (e) => console.log(e.data);
ws.send("hello");