W3cubDocs

/Web APIs

WebSocket

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.

Note: This feature is available in Web Workers

EventTarget WebSocket

Constructor

WebSocket()

Returns a newly created WebSocket object.

Instance 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.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.

Instance methods

WebSocket.close()

Closes the connection.

WebSocket.send()

Enqueues data to be transmitted.

Events

Listen to these events using addEventListener() or by assigning an event listener to the oneventname property of this interface.

close

Fired when a connection with a WebSocket is closed. Also available via the onclose property

error

Fired when a connection with a WebSocket has been closed because of an error, such as when some data couldn't be sent. Also available via the onerror property.

message

Fired when data is received through a WebSocket. Also available via the onmessage property.

open

Fired when a connection with a WebSocket is opened. Also available via the onopen property.

Examples

js

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

// Connection opened
socket.addEventListener("open", (event) => {
  socket.send("Hello Server!");
});

// Listen for messages
socket.addEventListener("message", (event) => {
  console.log("Message from server ", event.data);
});

Specifications

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
WebSocket 5 12 117–11 10 12.1 5 4.4 18 147–14 12.1 4.2 1.0
WebSocket 5 12 11
7–11Message size limited to 16 MB (see bug 711205).
10 12.1 5 4.4 18 14
7–14Message size limited to 16 MB (see bug 711205).
12.1 4.2 1.0
binaryType 15 12 11 10 12.1 6 4.4 18 14 12.1 6 1.0
bufferedAmount 5 12 7 10 12.1 5 4.4 18 7 12.1 4.2 1.0
close 5 12 7 10 12.1 5 4.4 18 7 12.1 4.2 1.0
close_event 5 12 7 10 12.1 5 ≤37 18 7 12.1 4.2 1.0
error_event 5 12 7 10 12.1 5 ≤37 18 7 12.1 4.2 1.0
extensions 16 12 8 10 12.1 6 4.4 18 8 12.1 6 1.0
message_event 5 12 7 10 12.1 5 ≤37 18 7 12.1 4.2 1.0
open_event 5 12 7 10 12.1 5 ≤37 18 7 12.1 4.2 1.0
protocol 15 12 7 No 12.1 6 4.4 18 7 12.1 6 1.0
protocol_rfc_6455 16 12 11 10 15 6 ≤37 18 14 14 6 1.0
readyState 5 12 7 10 12.1 5 4.4 18 7 12.1 4.2 1.0
send 5 12 18
11–18Only parameter of type ArrayBuffer and String supported.
8–11Only parameter of type String supported.
7–8Only parameter of type String supported. Returns boolean.
10 12.1 5 4.4 18 18
14–18Only parameter of type ArrayBuffer and String supported.
8–14Only parameter of type String supported.
7–8Only parameter of type String supported. Returns boolean.
12.1 4.2 1.0
url 18 12 7 No 12.1 6 4.4 18 7 12.1 6 1.0
worker_support 5 12 37 10 12.1 5 ≤37 18 37 12.1 5 1.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket