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 in Web 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

js
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 Blob.type is ignored). The value of bufferedAmount is increased by the byte size of that raw data.

TypedArray or a DataView

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.

Return value

None (undefined).

Exceptions

InvalidStateError DOMException

Thrown if WebSocket.readyState is CONNECTING.

Specifications

Specification
WebSockets
# ref-for-dom-websocket-send①

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
send
Chrome – Full support
Chrome 5 (Release date: 2010-05-25)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – No support
Firefox 7 – 7 (Release date: 2011-09-27)
footnote Only parameter of type String supported. Returns boolean.
Firefox – No support
Firefox 8 – 10 (Release date: 2011-11-08)
footnote Only parameter of type String supported.
Firefox – No support
Firefox 11 – 17 (Release date: 2012-03-13)
footnote Only parameter of type ArrayBuffer and String supported.
Firefox – Full support
Firefox 18 (Release date: 2013-01-08)
footnote
footnote See bug 775368
Opera – Full support
Opera 12.1 (Release date: 2012-11-20)
footnote Full support
Safari – Full support
Safari 5 (Release date: 2010-06-07)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – No support
Firefox for Android 7 – 7 (Release date: 2011-09-27)
footnote Only parameter of type String supported. Returns boolean.
Firefox for Android – No support
Firefox for Android 8 – 10 (Release date: 2011-11-08)
footnote Only parameter of type String supported.
Firefox for Android – No support
Firefox for Android 14 – 17 (Release date: 2012-06-26)
footnote Only parameter of type ArrayBuffer and String supported.
Firefox for Android – Full support
Firefox for Android 18 (Release date: 2013-01-08)
footnote
footnote See bug 775368
Opera Android – Full support
Opera Android 12.1 (Release date: 2012-10-09)
footnote Full support
Safari on iOS – Full support
Safari on iOS 4.2 (Release date: 2010-11-22)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 4.2 (Release date: 2010-11-22)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1.4 (Release date: 2020-09-13)
footnote Full support
Node.js – Full support
Node.js 22 (Release date: 2024-04-24)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.
Has more compatibility info.