We're looking for a person or people to help audit MDN to find places we could speed up. Is this you or someone you know? Check out the RFP: https://mzl.la/2IHcMiE

The WebSocket object provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection.

The WebSocket constructor accepts one required and one optional parameter:

WebSocket WebSocket(
  in DOMString url,
  in optional DOMString protocols

WebSocket WebSocket(
  in DOMString url,
  in optional DOMString[] protocols
The URL to which to connect; this should be the URL to which the WebSocket server will respond.
protocols Optional
Either a single protocol string or an array of protocol strings. These strings are used to indicate sub-protocols, so that a single server can implement multiple WebSocket sub-protocols (for example, you might want one server to be able to handle different types of interactions depending on the specified protocol). If you don't specify a protocol string, an empty string is assumed.

The constructor can throw exceptions:

The port to which the connection is being attempted is being blocked.

Method overview

void close(in optional unsigned long code, in optional DOMString reason);
void send(in DOMString data);


Attribute Type Description
binaryType DOMString A string indicating the type of binary data being transmitted by the connection. This should be either "blob" if DOM Blob objects are being used or "arraybuffer" if ArrayBuffer objects are being used.
bufferedAmount unsigned long The number of bytes of data that have been queued using calls to send() but not yet transmitted to the network. This value resets to zero once all queued data has been sent. This value does not reset to zero when the connection is closed; if you keep calling send(), this will continue to climb. Read only
extensions DOMString The extensions selected by the server. This is currently only the empty string or a list of extensions as negotiated by the connection.
onclose EventListener An event listener to be called when the WebSocket connection's readyState changes to CLOSED. The listener receives a CloseEvent named "close".
onerror EventListener An event listener to be called when an error occurs. This is a simple event named "error".
onmessage EventListener An event listener to be called when a message is received from the server. The listener receives a MessageEvent named "message".
onopen EventListener An event listener to be called when the WebSocket connection's readyState changes to OPEN; this indicates that the connection is ready to send and receive data. The event is a simple one with the name "open".
protocol DOMString A string indicating the name of the sub-protocol the server selected; this will be one of the strings specified in the protocols parameter when creating the WebSocket object.
readyState unsigned short The current state of the connection; this is one of the Ready state constants. Read only.
url DOMString The URL as resolved by the constructor. This is always an absolute URL. Read only.


Ready state constants

These constants are used by the readyState attribute to describe the state of the WebSocket connection.

Constant Value Description
CONNECTING 0 The connection is not yet open.
OPEN 1 The connection is open and ready to communicate.
CLOSING 2 The connection is in the process of closing.
CLOSED 3 The connection is closed or couldn't be opened.



Closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSED, this method does nothing.

void close(
  in optional unsigned short code,
  in optional DOMString reason


code Optional
A numeric value indicating the status code explaining why the connection is being closed. If this parameter is not specified, a default value of 1005 is assumed. See the list of status codes on the CloseEvent page for permitted values.
reason Optional
A human-readable string explaining why the connection is closing. This string must be no longer than 123 bytes of UTF-8 text (not characters).

Exceptions thrown

An invalid code was specified.
The reason string is too long or contains unpaired surrogates.

Note: In Gecko, this method didn't support any parameters prior to Gecko 8.0 (Firefox 8.0 / Thunderbird 8.0 / SeaMonkey 2.5).


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.

void send(
  in USVString data

void send(
  in ArrayBuffer data

void send(
  in Blob data

void send(
  in ArrayBufferView data


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

The connection is not currently OPEN.
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.


// Create WebSocket connection.
const socket = new WebSocket('ws://localhost:8080');

// Connection opened
socket.addEventListener('open', function (event) {
    socket.send('Hello Server!');

// Listen for messages
socket.addEventListener('message', function (event) {
    console.log('Message from server ', event.data);


Specification Status Comment
HTML Living Standard
The definition of 'WebSocket' in that specification.
Living Standard Initial definition

Browser compatibility

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) 4.0 (2.0)[1] (Yes) (Yes) (Yes)
Sub-protocol support ? ? 6.0 (6.0) ? ? ?
Feature Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? (Yes) 7.0 (7.0)[1] ? ? (Yes)
Sub-protocol support ? ? 7.0 (7.0) ? ? ?

[1] Starting in Gecko 6.0 (Firefox 6.0 / Thunderbird 6.0 / SeaMonkey 2.3), the constructor is prefixed; you will need to use MozWebSocket(): var mySocket = new MozWebSocket("http://www.example.com/socketserver");

The extensions attribute was not supported in Gecko until Gecko 8.0.

Prior to Gecko 11.0 (Firefox 11.0 / Thunderbird 11.0 / SeaMonkey 2.8), outbound messages sent using the send() method were limited to 16 MB. They can now be up to 2 GB in size.

See also