WebSocket Connection
A WebSocket Connection is a computer communication connection that follows a WebSockets protocol.
- Context:
- It can range from being a Short-Lived WebSocket Connection to being a Long-Lived WebSocket Connection.
- It can involve a client sending a WebSocket handshake request, for which a server can return a WebSocket handshake response.
- …
- Counter-Example(s):
- a HTTP Connection (using a HTTP Protocol).
- a UNIX-style Socket.
- a gRPC Connection (using a gRPC Protocol).
- See: Websocket Health Check, Full-Duplex, Transmission Control Protocol.
References
2020
- (Wikipedia, 2020) ⇒ https://en.wikipedia.org/wiki/WebSocket#Protocol_handshake
To establish a WebSocket connection, the client sends a WebSocket handshake request, for which the server returns a WebSocket handshake response, as shown in the example below.[1]
Client request (just like in HTTP, each line ends with \r\n
and there must be an extra blank line at the end):
<source lang="http"> GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw == Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 13 Origin: http://example.com </source>
Server response:
<source lang="http"> HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk = Sec-WebSocket-Protocol: chat </source>
The handshake starts with an HTTP request/response, allowing servers to handle HTTP connections as well as WebSocket connections on the same port. Once the connection is established, communication switches to a bidirectional binary protocol which does not conform to the HTTP protocol.
In addition to Upgrade
headers, the client sends a Sec-WebSocket-Key
header containing base64-encoded random bytes, and the server replies with a hash of the key in the Sec-WebSocket-Accept
header. This is intended to prevent a caching proxy from re-sending a previous WebSocket conversation,[2] and does not provide any authentication, privacy, or integrity. The hashing function appends the fixed string 258EAFA5-E914-47DA-95CA-C5AB0DC85B11
(a GUID) to the value from Sec-WebSocket-Key
header (which is not decoded from base64), applies the SHA-1 hashing function, and encodes the result using base64.[3]
Once the connection is established, the client and server can send WebSocket data or text frames back and forth in full-duplex mode. The data is minimally framed, with a small header followed by payload.[4] WebSocket transmissions are described as "messages", where a single message can optionally be split across several data frames. This can allow for sending of messages where initial data is available but the complete length of the message is unknown (it sends one data frame after another until the end is reached and marked with the FIN bit). With extensions to the protocol, this can also be used for multiplexing several streams simultaneously (for instance to avoid monopolizing use of a socket for a single large payload).[5]
- ↑ Template:Cite IETF
- ↑ "Main Goal of WebSocket protocol". IETF. https://trac.tools.ietf.org/wg/hybi/trac/wiki/FAQ. Retrieved 25 July 2015. "The computation [...] is meant to prevent a caching intermediary from providing a WS-client with a cached WS-server reply without actual interaction with the WS-server."
- ↑ Template:Cite IETF
- ↑ Template:Cite IETF
- ↑ Template:Cite IETF