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).
10
12.1
5
4.4
18
147–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
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.
10
12.1
5
4.4
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.