# WebSocket

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

**Field:** Software Development  
**Category:** Web Development  
**Also known as:** WebSockets, WS, WSS

## 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.

## 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

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

## 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.

## Why it matters

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

## Common mistake

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

## Three principles

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

## Related terms

- [Server-Sent Events (SSE)](https://learn.benibix.com/term/development/server-sent-events)
- [Polling](https://learn.benibix.com/term/development/polling)
- [Back End](https://learn.benibix.com/term/development/back-end)
- [JavaScript](https://learn.benibix.com/term/development/javascript)

## Told apart from

- [WebSocket vs Server-Sent Events](https://learn.benibix.com/compare/websocket-vs-server-sent-events): Both keep a line open so news arrives at once. A WebSocket lets both sides send. Server-sent events only flow from the server to the browser.

---

Source: https://learn.benibix.com/term/development/websocket
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
