Functional summary
- Bidirectiona message oriented streaming (client - server)
- Text + binary data
- Closest thing to a raw network socket in the browser
Additioanl services
- Connection negotiation
- Interoperatibility with existing HTTP infra
- Message-oriented communication and efficient message framing
- Subprotcool negotiation and extensivility
The WebSocket API
Small + simple!
Example connection initiation
- Mostly just need URL of websocket resources and a few application callabacks
The general order
- New Websocket connection
- Optional callback (when connection error)
- Optional callback (when connection end)
- Optional callback (when connection established)
- Client initiated message to the server
- Callback funtion for each new message
- Invoke binary or text processing logic for received message
Pretty much self-explanatory
WS and WSS URL Schemes
Looking at API, uses
ws
for plain textwss
when need encrypted channel
Why not use http
?
- WebSocket is primarily for brwoser - server communication cchannels, but can be used outside browser and negotiated via non-HTTP exchange
Receiving Text and Binary data
WebSocket doesn’t need to worry about buffering parsing, reconstructing received data.
onmessage
callback is only called when the entire message is availale on the client
When browser receives new message, automatically converts it to a _ then passes directly to the application
- DOMString object for text-based data
- Blob object for binary data
- Can also convert binary data to ArrayBuffer instead
Note: Blob is a file-ike object of immutable raw data. ArrayBuffer is likely better if you want to modify and whatnot.
Sending Text and Binary Data
Once you establish websocket connection client can send / receive UTF-8 a d binary emessages at will
Example code
- Send UTF-8 encoded text message
- Send UT-8 encoded JSOn paload
- Send Arraybuffer conents as binary payload
- Send the ArrayBufferView contents as binary payload
- Send the Blob contents as binary payload
The websocket api accepts DOMstring object (encoded as UTF-9) or (for binary transfer) either Arraybuffer, ArrayBufferView, Blob
send()
is asynchornous
- Data is queued vy clienta nd function returns immediately- so data is not immediatley sent!!
- But, you can monitor how much data has been queued by the browser
- Subscribe to update, check amount of buffered data on client, send update if buffer is empty
Why bother checking if previous messages have ben drained from the lcient’s buffer?
- Help avoid head of line blocking
Subprotocol Negotiation
WebSocket makes no assumption about format of each message
- Just a single bit tracks (text or binary?)
And no additional metadata like HTTP headers
- What to do if you want additional metadata? You’ll need to agree to implement a subprotocol
Luckily, WebSocket offers a subprotocol negotiation API
- Clients can advertise which protocols it supports to the server as part of its initial connection handshake.
How?
Then to check which ones the server chose
WebSocket Protocol
2 high level components
- Opening HTTP handshake
- Negotiates paramters of ocnnection
- Binary message framing mechanism
- Low overhead message based delivery of text / binary data
Binary Framing Layer
TODO
WebSocket Use Cases and Performance
Again, the WebSocke tAPI provides a simple interface for bidirectional message-oriented stremaing of text/binary data between client and server.
Super simple set up:
- WebSocket URL
- A few JS callback functions
- Up and running
And WebSocket Protocol
- Cusotm application protocols enabled!
Still important to understand the implementation details to optimize our applications!