WebSocket messages and send buffering
Quick answer
WebSocket sends text or binary messages. Incoming message events contain data, not a guarantee of valid application records.
How it works
send(data) queues a string, Blob, ArrayBuffer or array-buffer view. Text arrives as a string; binary reception is controlled by binaryType (blob or arraybuffer). bufferedAmount measures queued application bytes not yet transmitted; it is not an acknowledgement and does not limit inbound traffic. The classic WebSocket API has no automatic receive backpressure. Use bounded queues, message size limits and explicit application acknowledgements when required. Decode JSON inside try/catch and validate the fields before applying them.
Local examples
Run in the isolated projects/realtime-lab development origin (see README). NOT RUN in local validation; expected result is instructional.
(async () => {const url=new URL("/ws",location.href);url.protocol=location.protocol==="https:"?"wss:":"ws:";const ws=new WebSocket(url);try{await new Promise((ok,no)=>{ws.onopen=ok;ws.onerror=()=>no(new Error("connection failed"));});const answer=new Promise(ok=>ws.onmessage=e=>ok(e.data));ws.send("hello");console.log(await answer);}finally{ws.close();}})()Expected console output (not executed locally):
helloThe local echo service returns the sent text; send itself returns no acknowledgement.
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const canSend=(state,queued,bytes,limit)=>state===WebSocket.OPEN&&queued+bytes<=limit;console.log(JSON.stringify([canSend(1,8,2,10),canSend(1,8,3,10),canSend(0,0,1,10)]));})()Expected console output:
[true,false,false]The simulated policy permits only open connections with sufficient capacity.
Common mistakes
Never assume send waits for delivery, or render decoded user strings as HTML. A growing bufferedAmount is a reason to slow or reject additional sends.
Practice
Add a bounded send policy to the echo lab and distinguish queued, echoed and application-acknowledged messages.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
WebSocket messages and send buffering — Knowledge Check
Apply the concept and reason about local example output.
What does WebSocket.bufferedAmount measure?
What should happen after receiving a JSON message?
Does the classic WebSocket API automatically provide receive backpressure?
What is the expected console output of WebSocket messages and send buffering example 1?
(async () => {const url=new URL("/ws",location.href);url.protocol=location.protocol==="https:"?"wss:":"ws:";const ws=new WebSocket(url);try{await new Promise((ok,no)=>{ws.onopen=ok;ws.onerror=()=>no(new Error("connection failed"));});const answer=new Promise(ok=>ws.onmessage=e=>ok(e.data));ws.send("hello");console.log(await answer);}finally{ws.close();}})()What is the expected console output of WebSocket messages and send buffering example 2?
(async () => {const canSend=(state,queued,bytes,limit)=>state===WebSocket.OPEN&&queued+bytes<=limit;console.log(JSON.stringify([canSend(1,8,2,10),canSend(1,8,3,10),canSend(0,0,1,10)]));})()