Reputation: 455
I have recently come upon Node's net module, I was wondering if using this module would be the equivalent of using the socket.io module. What is the difference between them ? thanks!
Upvotes: 11
Views: 4379
Reputation: 707326
I have recently come upon Node's net module, I was wondering if using this module would be the equivalent of using the socket.io module.
No, they are not even close to the same thing.
Node's net module is a basic low level TCP and UDP interface. It allows you to make a TCP or UDP connection to some endpoint and to then send or receive data from that endpoint over that connection. These are raw TCP connections. You define the protocol, data format and all conventions used in the communication. All TCP does is deliver your data from one end to the other.
socket.io is somewhat at the other end of the network stack.
socket.io
webSocket
TCP
A webSocket is built on top of TCP. It has it's own unique connection scheme that starts with an http connection with certain custom headers and then requests an "upgrade" to the webSocket protocol. If the server approves the upgrade, then the same TCP socket that the http connection started over is converted to the webSocket protocol. The webSocket protocol has it's own unique encryption and data format.
Socket.io is built on top of the webSocket protocol (meaning it uses the webSocket protocol for it's communication). Socket.io has it's own unique connection scheme (starts with http polling, then switches to a regular webSocket if permissible) and it has an additional data structure built on top of the webSocket data frame that defines a message name and a data packet and a few other housekeeping things.
socket.io and webSocket are both supported from browser Javascript. A plain TCP or UDP connection is not supported from browser Javascript. So, if you were looking to communicate with a browser, you would not be using plain TCP.
Upvotes: 27