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