Web APIs

1 Server-sent events

2 WebSockets

2.2 WebSocket Server

An application listening on any port of a TCP server that then follows a specific protocol.

To begin a client will send a standard HTTP request

GET /chat HTTP/1.1
Host: example.com:8000
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13

the server responds with

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

Either the client or the server can choose to send a message (frame) at any time. Data transmitted from client to the server is masked using XOR encryption (with a 32-bit key).

credit to MDN