Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communicating between two nodejs application

I have three (nodejs) processes P1, P2, P3.

  • Function A of P1 completes its execution it sends some data to P2.

  • P2 performs its activities and its output should be sent to P3.

  • P3 works on the input sends some acknowledgement data to Function B of P1.

All three are different node js applications and runs on different servers (in the same LAN). The question is what should be the communication mechanism between these processes.

Three options appears to be viable:

1) REST API (Express)

Let all the processes be Express REST APIs and using the http node js package call the required functions

2) Pub/Sub

When P1 completes the work, it post the output as a message to a topic and P2 becomes a subscriber and onMessage P2 process executes.

3) TCP/IP

A simple TCP/IP server-client architecture where P1 becomes the server client and P2 as server. Whenever P1 completes the work send the output through sockets.

All three options are for used for many to one or many to many communication so I guess there could be a lot of unnecessary overhead as my requirement has only one to one communication between the processes.

Is there any other method that i can use or one of these best suits is the dilemma.

Please suggest.

Thanks.

like image 993
Lakshman Pilaka Avatar asked Oct 02 '17 03:10

Lakshman Pilaka


People also ask

How many connections NodeJS can handle?

To address these issues, Node. JS uses a single thread with an event-loop. In this way, Node can handle 1000s of concurrent connections without any of the traditional detriments associated with threads. There is essentially no memory overhead per-connection, and there is no context switching.

CAN node run client side?

Other reasons why we cannot use node modules at the client side is that the node uses the CommonJS module system while the browser uses standard ES Modules which has different syntax.

Why is NodeJS a cross-platform application?

Cross-platform development js allows you to build cross-platform desktop apps on platforms like NW. js or Electron. This allows you to reuse some of the codes from the web app to the desktop version for Windows, macOS, and Linux.


1 Answers

You can work with mqtt protocol. This is a very light weight and fast protocol. In this protocol you will get a publisher, a subscriber and a broker. Publisher will send data to subscriber under a topic via mqtt broker. It is almost same as Pub/Sub.

MQTT Architecture

MQTT

Note : Function A of P1 will publish data and P2 will subscribe. After P2 completes its activities it will publish data to P3 and P3 will be in subscribing mode. After that P3 will publish acknowledgement and Function B of P1 will subscribe the topic.

like image 66
Abdul Alim Shakir Avatar answered Sep 23 '22 08:09

Abdul Alim Shakir