Web APIs
WebSocket.prototype.send()
Queues supported text or binary data; returns without awaiting acknowledgement.
Formal Syntax & Structure
javascript syntaxjavascript
socket.send(data)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| data | string | Blob | BufferSource | Text or binary payload. |
Return Value
undefined
Queues supported text or binary data; returns without awaiting acknowledgement.
Working Example
Example Snippetjavascript
(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();}})()