igorpavlov
igorpavlov

Reputation: 3626

Websockets transport only in Socket.io 1.3.4

When I used Socket.io 0.9.16 I could set up the desired and the only transport (websockets). No I upgraded to version 1 (1.3.4) and cannot understand, how to restrict the transport.

Looks like it starts a connection with polling and then upgrades to websockets if "it" wants. I want it start and always work on websockets only.

Upvotes: 4

Views: 1588

Answers (1)

jfriend00
jfriend00

Reputation: 707158

All webSocket connections start with an HTTP request. That's how the specification works for webSocket. The client requests an upgrade to the webSocket protocol in that first HTTP request and, if the server agrees, then the socket is "upgraded" to the webSocket protocol.

In case the server does not support webSocket and will not agree to the upgrade, socket.io also sends polling parameters with that first http request so if it does not switch to webSocket, then it will immediately start with http polling.

So, the short answer is that if you look at the network trace, you may think it's starting with http polling, but that's really just the HTTP request that initiates a webSocket connection. This is the way it is supposed to be.

If you want to see more about how a webSocket connection gets established, you can read this nice summary.

Here's a request to start a webSocket connection:

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

And, here's a server response to agree to upgrade to a webSocket connection:

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

This is how the webSocket protocol was designed to make it possible for the same networking infrastructure to support both HTTP connections and webSocket connections and to allow the client to query the server to see if it supports webSocket.

After a successful "upgrade", then only the webSocket protocol is spoken on this socket.

Upvotes: 5

Related Questions