Short answer
Use a WebSocket when both sides must keep sending on one open connection, and use an HTTP request when the client can ask once and wait. RFC 6455 defines the socket as an upgrade followed by framed messages in both directions. MDN defines HTTP as separate requests and responses, and calls the protocol stateless. Neither document prints a latency winner.
Last verified against the sources listed below.
Key takeaways
- The browser opens both. HTTP stays a request and a response. A socket stays open both ways.
- RFC 6455 defaults ws to port 80 and wss to port 443, then upgrades from HTTP.
- The client must mask every frame it sends. The server must not mask frames it sends.
- MDN calls HTTP stateless. Cookies can still tie requests into a session.
- A WebSocket has no backpressure. MDN points to WebTransport when that matters.
The choice in websocket vs rest api is between a channel that stays open in both directions and a request the client sends when it wants a resource. Pick the socket for a live exchange. Pick the request when each question can stand alone and wait for its answer.
Both patterns are part of back end development. The comparison below uses RFC 6455 and MDN, and it does not invent a speed gap the specs do not print.
Which one wins?
The socket wins when both sides talk without a new request. The HTTP call wins when the client is fetching or submitting a resource.
RFC 6455 says the WebSocket Protocol gives two-way communication between client code and a remote host that has opted in. It is an opening handshake, then message frames, layered over TCP. The stated goal is browser applications that need two-way communication without opening multiple HTTP connections, the pattern of XMLHttpRequest or long polling.
MDN’s HTTP overview describes a different shape. Clients and servers exchange individual messages, not a stream. The browser sends a request.
The server sends a response. The browser is always the side that starts that request.
| WebSocket | HTTP request, including a REST-style call | |
|---|---|---|
| Who opens | The client sends the opening handshake | The user agent opens each request |
| Direction | Both sides can send after the handshake | A request, then a response to that request |
| State | One connection moves from connecting to open until it closes | HTTP itself is stateless. Cookies can add a session |
| Typical job | Live two-way messages without a new HTTP request per update | Fetch a document, submit a form, read or write one resource |
MDN’s REST glossary says REST is a group of constraints for distributed systems, and that a resource moves through standard client and server interactions. It also says many HTTP APIs are called REST even when they skip some constraints. In this article, REST API means that HTTP style of call, not a promise that every constraint is met.
Who opens the channel?
The client opens both. Only the socket turns the open into a long-lived upgrade.
To establish a WebSocket, RFC 6455 says the client opens a connection and sends a handshake. The connection starts in the CONNECTING state. The client supplies host, port, resource name, and a secure flag, plus any subprotocols and extensions. A browser also supplies the origin.
The handshake is an HTTP Upgrade request. The WebSockets Standard sets Sec-WebSocket-Version to 13 on that request. IANA lists 13 as the standard version.
MDN lists the related headers, including Sec-WebSocket-Key, Sec-WebSocket-Accept, Sec-WebSocket-Version, Sec-WebSocket-Protocol, and Sec-WebSocket-Extensions. The browser adds the key. You do not hand-write it in page script.
An HTTP request does not upgrade. MDN says the client opens a TCP connection, sends one message, reads the response, and then closes or reuses the connection. A request line carries a method such as GET, POST, OPTIONS, or HEAD, the path, and the protocol version, then headers and sometimes a body.
The response carries a status code, a status text, headers, and an optional body. Before HTTP/2, those messages are human-readable text. HTTP/2 puts them in frames, and the semantics stay the same.
Default ports on the socket side are printed in the RFC. ws uses port 80 when the port is omitted. wss uses port 443. wss is the secure scheme.
Which way does data move?
After the handshake, either side can send a frame. An HTTP call waits for the response to the request that was just sent.
RFC 6455 says the result of a successful handshake is a two-way channel where each side can send data independently. Data travels in frames. Opcodes distinguish text frames and binary frames, plus control frames for close, ping, and pong.
The client must mask every frame it sends. The server must not mask. A masked frame from the server, or an unmasked frame from the client, is a protocol error, status code 1002, and the other side closes.
HTTP does not give the server a matching right to start a request. MDN says the browser is never the server, though some mechanisms simulate server-initiated messages. One of those mechanisms is on the same page: server-sent events, a one-way HTTP stream from server to client through EventSource.
That is still not a WebSocket. The client is not sending a parallel series of frames back on that stream.
Polling is the pattern the RFC is trying to avoid for two-way apps: repeated HTTP requests, or a long-held request, so the client can discover that the server has something to say. A socket removes that extra request. It does not remove the need for the application to understand each message.
Does either side keep state?
HTTP is stateless at the protocol layer. A WebSocket keeps a connection open until someone closes it.
MDN’s phrase is direct: there is no link between two requests carried on the same connection. Shopping carts and signed-in pages still exist because cookies, added through headers, let the server treat a series of requests as one session. The protocol did not grow a memory of its own. The application added one.
A WebSocket connection is a piece of state by itself. It begins CONNECTING, the handshake runs, and later a close handshake shuts it down. While it is open, both sides may send.
MDN warns about a limit here. The WebSocket interface has no backpressure. If messages arrive faster than the page can handle them, the browser may buffer until memory fills, burn CPU until the page stalls, or both.
When backpressure matters, MDN points to the WebTransport API, which also offers unidirectional streams, out-of-order delivery, and unreliable datagrams. WebSocketStream is noted as an earlier, non-standard attempt.
There is a page-lifecycle cost too. MDN says a page with an open socket may not enter the back or forward cache. Close the socket when the visit is over.
How those bytes cross the network, under the application messages, is the subject of how the internet works. HTTP and the socket handshake sit at the application layer. TCP, which both rely on for a reliable stream, sits underneath.
When is a live channel worth it?
Use the socket when a new HTTP request per update would be the product. Use the request when the client can ask and then stop.
A live channel fits a chat message, a multiplayer move, or a dashboard tick that the server must push and the page must also send, without waiting to open another request. That is the job RFC 6455 names. The cost is an open connection, masking rules, a close handshake, and the backpressure limit above.
A REST-style call fits a read of one resource, a form submit, or any job that maps to a method and a path. MDN’s Fetch API is the usual browser tool for that call, replacing XMLHttpRequest. The server stays free to answer and forget, unless a cookie builds a session.
Caches, proxies, and authentication headers are part of the HTTP design MDN lists. They are not part of the framed channel that exists only after an upgrade.
A drawing on the page is a separate job from the channel that might deliver it. The SVG to JSX converter turns an SVG into a React component, which is JavaScript the page can render. It does not choose your protocol.
What can go wrong?
The usual failures are a half-closed design: polling that should have been a socket, or a socket that should have been one request.
Leave a socket open and the page may miss the back or forward cache. Let the server outrun the client and, per MDN, memory or CPU can spike because there is no backpressure. Forget to mask, and RFC 6455 requires the server to close.
Forget that HTTP is stateless, and a second request will not remember the first unless you send the cookie or the token again.
Call an HTTP service REST and you may be using the colloquial name MDN warns about. The service can still be a perfectly good request and response API. It has not automatically picked up every REST constraint.
Match the name to the behavior you actually implemented: a resource and a response, or an upgraded channel and frames.
Frequently asked questions
What is a REST API if it is only HTTP?
MDN's glossary says REST is a set of architecture constraints for distributed systems. A resource, such as a document, moves through familiar client and server interactions. An HTTP API is often called REST even when it misses some constraints. For a beginner, MDN says to treat that name as an HTTP service you can call with ordinary web libraries. The label alone does not prove every constraint is met. Check the methods and status codes the service actually implements.
When would server-sent events fit instead?
MDN describes server-sent events as a one-way HTTP service. The browser opens it with EventSource, and the server pushes events that become Event objects. The client does not send a matching series of frames back on that channel. Use it when the page only needs to listen. Use a WebSocket when the page must also send without opening a fresh request for every update. The socket still starts life as an HTTP upgrade, which server-sent events never do.
Why must the client mask frames?
RFC 6455 says the client must mask every frame it sends, with or without TLS. An unmasked client frame makes the server close the connection. IANA's close-code registry names 1002 a protocol error, which is the code the RFC allows for that failure. A server must not mask what it sends. A masked server frame makes the client close. The masking key is present only when the mask bit is set. Opcode 1 is a text frame and opcode 2 is a binary frame in that same registry.
What happens to the back or forward cache?
MDN notes that a page with an open WebSocket may not be added to the back or forward cache. The practical habit is to close the socket when the person is done with the page, so a later return can restore the page from that cache. Leaving the socket open keeps a live channel the cache is not prepared to freeze. This is a page-lifecycle concern, separate from the question of whether the channel was the right design for the data.
Which URI schemes does the protocol register?
RFC 6455 defines ws and wss. The port is optional. The default for ws is port 80, and the default for wss is port 443. The scheme is secure when it matches wss, ignoring case. Fragment identifiers are meaningless on these URIs and must not be used. A hash that is not a fragment has to be escaped as percent two three. The handshake itself is still HTTP: the client sends Upgrade websocket and Connection Upgrade, and the example in the RFC includes Sec-WebSocket-Version 13.
Sources
- The WebSocket Protocol · IETF, 2011
- WebSocket API · MDN, 2026
- Overview of HTTP · MDN, 2026
- REST · MDN, 2026
- WebSockets Standard · WHATWG, 2026
- WebSocket Protocol Registries · IANA, 2026