What are WebSockets?
Useful first: HTTP · optional, not a requirement
Choose your depth
Stay with the essentials, or look closer.
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]WebSocket gives the browser and server a continuing two-way message channel.
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.
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.
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.
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]Wait for open, use wss for protected transport, and design what happens after close.
Once a WebSocket opens, it stays connected forever.
Connections can close or fail. Reconnection and recovery of missing application data require explicit handling.
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.
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.
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.
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.
WebSocket carries messages; the application defines recovery, workload limits, and what success means.
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.
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.
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.
- MDN — WebSocket API: two-way communication and backpressure
- RFC 6455 — The WebSocket Protocol: handshake, framing, recovery and TLS
- MDN — Writing WebSocket client applications: connection lifecycle
- WHATWG — WebSockets Standard: browser API and bufferedAmount
- MDN — WebSocket send(): queueing data and connection states