0

Here is my code:

var express = require('express');
var net = require('net');
var app = express();


//first of all connect to a stable client
var server = net.createServer(function(socket) {
// do nothing here . what i want is to use this socket in 
// (following) app.get() function
});
server.listen(1337, '127.0.0.1');

//receive request from other clients
app.get('/', function (req, res) 
{
// retriving mobileNumber and message
var mobileNumber = req.query.mobileNumber;
var message      = req.query.message;

//now i want to send this data to the stable client
// to which i have connected earlier.
res.end();
});

app.listen(6544);

I want to send data to the previously connected socket whenever a new '/' request arrives.

1 Answers1

0

One way to do that through one variable var sock as bellow.

var sock;
var server = net.createServer(function(socket) {
   sock = socket;
});
server.listen(1337, '127.0.0.1');

app.get('/', function (req, res) 
{
    if (sock) {
        // use sock here
    }
});
zangw
  • 43,869
  • 19
  • 177
  • 214
  • thankyou for reply . It worked for localhost but i want to deploy it on openshift . Openshift gave me a url to access my nodejs app but not ip address. so is there any way to do that on openshift please help me. – Narayan Choudhary Feb 25 '16 at 13:39
  • @NarayanChoudhary, Sorry, I am not familiar with Openshift. maybe you can ask another question for your new issue – zangw Feb 25 '16 at 13:41