The WebSocket API (WebSockets)
The WebSocket API is an advanced technology that makes it possible to open a two-way interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.
Note: While a WebSocket connection is functionally somewhat similar to standard Unix-style sockets, they are not related.
Interfaces
WebSocket
-
The primary interface for connecting to a WebSocket server and then sending and receiving data on the connection.
CloseEvent
-
The event sent by the WebSocket object when the connection closes.
MessageEvent
-
The event sent by the WebSocket object when a message is received from the server.
Guides
- Writing WebSocket client applications
- Writing WebSocket servers
- Writing a WebSocket server in C#
- Writing a WebSocket server in Java
Tools
- AsyncAPI: A specification for describing event-driven architectures like WebSocket. You can use it to describe WebSocket-based APIs just as you would describe REST APIs with the OpenAPI specification. Learn why you should consider using AsyncAPI with WebSocket and how to do so.
- HumbleNet: A cross-platform networking library that works in the browser. It consists of a C wrapper around WebSockets and WebRTC that abstracts away cross-browser differences, facilitating the creation of multi-user networking functionality for games and other apps.
- µWebSockets: Highly scalable WebSocket server and client implementation for C++11 and Node.js.
- ClusterWS: Lightweight, fast and powerful framework for building scalable WebSocket applications in Node.js.
- CWS: Fast C++ WebSocket implementation for Node.js (uWebSockets v0.14 fork)
- Socket.IO: A long polling/WebSocket based third party transfer protocol for Node.js.
- SocketCluster: A pub/sub WebSocket framework for Node.js with a focus on scalability.
- WebSocket-Node: A WebSocket server API implementation for Node.js.
- Total.js: Web application framework for Node.js (Example: WebSocket chat)
-
Faye: A
WebSocket
(two-ways connections) and EventSource (one-way connections) for Node.js Server and Client. - SignalR: SignalR will use WebSockets under the covers when it's available, and gracefully fallback to other techniques and technologies when it isn't, while your application code stays the same.
- Caddy: A web server capable of proxying arbitrary commands (stdin/stdout) as a websocket.
- ws: a popular WebSocket client & server library for Node.js.
- jsonrpc-bidirectional: Asynchronous RPC which, on a single connection, may have functions exported on the server and, and the same time, on the client (client may call server, server may also call client).
- cowboy: Cowboy is a small, fast and modern HTTP server for Erlang/OTP with WebSocket support.
- WebSocket King: A client tool to help develop, test and work with WebSocket servers.
- PHP WebSocket Server: Server written in PHP to handle connections via websocksets wss:// or ws://and normal sockets over ssl:// ,tcp://
- Channels: Django library that adds support for WebSockets (and other protocols that require long running asynchronous connections).
- Flask-SocketIO: gives Flask applications access to low latency bi-directional communications between the clients and the server.
- Gorilla WebSocket: Gorilla WebSocket is a Go implementation of the WebSocket protocol.
Related Topics
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 | |
WebSockets_API |
4 |
12 |
11
7-11
Message size limited to 16 MB (see bug 711205).
|
10 |
12.1 |
5 |
≤37 |
18 |
14
7-14
Message size limited to 16 MB (see bug 711205).
|
12.1 |
4.2 |
1.0 |
WebSocket |
4 |
12 |
11
7-11
|
10 |
12.1 |
5 |
≤37 |
18 |
14
7-14
|
12.1 |
4.2 |
1.0 |
binaryType |
15 |
12 |
11 |
10 |
12.1 |
6 |
≤37 |
18 |
14 |
12.1 |
6 |
1.0 |
bufferedAmount |
4 |
12 |
7 |
10 |
12.1 |
5 |
≤37 |
18 |
7 |
12.1 |
4.2 |
1.0 |
close |
4 |
12 |
7 |
10 |
12.1 |
5 |
≤37 |
18 |
7 |
12.1 |
4.2 |
1.0 |
close_event |
4 |
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 |
≤37 |
18 |
8 |
12.1 |
6 |
1.0 |
message_event |
4 |
12 |
7 |
10 |
12.1 |
5 |
≤37 |
18 |
7 |
12.1 |
4.2 |
1.0 |
onclose |
4 |
12 |
7 |
10 |
12.1 |
5 |
≤37 |
18 |
7 |
12.1 |
4.2 |
1.0 |
onerror |
5 |
12 |
7 |
10 |
12.1 |
5 |
≤37 |
18 |
7 |
12.1 |
4.2 |
1.0 |
onmessage |
4 |
12 |
7 |
10 |
12.1 |
5 |
≤37 |
18 |
7 |
12.1 |
4.2 |
1.0 |
onopen |
4 |
12 |
7 |
10 |
12.1 |
5 |
≤37 |
18 |
7 |
12.1 |
4.2 |
1.0 |
open_event |
4 |
12 |
7 |
10 |
12.1 |
5 |
≤37 |
18 |
7 |
12.1 |
4.2 |
1.0 |
protocol |
15 |
12 |
7 |
No |
12.1 |
6 |
≤37 |
18 |
7 |
12.1 |
6 |
1.0 |
protocol_rfc_6455 |
16 |
12 |
11 |
10 |
15 |
6 |
≤37 |
18 |
14 |
14 |
6 |
1.0 |
readyState |
4 |
12 |
7 |
10 |
12.1 |
5 |
≤37 |
18 |
7 |
12.1 |
4.2 |
1.0 |
send |
4 |
12 |
18
See bug 775368.
11-18
Only parameter of type
ArrayBuffer and String supported.8-11
Only parameter of type
String supported.7-8
Only parameter of type
String supported. Returns boolean . |
10 |
12.1 |
5 |
≤37 |
18 |
18
See bug 775368.
14-18
Only parameter of type
ArrayBuffer and String supported.8-14
Only parameter of type
String supported.7-8
Only parameter of type
String supported. Returns boolean . |
12.1 |
4.2 |
1.0 |
url |
18 |
12 |
7 |
No |
12.1 |
6 |
≤37 |
18 |
7 |
12.1 |
6 |
1.0 |
worker_support |
4 |
12 |
37 |
10 |
12.1 |
5 |
≤37 |
18 |
37 |
12.1 |
5 |
1.0 |
See also
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API