Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the most efficient node.js inter-process communication library/method?

We have few node.js processes that should be able to pass messages, What's the most efficient way doing that? How about using node_redis pub/sub

EDIT: the processes might run on different machines

like image 562
DuduAlul Avatar asked Jun 24 '11 05:06

DuduAlul


People also ask

Why is node js so efficient?

It's a light, scalable, and cross-platform way to execute code. It uses an event-driven I/O model which makes it extremely efficient and makes scalable network applications possible. With more than three billion downloads as of 2022, Node.

What are the reasons for node js being lightweight and efficient?

Node JS uses an asynchronous event-driven I/O model, ensuring that almost no function in Node directly performs I/O. That makes it lightweight and efficient.

What is IPC in node JS?

'node-ipc' is a Node. js module for local and remote Inter Process Communication with full support for Linux, Mac and Windows. It also supports all forms of socket communication from low level unix and windows sockets to UDP and secure TLS and TCP sockets.

How node js is faster?

The virtual machine can take the source code to compile it into the machine code at runtime. What it means is that all the “hot” functions that get called often than not can be compiled to the machine code thus boosting the execution speed.


1 Answers

If you want to send messages from one machine to another and do not care about callbacks then Redis pub/sub is the best solution. It's really easy to implement and Redis is really fast.

First you have to install Redis on one of your machines.

Its really easy to connect to Redis:

var client = require('redis').createClient(redis_port, redis_host); 

But do not forget about opening Redis port in your firewall!

Then you have to subscribe each machine to some channel:

client.on('ready', function() {   return client.subscribe('your_namespace:machine_name'); });  client.on('message', function(channel, json_message) {   var message;   message = JSON.parse(message);   // do whatever you vant with the message }); 

You may skip your_namespace and use global namespace, but you will regret it sooner or later.

It's really easy to send messages, too:

var send_message = function(machine_name, message) {   return client.publish("your_namespace:" + machine_name, JSON.stringify(message)); }; 

If you want to send different kinds of messages, you can use pmessages instead of messages:

client.on('ready', function() {   return client.psubscribe('your_namespace:machine_name:*'); });  client.on('pmessage', function(pattern, channel, json_message) {   // pattern === 'your_namespace:machine_name:*'   // channel === 'your_namespace:machine_name:'+message_type   var message = JSON.parse(message);   var message_type = channel.split(':')[2];   // do whatever you want with the message and message_type });  send_message = function(machine_name, message_type, message) {   return client.publish([     'your_namespace',     machine_name,     message_type   ].join(':'), JSON.stringify(message)); }; 

The best practice is to name your processes (or machines) by their functionality (e.g. 'send_email'). In that case process (or machine) may be subscribed to more than one channel if it implements more than one functionality.

Actually, it's possible to build a bi-directional communication using redis. But it's more tricky since it would require to add unique callback channel name to each message in order to receive callback without losing context.

So, my conclusion is this: Use Redis if you need "send and forget" communication, investigate another solutions if you need full-fledged bi-directional communication.

like image 199
Leonid Beschastny Avatar answered Oct 16 '22 04:10

Leonid Beschastny