# WebSocket

> A connection between browser and server that stays open, so both can talk anytime.

- Level: Advanced
- Topics: Networks, Software
- URL: https://tidbit.legios.com.ar/websocket/
- In Spanish: [WebSocket](https://tidbit.legios.com.ar/es/websocket/)

Plain HTTP works like a letter exchange: the browser asks, the server answers, and the conversation is over. The server cannot speak first. A **WebSocket** is a connection that stays open after it is created, so either side can send a message at any moment, in both directions, without asking again.

It starts as an ordinary HTTP request with a special header that says, in effect, 'let us switch to WebSocket' (the **upgrade handshake**). If the server agrees, the same TCP connection is kept and from then on carries small messages, called frames, instead of full requests and responses. In the browser it is a few lines of code: `new WebSocket("wss://chat.example.com")`, then a function that runs every time a message arrives. The `wss://` prefix means it is encrypted, like HTTPS.

It is what makes real-time web apps feel instant: chat apps, live sports scores, shared documents where you see other people typing, trading screens, multiplayer browser games. The old alternative was **polling**, asking the server 'anything new?' every few seconds, which is slower and wastes requests when the answer is no. The cost is that the server has to keep one open connection per user, which takes memory and care when scaling, and connections drop on bad networks, so apps must reconnect.

## An analogy

HTTP is sending text messages and waiting for each reply; a WebSocket is a phone call that stays connected, where either person can speak the moment they have something to say.

## Keep in mind

- One connection stays open and carries messages both ways.
- It begins as an HTTP request that is upgraded; `wss://` is the encrypted version.
- It replaces polling for chats, live data, collaboration and games.
- Servers must hold one connection per user and clients must handle reconnecting.

## Go deeper

- [The WebSocket API (MDN)](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API)
- [RFC 6455, the WebSocket protocol](https://www.rfc-editor.org/rfc/rfc6455)
- [WebSocket on Wikipedia](https://en.wikipedia.org/wiki/WebSocket)

## Related terms

- [HTTP](https://tidbit.legios.com.ar/http/): The request-and-response language browsers and websites speak.
- [Web browser](https://tidbit.legios.com.ar/web-browser/): The app that fetches web pages from the internet and draws them on your screen.
- [Server](https://tidbit.legios.com.ar/server/): A computer whose job is to answer requests from other computers, all day, every day.
- [TCP and UDP](https://tidbit.legios.com.ar/tcp-and-udp/): Two ways to send packets: TCP checks everything arrives, UDP just sends and moves on.
- [Encryption](https://tidbit.legios.com.ar/encryption/): Scrambling data so that only someone with the key can read it.
- [HTTPS](https://tidbit.legios.com.ar/https/): The padlock in your browser: web traffic nobody in between can read.
