1

Currently I'm doing a nodejs project where I don't want to use any third party packages or database systems. Now I'm almost done with the project. But here's the problem. I've some authentication related functionality that needs "Sticky Load Balancing", that means I need to do all the authentication related task by the primary node.

I know, I can send message to the primary node from the child nodes using the "process.send({ --- msg object --- })" and from the primary I can do "worker.send({ -- response object -- })" to send message to child nodes.

But I need to do something like this from the child nodes:

process.send(msgObject, callback);

where I'll get the response in the callback. But all the child nodes are separate nodejs instances. I've tried to include the callback function in the msgObject but in the primary node it strips all functions from the msgObject. I read the docs and found this:

process.send(message[, sendHandle[, options]][, callback])

But there's no example of how to implement it. It says the sendHandle is of type net.Server | net.Socket. I know how to set up a basic socket server and communicate with it. But I'm not sure if it's a good idea to use a socket server to implement this communication.

halfer
  • 19,824
  • 17
  • 99
  • 186
h-sifat
  • 1,455
  • 3
  • 19
  • 1
    You can't get the response back in a callback for `process.send()` as it's a messaging system, not a request/response protocol. You can get a response back in another message that you are listening for that the parent sends you back after it receives the original message. Or, you can implement an http server in the parent and then use request/response to that http server from the child. – jfriend00 Jun 29 '21 at 04:27
  • @jfriend00 thanks for your feedback. All the child nodes in my application are already http servers and they are all listening on the same port. If I send the response from the primary node as a http request then it's not guaranteed that the response will be sent to the specific child that sent the message to the primary. Because the primary does the load balancing using the Round Robin algorithm. And another reason that I can't use "process.on("message", cb)" on the child because child nodes will be sending lots of message to the parent and it will be hard to keep track of the responses. – h-sifat Jun 29 '21 at 05:07
  • 1
    You would use a separate non-shared, non-clustered http server, often referred to as a control port server for this type of communication. Child processes would be clients (making requests, getting responses), parent process would exclusively have the server and be answering requests. This other http server would be specifically for request/response communication from the children., You don't have to do solve your problem this way. As I said above, you can just have the parent send the child back a separate message with the response in it. – jfriend00 Jun 29 '21 at 05:09
  • 1
    FYI, you can implement request/response with process messages by embedding a uniqueId in each request message and then having the parent send it's response message with the same uniqueID so the sender can associate the correct response with the original message. But that's an extra layer to write which is why I suggest you just use an existing request/response protocol by creating a control port http server. You could also use a socket.io connection from child to parent which has a request/response feature. There are dozens of ways to implement. – jfriend00 Jun 29 '21 at 05:12
  • @jfriend00 thanks a lot for you answers. I was also thinking of embedding my child node's pid in the message and send it back to the exact child node. I think I would use a HashMap in the child to keep track of responses from the parent. – h-sifat Jun 29 '21 at 05:18
  • That would work, but it's probably less work to just fire up another http server in the parent and define a couple routes on it and use the request/response nature of http. – jfriend00 Jun 29 '21 at 05:58
  • @jfriend00 , if I create another server in the primary node will people be able to do a port scan and send request that primary node? If that's true how can I prevent that? – h-sifat Jun 29 '21 at 08:41
  • You would set that server up on a port that is not routed from the internet so it can only be accessed locally. How you do that depends upon your hosting provider, but there are probably many ports you can use that are not accessible from the outside. – jfriend00 Jun 29 '21 at 19:34

0 Answers0