Sanjit Roy
Sanjit Roy

Reputation: 187

What is the best way to communicate between two servers?

I am building a web app which has two parts. In one part it uses a real time connection between the server and the client and in the other part it does some cpu intensive task to provide relevant data.

Implementing the real time communication in nodejs and the cpu intensive part in python/java. What is the best way the nodejs server can participate in a duplex communication with the other server ?

Upvotes: 9

Views: 15551

Answers (4)

Jedediah Smith
Jedediah Smith

Reputation: 578

For a basic solution you can use Socket.IO if you are already using it and know how it works, it will get the job done since it allows for communication between a client and server where the client can be a different server in a different language.

If you want a more robust solution with additional options and controls or which can handle higher traffic throughput (though this shouldn't be an issue if you are ultimately just sending it through the relatively slow internet) you can look at something like ØMQ (ZeroMQ). It is a messaging queue which gives you more control and lots of different communications methods beyond just request-response.

When you set either up I would recommend using your CPU intensive server as the stable end(server) and your web server(s) as your client. Assuming that you are using a single server for your CPU intensive tasks and you are running several NodeJS server instances to take advantage of multi-cores for your web server. This simplifies your communication since you want to have a single point to connect to.

If you foresee needing multiple CPU servers you will want to setup a routing server that can route between multiple web servers and multiple CPU servers and in this case I would recommend the extra work of learning ØMQ.

Upvotes: 8

RoyHB
RoyHB

Reputation: 1725

I maintain a node.js application that intercommunicates among 34 tasks spread across 2 servers.

In your case, for communication between the web server and the app server you might consider mqtt.

I use mqtt for this kind of communication. There are mqtt clients for most languages, including node/javascript, python and java. In my case I publish json messages using mqtt 'topics' and any task that has registered to subscribe to a 'topic' receives it's data when published. If you google "pub sub", "mqtt" and "mosquitto" you'll find lots of references and examples. Mosquitto (now an Eclipse project) is only one of a number of mqtt brokers that are available. Another very good broker that is written in Java is called hivemq.

This is a very simple, reliable solution that scales well. In my case literally millions of messages reliably pass through mqtt every day.

Upvotes: 2

binariedMe
binariedMe

Reputation: 4329

You can use http.request method provided to make curl request within node's code. http.request method is also used for implementing Authentication api. You can put your callback in the success of request and when you get the response data in node, you can send it back to user. While in backgrount java/python server can utilize node's request for CPU intensive task.

Upvotes: 2

Robin
Robin

Reputation: 864

You must be looking for socketio

Socket.IO enables real-time bidirectional event-based communication. It works on every platform, browser or device, focusing equally on reliability and speed.

Sockets have traditionally been the solution around which most realtime systems are architected, providing a bi-directional communication channel between a client and a server.

Upvotes: 0

Related Questions