Download EPUB

WebSocket API (WebSockets) - Web APIs | MDN

MDN Web Docs 4 min read Original article
Summary
MDN reference page for the WebSocket API. It lists the core interfaces: WebSocket for connecting and sending data, WebSocketStream for a promise-based connection using streams, plus CloseEvent and MessageEvent for connection and message events.

It covers the HTTP handshake headers: Sec-WebSocket-Key (client nonce), Sec-WebSocket-Accept (server upgrade reply), Sec-WebSocket-Version, Sec-WebSocket-Protocol (sub-protocols), and Sec-WebSocket-Extensions.

The page links guides on writing clients and servers in C#, Java, JavaScript (Deno), plus a WebSocketStream client guide. It also lists tools and libraries: AsyncAPI, µWebSockets, Socket.IO, SocketCluster, WebSocket-Node, Total.js, SignalR, Caddy, ws, cowboy, ZeroMQ, PHP WebSocket Server, Django Channels, Phoenix LiveView, Flask-SocketIO and Gorilla WebSocket. Specs: RFC 6455.

Interfaces

WebSocket

The primary interface for connecting to a WebSocket server and then sending and receiving data on the connection.

WebSocketStream

Promise-based interface for connecting to a WebSocket server; uses streams to send and receive 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.

The HTTP headers are used in the WebSocket handshake:

Sec-WebSocket-Key

An HTTP request header that contains a nonce from the client. This is used in the WebSocket opening handshake to verify that the client explicitly intends to open a WebSocket. It is added automatically by the browser.

Sec-WebSocket-Accept

An HTTP response header used in the WebSocket opening handshake to indicate that the server is willing to upgrade to a WebSocket connection. The value in the directive is calculated from the value of Sec-WebSocket-Key in the corresponding request.

Sec-WebSocket-Version

An HTTP header that in requests indicates the version of the WebSocket protocol understood by the client. In responses, it is sent only if the requested protocol version is not supported by the server, and lists the versions that the server supports.

Sec-WebSocket-Protocol

An HTTP header that in requests indicates the sub-protocols supported by the client in preferred order. In responses, it indicates the sub-protocol selected by the server from the client's preferences.

Sec-WebSocket-Extensions

An HTTP header that in requests indicates the WebSocket extensions supported by the client in preferred order. In responses, it indicates the extension selected by the server from the client's preferences.

Guides

Tools

  • AsyncAPI: A specification for describing event-driven architectures based on protocols 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.
  • µWebSockets: Highly scalable WebSocket server and client implementation for C++11 and Node.js.
  • 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)
  • 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.
  • cowboy: Cowboy is a small, fast and modern HTTP server for Erlang/OTP with WebSocket support.
  • ZeroMQ: ZeroMQ is embeddable networking library that carries messages across in-process, IPC, TCP, UDP, TIPC, multicast and WebSocket.
  • 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 websockets wss:// or ws:// and normal sockets over ssl://, tcp://
  • Django Channels: Django library that adds support for WebSockets (and other protocols that require long-running asynchronous connections).
  • (Phoenix) Channels: Scalable real-time communication using WebSocket in Elixir Phoenix framework.
  • Phoenix LiveView: Real-time interactive web experiences through WebSocket in Elixir Phoenix framework.
  • 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.

Specifications

Specification
WebSockets
# the-websocket-interface

Browser compatibility

See also

Help improve MDN

Was this page helpful to you?

Yes No

Learn how to contribute

This page was last modified on by MDN contributors.

Report this article

Keep this one

14 days free, no card. Then $60 a year ($5 a month, billed yearly) or $10 a month.

Your saves stay readable and exportable, even if you stop paying.