Skip to content
BENIBIX Learn

WebSocket

Development🌐 Web DevelopmentReal-Time

Also known as WebSockets, WS, WSS

An open, two-way line between browser and server. Either side can send at any time.

Think of it like

Think of it like a phone call instead of letters. Once the line is open, both sides can talk at any time.

Example

Jack's chat app opens one WebSocket per user. When Emma types a message, Lucy sees it in 50 milliseconds.

Why it matters

Anything live, like chat or a shared board, needs messages pushed both ways at once.

Definition

A WebSocket is a link between a browser and a server that stays open. Both sides can send messages at any time. It starts as a normal web request, then upgrades to a two-way line. Messages arrive in milliseconds, with no new request for each one. Chat apps, live games and shared editors use it. Addresses start with ws:// or, when encrypted, wss://.

Example code

javascript
const ws = new WebSocket("wss://example.com/chat");
ws.onmessage = (event) => console.log(event.data);
ws.send("Hi Emma");
Open the line, print each message that arrives, and send one.

Common mistake

Not planning for a dropped line. Phones change networks all the time, so reconnect on your own.

Key takeaways

  1. 1
    Stays open. One line, many messages.
  2. 2
    Both ways. Server or browser can send.
  3. 3
    Plan to reconnect. Lines drop on real networks.

Compare