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.
WebSocket.send()
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
bufferedAmount
is increased by the number of bytes required to represent the UTF-8 string. -
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
bufferedAmount
by the requisite number of bytes. -
Blob
-
Specifying a
Blob
enqueues the blob's raw data to be transmitted in a binary frame. The value ofbufferedAmount
is increased by the byte size of that raw data. -
ArrayBufferView
-
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
bufferedAmount
by the requisite number of bytes.
Exceptions
-
InvalidStateError
DOMException
-
Thrown if
WebSocket.readyState
isCONNECTING
.
Specifications
Specification |
---|
WebSockets Standard # ref-for-dom-websocket-send① |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
send |
4
|
12
|
18
See
bug 775368.
11-18
Only parameter of type
ArrayBuffer and
String supported.
8-11
Only parameter of type
String supported.
7-8
Only parameter of type
String supported. Returns
boolean .
|
10
|
12.1
|
5
|
4.4
|
18
|
18
See
bug 775368.
14-18
Only parameter of type
ArrayBuffer and
String supported.
8-14
Only parameter of type
String supported.
7-8
Only parameter of type
String supported. Returns
boolean .
|
12.1
|
4.2
|
1.0
|
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send