WebSocket is a different protocol from HTTP. Both protocols are located at layer 7 in the OSI model and depend on TCP at layer 4. Although they are different, RFC 6455 states that WebSocket "is designed to work over HTTP ports 80 and 443 as well as to support HTTP proxies and intermediaries" thus making it compatible with the HTTP protocol. To achieve compatibility, the WebSocket handshake uses the HTTP Upgrade header to change from the HTTP protocol to the WebSocket protocol.
Sockets are used for real-time communication. Instead of requests that transfer data once, sockets provide a sustained connection to transfer data without a delay. Keep in mind that for this to work on our servers you should first notify the DevOps Team as it can be complicated based on the project.
This technique can get tricky, discuss with other developers that have experience with it when needed.
A websocket can be used to provide realtime server to client communications. Please note that it is not possible to connect a client to a client.
A websocket is an alternative for what used to be polling an HTTP server. A websocket just opens one connection instead of creating new connections for every update.
No
A websocket itself is not capable of loadbalancing. A reverse proxy makes it possible to do so but it's not recommended.
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
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat
For a quick and dirty test you can just CURL the socket url like this:
curl -v -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: example.com" -H "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==" -H "Sec-WebSocket-Version: 13" -k --http1.0 https://example.com/socket
Last modified | Tuesday, February 2, 2021, 3:09:34 PM UTC |
Last author | Glenn de Haan |
Commit ID | b908c1b |