Node-IPC send message from server to client
<p>When using node-ipc to communicate between NodeJS processes over IPC, it seems pretty simple to start a server and listen for connections, or create a client and connect to the server.</p>

<p>But there is something that doesn't seem all that simple:</p>

<blockquote>
  <p><em>How can the server push a message to a client?</em></p>
</blockquote>

<p>The manual says, </p>

<p><strong>On the client, listening to the server for a message</strong>: </p>

<pre><code>ipc.connectTo('world', function(){
        ipc.of.world.on('hello', function(data){
                console.log(data);
            }
        )
    }
);
</code></pre>

<p>Pretty straightforward... But:</p>

<p><strong>On the server, sending a message</strong></p>

<pre><code>// Start server
ipc.serve(function(){

    // Listen for connecting clients
    ipc.server.on('connect', socket =&gt; {
        // From here I can do ipc.server.emit(socket, 'message', {});
        // But I still do not know the ID of the client that just connected
        // There could be 10 child processes connecting, I do not know which client this is
    });
});
</code></pre>

<p>So lets say I setup the server as the given example above, and the client connected.. Than later on in a different timeFrame, On the server side, you would expect this to be the way to send a message to the client:</p>

<pre><code>ipc.server.of[TheClientIdThatJustConnected].emit('myMessage')
</code></pre>

<p>But this does not work, as ipc.server.of is always an empty object and I still do not know the socket client IDs..</p>

<p>So the only way I can think of, is to start EVERY process as both a server and a client... Or after the client connected, I send an '_identification' event with a hardcoded ID and store that sockets in a separate object, but that doesn't make any sense, because the socket is already given in the on('connect') function of the ipc.serve()...</p>

<p>So, how does a node-ipc server send a message to a connected client?</p>

<p>Thanks!</p>