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.

To construct a WebSocket, use the WebSocket() constructor.

Constructor

WebSocket(url[, protocols])
Returns a newly created WebSocket object.

Properties

WebSocket.binaryType
The binary data type used by the connection.
WebSocket.bufferedAmount Read only
The number of bytes of queued data.
WebSocket.extensions Read only
The extensions selected by the server.
WebSocket.onclose
An event listener to be called when the connection is closed.
WebSocket.onerror
An event listener to be called when an error occurs.
WebSocket.onmessage
An event listener to be called when a message is received from the server.
WebSocket.onopen
An event listener to be called when the connection is opened.
WebSocket.protocol Read only
The sub-protocol selected by the server.
WebSocket.readyState Read only
The current state of the connection.
WebSocket.url Read only
The absolute URL of the WebSocket.

Methods

WebSocket.close([code[, reason]])
Closes the connection.
WebSocket.send(data)
Enqueues data to be transmitted.

Example

// 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);
});

Specifications

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

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support Yes Yes

111

7 — 11 Moz 2

4 — 62

? Yes Yes
WebSocket() constructor Yes ?

7

4 — 73

? Yes ?
binaryType Yes ? Yes ? Yes ?
bufferedAmount Yes ? Yes ? Yes ?
close Yes ?

8

4 — 84

? Yes ?
extensions Yes ?8 ? Yes ?
onclose Yes ? Yes ? Yes ?
onerror Yes ? Yes ? Yes ?
onmessage Yes ? Yes ? Yes ?
onopen Yes ? Yes ? Yes ?
protocol Yes ? Yes ? Yes ?
readyState Yes ? Yes ? Yes ?
send Yes ?

185

11 — 186

8 — 117

4 — 88

? Yes ?
url Yes ? Yes ? Yes ?
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support ? ? Yes

141

7 — 14 Moz 2

? Yes ?
WebSocket() constructor ? Yes ?7 ? ? ?
binaryType ? Yes ? Yes ? ? ?
bufferedAmount ? Yes ? Yes ? ? ?
close ? Yes ? Yes ? ? ?
extensions ? Yes ?8 ? ? ?
onclose ? Yes ? Yes ? ? ?
onerror ? Yes ? Yes ? ? ?
onmessage ? Yes ? Yes ? ? ?
onopen ? Yes ? Yes ? ? ?
protocol ? Yes ? Yes ? ? ?
readyState ? Yes ? Yes ? ? ?
send ? Yes ? Yes ? ? ?
url ? Yes ? Yes ? ? ?

1. See bug 695635.

2. Message size limited to 16 MB (see bug 711205).

3. Parameter protocols not supported.

4. Parameters not supported, see bug 674716.

5. See bug 775368.

6. Only parameter of type ArrayBuffer and String supported.

7. Only parameter of type String supported.

8. Only parameter of type String supported. Returns boolean.

See also