Web/API/WebSocket/send

From Get docs

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.

Syntax

WebSocket.send("Hello server!");

Parameters

data
The data to send to the server. It may be one of the following types:
USVString
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 of bufferedAmount 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 thrown

INVALID_STATE_ERR
The connection is not currently OPEN.
SYNTAX_ERR
The data is a string that has unpaired surrogates.

Note: Gecko's implementation of the send() method differs somewhat from the specification in Gecko 6.0; Gecko returns a boolean indicating whether or not the connection is still open (and, by extension, that the data was successfully queued or transmitted); this is corrected in Gecko 8.0.

As of Gecko 11.0, support for ArrayBuffer is implemented but not Blob data types.


Specifications

Specification Status Comment
HTML Living StandardThe definition of 'WebSocket: send' in that specification. Living Standard Initial definition

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
send Chrome

Full support 4

Edge

Full support 12

Firefox Full support 18

Notes'

Full support 18

Notes'

Notes' See bug 775368. No support 11 — 18

Notes'

Notes' Only parameter of type ArrayBuffer and String supported. No support 8 — 11

Notes'

Notes' Only parameter of type String supported. No support 4 — 8

Notes'

Notes' Only parameter of type String supported. Returns boolean.

IE

Full support 10

Opera

Full support 12.1

Safari

Full support 5

WebView Android

Full support ≤37

Chrome Android

Full support 18

Firefox Android Full support 18

Notes'

Full support 18

Notes'

Notes' See bug 775368. No support 14 — 18

Notes'

Notes' Only parameter of type ArrayBuffer and String supported. No support 8 — 14

Notes'

Notes' Only parameter of type String supported. No support 4 — 8

Notes'

Notes' Only parameter of type String supported. Returns boolean.

Opera Android

Full support 12.1

Safari iOS

Full support 4.2

Samsung Internet Android

Full support 1.0

Legend

Full support  
Full support
See implementation notes.'
See implementation notes.