web api instance method
WebSocket: send() method
Available in workers
The WebSocket.send() method enqueues the specified data
to be transmitted to the server over the WebSocket connection, increasing the value of
bufferedAmount by the number of bytes needed to contain the data. If the
data can’t be sent (for example, because it needs to be buffered but the buffer is
full), the socket is closed automatically.
The browser will throw an exception if you call send() when the connection is in the CONNECTING state. If you call send() when the connection is in the CLOSING or CLOSED states, the browser will silently discard the data.
Syntax
send(data)
Parameters
data- : The data to send to the server. It may be one of the following types:
string- : A text string. The string is added to the buffer in UTF-8 format, and the value
of
bufferedAmountis increased by the number of bytes required to represent the UTF-8 string.
- : A text string. The string is added to the buffer in UTF-8 format, and the value
of
ArrayBuffer- : You can send the underlying binary data used by a typed array object; its binary
data contents are queued in the buffer, increasing the value of
bufferedAmountby the requisite number of bytes.
- : You can send the underlying binary data used by a typed array object; its binary
data contents are queued in the buffer, increasing the value of
Blob- : Specifying a
Blobenqueues the blob’s raw data to be transmitted in a binary frame (thetypeis ignored). The value ofbufferedAmountis increased by the byte size of that raw data.
- : Specifying a
TypedArrayor aDataView- : You can send any JavaScript 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.
- : You can send any JavaScript typed array object as a binary frame;
its binary data contents are queued in the buffer,
increasing the value of
- : The data to send to the server. It may be one of the following types:
Return value
None (undefined).
Exceptions
InvalidStateErrorDOMException- : Thrown if
WebSocket.readyStateisCONNECTING.
- : Thrown if
Specifications
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.