WebSocket: send() method
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Note: This feature is available inWeb Workers.
TheWebSocket.send() method enqueues the specified datato be transmitted to the server over the WebSocket connection, increasing the value ofbufferedAmount by the number of bytes needed to contain the data. If thedata can't be sent (for example, because it needs to be buffered but the buffer isfull), the socket is closed automatically.The browser will throw an exception if you callsend() when the connection is in theCONNECTING state. If you callsend() when the connection is in theCLOSING orCLOSED states, the browser will silently discard the data.
In this article
Syntax
send(data)Parameters
dataThe data to send to the server. It may be one of the following types:
stringA text string. The string is added to the buffer in UTF-8 format, and the valueof
bufferedAmountis increased by the number of bytes required torepresent the UTF-8 string.ArrayBufferYou can send the underlying binary data used by a typed array object; its binarydata contents are queued in the buffer, increasing the value of
bufferedAmountby the requisite number of bytes.BlobSpecifying a
Blobenqueues the blob's raw data to be transmitted ina binary frame (theBlob.typeis ignored).The value ofbufferedAmountis increased by the byte size of that raw data.TypedArrayor aDataViewYou can send anyJavaScript typed array object as a binary frame;its binary data contents are queued in the buffer,increasing the value of
bufferedAmountby the requisite number of bytes.
Return value
None (undefined).
Exceptions
InvalidStateErrorDOMExceptionThrown if
WebSocket.readyStateisCONNECTING.
Specifications
| Specification |
|---|
| WebSockets> # ref-for-dom-websocket-send①> |