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.
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.
Fired when a connection with a WebSocket is opened. Also available via the onopen property.
Examples
js
// Create WebSocket connection.const socket =newWebSocket("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);});
117–11Message size limited to 16 MB (see bug 711205).
12.1
5
18
147–14Message size limited to 16 MB (see bug 711205).
12.1
4.2
1.0
4.4
binaryType
15
12
11
12.1
6
18
14
12.1
6
1.0
4.4
bufferedAmount
5
12
7
12.1
5
18
7
12.1
4.2
1.0
4.4
close
5
12
7
12.1
5
18
7
12.1
4.2
1.0
4.4
close_event
5
12
7
12.1
5
18
7
12.1
4.2
1.0
≤37
error_event
5
12
7
12.1
5
18
7
12.1
4.2
1.0
≤37
extensions
16
12
8
12.1
6
18
8
12.1
6
1.0
4.4
message_event
5
12
7
12.1
5
18
7
12.1
4.2
1.0
≤37
open_event
5
12
7
12.1
5
18
7
12.1
4.2
1.0
≤37
protocol
15
12
7
12.1
6
18
7
12.1
6
1.0
4.4
protocol_rfc_6455
16
12
11
15
6
18
14
14
6
1.0
≤37
readyState
5
12
7
12.1
5
18
7
12.1
4.2
1.0
4.4
send
5
12
1811–18Only parameter of type ArrayBuffer and String supported.8–11Only parameter of type String supported.7–8Only parameter of type String supported. Returns boolean.
12.1
5
18
1814–18Only parameter of type ArrayBuffer and String supported.8–14Only parameter of type String supported.7–8Only parameter of type String supported. Returns boolean.