Loourr
Loourr

Reputation: 5125

Pass variable into a running node.js program

I have an process which is running on my server in the background. The object is exported like this

module.exports = new Application()

because I only want one instance of the object to ever exist.

I then have a separate process which tries to call a prototyped function process of Application. It looks something like this

var app = require('./Application.js');

app.process(process.argv[2]);

and say the file is called process.js. I'll then call node process.js thing_to_pass

The trouble is, every time I do this is it re-instantiates application which leads to behavior I am not looking for.

How can I get around this? I was thinking of using socket.io to communicate between the two processes but this seems hackish.

Upvotes: 0

Views: 144

Answers (3)

Jason
Jason

Reputation: 13766

If you want a running process to receive new data, it has to listen for that data in some way. Whether you set up an HTTP server and accept and process requests with new data, or socket.io, or you wait for info from the command line, watch a file for changes, or what have you.

If you start up a whole separate process, it will be logically segregated in every way from the currently running process, and thank goodness or else completely separate apps could mess with each other in unintended ways.

Upvotes: 0

leszek.hanusz
leszek.hanusz

Reputation: 5327

If you're using Linux, I would suggest to use UNIX-domain sockets.

Upvotes: 0

bendecoste
bendecoste

Reputation: 798

You aren't going to be able to go down the road you are trying to go down. Because you have two different processes, you are going to make two different instances of Application. Sending messages from one process to another via socket.io isn't that hackish imo, definitely something to consider.

You could also consider using child_process and sending messages with kill http://nodejs.org/api/process.html

Upvotes: 3

Related Questions