Flexer
Flexer

Reputation: 131

Send message between two independent running processes in Node.js

I've got an Adobe AIR Application on the local machine that communicates with an remote node.js server script (socket-script.js) via socket connection. Furthermore i start a new node.js process through command line and send some additional arguments to a second server script (terminal-script.js). Question: How can i send the arguments from the terminal-script.js to socket-script.js? Afterwards the socket-script.js should broadcast the args to the AIR Application. Anyone an idea how to connect the two independent running processes in Node.js? Thanks.

enter image description here

Illustration link

Upvotes: 2

Views: 2873

Answers (2)

levi
levi

Reputation: 25091

Use the server to communicate between processes:

socket-script.js

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

var server = net.createServer(function(socket) { 
    socket.on('data', function(data){
        if(data.indexOf('terminal:') >-1){
            if(app){
                app.write(data);
            }
        } else if(data.indexOf('app:') >-1){
            app = socket;
        }
    });
});

terminal-script.js:

var net = require('net');
var client = net.connect({port: 9001}, function() { 
    client.write('terminal:' + process.argv[2]);
});

app:

var net = require('net');
var client = net.connect({port: 9001}, function() { 
    client.write('app:connect');
});

client.on('data', function(data){
    if(data.indexOf('terminal:') >-1){
        // got terminal data
    }
});

Upvotes: 6

Matthew Graves
Matthew Graves

Reputation: 3284

The only way that I conceive of to make this work is something like this:

1) You'll need to have terminal-script.js be listening on a socket. Like so:

var arguments = process.args.splice(2);
var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end(arguments[0]);
}).listen(8000, '127.0.0.1');

2) Just make a request from socket-script to the terminal script:

//somewhere in socket-script use this to grab the value from the terminal script.
var http = require('http');

var options = {
  host: 'terminal-script-host.com',
  port: '8000',
  path: '/'
};

var req = http.get(options, function(res) {
    res.on('data', function (data) {
         console.log('socket-script got the data from terminal-script: ' + data);
     });
});

Not sure if this helps. But I can tell you that it would be nearly impossible to "inject" something into the socket-script from the terminal-script, not in a way that would work with the same request anyways.

Upvotes: 0

Related Questions