Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a WebSocket Subprotocol?

Ive been trying to implement the WebSocket protocol from scratch in nodejs, doing so i have a question thats since been unawnsered. What exactly are subprotocols in regards to websockets? The second parameter of the WebSocket constructor is where you specify "subprotocols" -

let socket = new WebSocket("ws://localhost:3000",["http",...]);

Can anybody give me a clear awnser to what purpose they have?

like image 886
Deon Rich Avatar asked May 07 '21 14:05

Deon Rich


People also ask

What is WebSocket WSS?

The WebSocket protocol specification defines ws (WebSocket) and wss (WebSocket Secure) as two new uniform resource identifier (URI) schemes that are used for unencrypted and encrypted connections respectively.

How does WSS work?

WebSockets are used to provide a connection between a client and a server so that both parties can send data at any time. The client uses a process known as the WebSocket handshake, which helps establish a connection between the server and the client.

What is the purpose of a WebSocket?

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.

What are sub protocols?

subprotocol (plural subprotocols) A secondary or subsidiary protocol.


3 Answers

Websockets just define a mechanism to exchange arbitrary messages. What those messages mean, what kind of messages a client can expect at any particular point in time or what messages they are allowed to send is entirely up to the implementing application. So you need an agreement between the server and client about these things. You might say… you need a protocol specification. The subprotocol parameter simply lets clients formally exchange this information. You can just make up any name for any protocol you want. The server can simply check that the client appears to adhere to that protocol during the handshake. You can also use it to request different kinds of protocols from the server, or use it for versioning (e.g. when you introduce my-protocol-v2, but still need to support clients only understanding my-protocol-v1).

like image 140
deceze Avatar answered Oct 17 '22 14:10

deceze


Explained on MDN here

Think of a subprotocol as a custom XML schema or doctype declaration. You're still using XML and its syntax, but you're additionally restricted by a structure you agreed on. WebSocket subprotocols are just like that. They do not introduce anything fancy, they just establish structure. Like a doctype or schema, both parties must agree on the subprotocol; unlike a doctype or schema, the subprotocol is implemented on the server and cannot be externally referred to by the client.

Subprotocols are explained in sections 1.9, 4.2, 11.3.4, and 11.5 of the spec.

A client has to ask for a specific subprotocol. To do so, it will send something like this as part of the original handshake:

http GET /chat HTTP/1.1 ... Sec-WebSocket-Protocol: soap, wamp

or, equivalently:

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

Now the server must pick one of the protocols that the client suggested and it supports. If there is more than one, send the first one the client sent. Imagine our server can use both soap and wamp. Then, in the response handshake, it sends:

Sec-WebSocket-Protocol: soap

The server can't send more than one Sec-Websocket-Protocol header. If the server doesn't want to use any subprotocol, it shouldn't send any Sec-WebSocket-Protocol header. Sending a blank header is incorrect. The client may close the connection if it doesn't get the subprotocol it wants.

If you want your server to obey certain subprotocols, then naturally you'll need extra code on the server. Let's imagine we're using a subprotocol json. In this subprotocol, all data is passed as JSON. If the client solicits this protocol and the server wants to use it, the server needs to have a JSON parser. Practically speaking, this will be part of a library, but the server needs to pass the data around.

like image 38
Liam Avatar answered Oct 17 '22 13:10

Liam


Some sample code, copy from https://hpbn.co/websocket/#subprotocol-negotiation, to make it clear.

The client can advertise which protocols it supports to the server as part of its initial connection handshake:

var ws = new WebSocket('wss://example.com/socket',
                       ['appProtocol', 'appProtocol-v2']); 

ws.onopen = function () {
  if (ws.protocol == 'appProtocol-v2') { 
    ...
  } else {
    ...
  }
}
like image 1
Qiulang 邱朗 Avatar answered Oct 17 '22 15:10

Qiulang 邱朗