Search code examples
javascriptnode.jschild-processspawn

Node.js use spawn two child processes, then how two child processes exchange with each other?


I have a ELF program named "A", "A" is a tcp server, and I have a ELF program named "B", "B" is a tcp client. When "B" begins to run, It will send msg to the server without judges if the server is ready.

Now I have to use Node.js to run "A" and "B" as two child process, how can I make the "A" must run early than "B"?

I impl like this:

socket.on('xxx', function() {    
    var A = spawn("A", ...);     
    ...      
    var B = spawn("B", ...);     
    setTimeout(B.sendToA, 500);       
    ...   
});

Is there any good Ideas?

Thank you for your help!


Solution

  • Since you mentioned that the server outputs some information on it's startup, you could listen for this and then start the client when it's up.

    Something like:

    socket.on('xxx', function() {    
        var A = spawn("A", ...);
        A.stdin.write("yyy");
        A.stdout.on('data', function(e) {
            if(e.toString() === "yyy") {
                var B = spawn("B", ...);
                B.sendToA(); //I'm guessing this is a pseudo-method?
            }
        });
    });