Reputation: 22946
WebSockets provide a bidirectional, full-duplex communications channel that operates over HTTP through a single TCP/IP socket connection.
Http is considered unidirectional and slower than websockets so what does it mean that Web sockets operate over Http?
in node.js I have seen that an http server is created first and then socket.io
starts piggybanking on that server.
Is that the only way to work? Why is that done?
Upvotes: 1
Views: 353
Reputation: 74879
Using the word "over" is a mistake. Websockets do not operate over HTTP. That post does go on to explain the upgrade separation correctly later though.
https://www.rfc-editor.org/rfc/rfc6455#section-1.7
The WebSocket Protocol is an independent TCP-based protocol. Its only relationship to HTTP is that its handshake is interpreted by HTTP servers as an Upgrade request.
The socket.io library can do it's bidirectional messaging over multiple transports, websockets being one of them. It also supports HTTP long polling that will be used if a websocket connection is not possible.
Socket.IO is NOT a WebSocket implementation. Although Socket.IO indeed uses WebSocket as a transport when possible, it adds additional metadata to each packet. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a plain WebSocket server either
Socket.io also sets up the middleware to deliver the Javascript content for the client and handle the long polling requests when piggy backed to the http
server. This makes it easier to use and means you don't need to manage a static Javascript dependency
Upvotes: 1