0

According to the Wikipedia article: http://en.wikipedia.org/wiki/WebSocket, The server sends back this response to the client during handshake:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat

Does this close the connection (as HTTP responses usually do) or it is kept open throughout the entire handshake and it can start sending WebSocket frames straight away (assuming that it succeeds)?

Jon
  • 1,224
  • 2
  • 14
  • 23

1 Answers1

2

An HTTP socket going through the handshake process to be upgraded to the webSocket protocol is not closed during that process. The same open socket goes through the whole process and then becomes the socket used for the webSocket protocol. As soon as the upgrade is complete, that very socket is ready for messages to be sent per the webSocket protocol.

It is this use of the exact same socket that enables a webSocket connection to run on the same port as an HTTP request (no extra port is needed) because it literally starts out as an HTTP request (with some extra headers attached) and then when those headers are recognized and both sides agree, the socket from that original HTTP request on the original web port (often port 80) is then switched to use the webSocket protocol. No additional connection on some new port is needed.

I actually find it a relatively elegant design because it makes for easy coexistence with a web server which was an important design parameter. And, a slight extra bit of connection overhead (protocol upgrade negotiation) is generally not an issue because webSocket connections by their very nature are designed to be long running sockets which you open once and use over an extended period of time so a little extra overhead to open them doesn't generally bother their use.

If, for any reason, the upgrade is not completed (both sides don't agree on the upgrade to webSocket), then the socket would remain an HTTP socket and would behave as HTTP sockets normally do (likely getting closed right away, but subject to normal HTTP interactions).

You can see this answer for more details on the back and forth during an upgrade to webSocket: SocketIO tries to connect using same port as the browser used to get web page


Note: There is sometimes confusion about this topic because of how socket.io (a layer built on top of http and webSocket) works by default. This extra socket.io behavior has nothing to do with the webSocket connection protocol itself. Socket.io (in its default configuration) will initially make a series of regular http requests and then after some criteria is met, it will then initiate a webSocket connection (which starts with an http request with the appropriate webSocket headers). That last http request (with the webSocket headers) then goes through the above-described handshake process and changes over to using the webSocket protocol.

Socket.io can be configured (via a client-side connection option) to start immediately with the webSocket connection and skip the series of http requests - though this is not its default configuration.

jfriend00
  • 683,504
  • 96
  • 985
  • 979