Sunday, 2 April 2017

How to reuse nodejs serverside tcp socket connection

I am using NodeJS net.socket module in a web application. I have made a rest api which hits a method in webserver. That method creates a socket connection with another server to get data. This data is then sent as response to the Rest api request.

Issue is , whenever the Rest api hits the web server , it always creates a new socket connection. I want to re use the previous socket connection. I have tried pushing the socket object in an array, but first of all it looses all the properties if i try to reuse the object from array. Secondly i don't know how to check whether that connection is alive or not.

var net = require('net').Socket();
var sockets = [];

module.exports.Socket = function (port, ip) {
        var Client = net.connect(port, ip);
        sockets.push(Client);
    }
    return Client;
}


via Chandan Sidana

No comments:

Post a Comment