Spec-Zone.ru › HTTP

Headers: Sec-WebSocket-Protocol

Sec-WebSocket-Protocol header

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

  • Learn more
  • See full compatibility
>

The HTTP Sec-WebSocket-Protocol request and response header is used in the WebSocket opening handshake to negotiate a sub-protocol to use in the communication. This can be a well understood protocol, such as SOAP or WAMP, or a custom protocol understood by the client and server.

In a request the header specifies one or more WebSocket sub-protocols that the web application would like to use, in order of preference. These can be added as protocol values in multiple headers, or as comma separate values added to a single header.

In a response it specifies the sub-protocol selected by the server. This must be the first sub-protocol that the server supports from the list provided in the request header.

The request header is automatically added and populated by the browser using values specified by the application in the protocols argument to the WebSocket(). The sub-protocol selected by the server is made available to the web application in WebSocket.protocol.

Header type Request header, Response header
Forbidden request header Yes (Sec- prefix)

Syntax

Sec-WebSocket-Protocol: <sub-protocols>

Directives

<sub-protocols>

A comma-separated list of sub-protocol names, in the order of preference. The sub-protocols may be selected from the IANA WebSocket Subprotocol Name Registry, or may be a custom name jointly understood by the client and the server.

As a response header, this is a single sub-protocol that the server selected.

Examples

>

WebSocket opening handshake

The sub-protocol is specified in the original WebSocket handshake request. The request below shows that the client prefers soap, but also supports wamp.

GET /chat HTTP/1.1
Host: example.com:8000
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
Sec-WebSocket-Protocol: soap, wamp

Specifying the protocols like this has the same effect:

Sec-WebSocket-Protocol: soap
Sec-WebSocket-Protocol: wamp

The response from the server will include the Sec-WebSocket-Protocol header, selecting the first sub-protocol that it supports from the client's preferences. Below that is shown as soap:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
Sec-WebSocket-Protocol: soap

Specifications

Specification
The WebSocket Protocol>
# section-11.3.4>

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet WebView Android WebView on iOS
Sec-WebSocket-Protocol 16 12 11 15 6 18 14 14 6 1.0 4.4 6

See also

  • Sec-WebSocket-Accept
  • Sec-WebSocket-Key
  • Sec-WebSocket-Version
  • Sec-WebSocket-Extensions
  • The WebSocket handshake and Subprotocols in Writing WebSocket servers

© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Sec-WebSocket-Protocol

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API