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

2

8

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:

  • upgrading my already existing http server to a socket.IO server

    var app = require('http').createServer(handler);
    var io = require('socket.io').listen(app);
    function handler(req, res){
       //code
    }
    app.listen(8080);
    
  • creating a separate TCP server based on net module then establish a connection between this TCP server and the http server like is suggested here Create WebSockets between a TCP server and HTTP server in node.js

    var net = require('net');
    net.createServer(function (socket) {
       socket.write('Hello World!\r\n');
       socket.end();
     }).listen(1337);
    

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) ?

Transcendence answered 11/12, 2013 at 19:7 Comment(0)
D
5

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.

Duckbill answered 11/12, 2013 at 21:17 Comment(3)
so just to make sure.. we use net module only if the clients are not browsers?Transcendence
Well... not really. You use net if you have a need to use raw TCP socket communication. You can use socket.io if you want to implement bidirectional realtime messaging on top of HTTP. Even if your clients aren't browsers, there are WebSocket/socket.io client implementations for non-browser environments. The correct choice for your application really depends on exactly what you're trying to accomplish, the kind of features and compatibility you need, and what kind of networks you're dealing with.Duckbill
There will be performance difference using socket io and net with native Android/IOs apps for a chat application?Massey
A
14

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.

Anitraaniweta answered 11/12, 2013 at 21:24 Comment(0)
D
5

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.

Duckbill answered 11/12, 2013 at 21:17 Comment(3)
so just to make sure.. we use net module only if the clients are not browsers?Transcendence
Well... not really. You use net if you have a need to use raw TCP socket communication. You can use socket.io if you want to implement bidirectional realtime messaging on top of HTTP. Even if your clients aren't browsers, there are WebSocket/socket.io client implementations for non-browser environments. The correct choice for your application really depends on exactly what you're trying to accomplish, the kind of features and compatibility you need, and what kind of networks you're dealing with.Duckbill
There will be performance difference using socket io and net with native Android/IOs apps for a chat application?Massey

© 2022 - 2024 — McMap. All rights reserved.