INTERNET FUNDAMENTALSAI-REVIEWED

What are WebSockets?

Useful first: HTTP · optional, not a requirement

Choose your depth

Stay with the essentials, or look closer.

Simple explanation
2 min read · estimate

YOUR GOALRecognize why an application might keep a two-way connection open.

A conversation that stays open

Updates without asking again

Imagine a shared noticeboard. You post a message, and someone else posts one later. With an open WebSocket connection, your browser can send your post and the server can send the other update without waiting for another request from you.

This is useful for live conversations and changing information. Either side can send messages while the connection is open. That does not make the network instant or prevent interruptions.

[1]

WebSocket vs HTTP

A normal HTTP request and response is an exchange: ask for something, then receive a response. WebSocket adds an ongoing message channel. The distinction is how messages are exchanged, not whether the page looks interactive.

A hypothetical noticeboard could fetch its starting contents with HTTP and then receive new posts over WebSocket. WebSocket is not required for every interactive page; HTTP techniques can also deliver updates.

[1] [2]
THE IDEA TO KEEP

WebSocket gives the browser and server a continuing two-way message channel.

ONE THING TO WATCH OUT FOR

A live page must keep asking for every update.

With WebSocket, the server can send updates over an already-open connection without a new request for each one.

A QUICK CHECK, NOT A TEST
The server has a new post and the WebSocket connection is still open. Which statement about sending the new post is correct?
Show the explanation

The server can send a message over the existing WebSocket connection. Either endpoint can send on an open WebSocket connection. A broken connection still needs recovery.

Progress is saved only in this browser.

KEEP EXPLORINGHTTP
Was this explanation clear?
Report a mistake by email
Basic explanation
2 min read · estimate

YOUR GOALFollow the connection lifecycle and distinguish ws from wss.

Open, exchange, close

The opening handshake

In the classic HTTP/1.1 handshake, the client asks to upgrade to WebSocket. An accepting server replies with 101 Switching Protocols and the required handshake headers. Messages can then travel over the connection.

Browser code creates a WebSocket object and waits for its open event before sending. Creating the object starts a connection attempt; it does not mean the connection has already succeeded.

[2] [3]

ws vs wss

wss://updates.example/live is an illustrative secure WebSocket address. The wss scheme uses TLS to protect traffic in transit; ws does not itself add TLS. Public HTTPS applications should use secure WebSocket connections.

Encryption protects the connection. It does not decide which noticeboard a user may edit: the application must still enforce its own access rules.

[2] [3]

A closed connection needs a recovery plan

When the connection closes, the browser fires a close event. The standard browser object does not automatically reopen itself. An application or library must arrange a new connection if one is needed.

Reconnecting also does not automatically restore missed posts. For our example, the application could request the latest board contents after reconnecting. That is an application design choice, not a built-in WebSocket history feature.

[3] [4]
THE IDEA TO KEEP

Wait for open, use wss for protected transport, and design what happens after close.

ONE THING TO WATCH OUT FOR

Once a WebSocket opens, it stays connected forever.

Connections can close or fail. Reconnection and recovery of missing application data require explicit handling.

A QUICK CHECK, NOT A TEST
A noticeboard reconnects after being offline. How can it recover posts it missed?
Show the explanation

Use an application recovery step, such as requesting the current board. A new connection supplies a channel, not an automatic message history. The application defines how to catch up.

Progress is saved only in this browser.

KEEP EXPLORINGHTTP
Was this explanation clear?
Report a mistake by email
Advanced explanation
3 min read · estimate

YOUR GOALSeparate protocol framing and queued bytes from application success.

Messages, buffers, and confirmation

One message can span several frames

A WebSocket message may use one frame or several fragments. For example, one large text message can travel in multiple frames without becoming multiple application messages. The browser exposes received messages through its message event.

Client-to-server frames are masked, but masking is not encryption: the masking key travels in the frame. Use wss for TLS protection.

[2] [4]

Queued is not saved

send() queues data for transmission. bufferedAmount reports application bytes queued but not yet transmitted to the network; zero does not prove that the server saved a post.

For example, an application might reply with post 42 saved only after storing that post. This explicitly defined reply would provide confirmation that a locally empty send buffer cannot. A full send buffer can also cause automatic closure. Sending while the connection is still CONNECTING throws; sending after it is CLOSING or CLOSED silently discards the data.

[4] [5]

Keep the work bounded

The conventional browser WebSocket API does not provide automatic backpressure: it cannot automatically make an incoming producer wait for a slow consumer. Excess incoming messages can overwhelm memory or processing.

An application can define limits, reduce the update rate, or request a current snapshot instead of processing every outdated update. Those are design choices. Watching the outgoing bufferedAmount can help regulate sends, but does not solve the incoming processing problem by itself.

[1] [4]
THE IDEA TO KEEP

WebSocket carries messages; the application defines recovery, workload limits, and what success means.

ONE THING TO WATCH OUT FOR

bufferedAmount equal to zero proves the server saved my change.

It describes the sending buffer, not the receiving application. Saving requires its own defined confirmation.

A QUICK CHECK, NOT A TEST
send() returned without throwing on an open socket. Which conclusion is justified?
Show the explanation

This does not confirm that the receiving application processed or saved the change. send() concerns transmission, not processing or persistence at the server. The application must define its success response and recovery behavior.

Progress is saved only in this browser.

KEEP EXPLORINGHTTP
Was this explanation clear?
Report a mistake by email
GO STRAIGHT TO THE SOURCE

Keep asking. Check the details.

Source-checked using AI (OpenAI Codex) on . The checks covered sources, examples, and quizzes for this article version.

No human review is claimed. AI review can miss errors; check the original sources below.

  1. MDN — WebSocket API: two-way communication and backpressure
  2. RFC 6455 — The WebSocket Protocol: handshake, framing, recovery and TLS
  3. MDN — Writing WebSocket client applications: connection lifecycle
  4. WHATWG — WebSockets Standard: browser API and bufferedAmount
  5. MDN — WebSocket send(): queueing data and connection states
How explanations are prepared