Web APIs

WebSocket.prototype.send()

Try the free JavaScript compiler

Queues supported text or binary data; returns without awaiting acknowledgement.

Formal Syntax & Structure

javascript syntaxjavascript
socket.send(data)

Parameters & Attributes

NameTypeDescription
datastring | Blob | BufferSourceText 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();}})()
JavaScript Reference