user1499220
user1499220

Reputation: 419

creating a client-server TCP connection in node : socket.io or net module

I'm newbie to node and I would like to create a TCP connection between client and server using node.js. I already have an http server built on node and which sends/pulls data to and from the client. Now, I need to add this 'connection' oriented concept.

I've been reading tutorials and forums and I'm little confused. If I understood well, there are two ways of creating such connection:

So, when do we need to create 2 separate TCP and HTTP servers and when do we need to have only one server (upgrade an HTTP server to a socket.IO one) ?

Upvotes: 8

Views: 13849

Answers (2)

Brad
Brad

Reputation: 163603

WebSockets don't have anything to do with TCP connections (other than that they use them). If you just want to open up a regular TCP connection, the built in net package is what you're looking for.

Socket.IO is an RPC package that uses either WebSockets or emulated WebSockets over other transports such as long-polling JSON.

Upvotes: 14

josh3736
josh3736

Reputation: 145152

If your clients are browsers, then your only option is to use WebSockets (socket.io provides such an implementation).

Browsers do not have an API that you can use to open raw TCP sockets, which is what the net module gives you.

Upvotes: 5

Related Questions