29

Let's say I have 2 web servers. Both of them just installed Node.js and is running a website (using Express). Pretty basic stuff.

How can Server-A tell Server-B to execute a function? (inside node.js)

Preferably...is there a npm module for this that makes it really easy for me?

TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

4 Answers4

16

How can Server-A tell Server-B to execute a function?

You can use one of the RPC modules, for example dnode.

yojimbo87
  • 65,684
  • 25
  • 123
  • 131
  • 8
    Can't I just use HTTP REST? (basic post/get) – TIMEX May 12 '11 at 09:29
  • @Owalla: Yes you can, but you will need to bind your REST interface and calls to certain API within your node.js programs. Modules like dnode saves you time and code because they abstract it for you. You can also use standard net module and sockets if it's only server to server communication, but this also requires writing more code than using RPC libraries. – yojimbo87 May 12 '11 at 09:33
  • In terms of scaling up, isn't it better to use http? That would allow deploying different server to different instances more easily I guess. PS:- I am not well aware of RPC modules. – vcode Jan 23 '20 at 15:16
9

Check out Wildcard API, it's an RPC implementation for JavaScript.

It works between the browser and a Node.js server and also works between multiple Node.js processes:

// Node.js process 1

const express = require('express');
const wildcardMiddleware = require('@wildcard-api/server/express');
const {endpoints} = require('@wildcard-api/server');

endpoints.hello = async function() {
  const msg = 'Hello from process 1';
  return msg;
};

const app = express();
app.use(wildcardMiddleware());

app.listen(3000);
// Node.js process 2

const wildcard = require('@wildcard-api/client');
const {endpoints} = require('@wildcard-api/client');

wildcard.serverUrl = 'http://localhost:3000';

(async () => {
  const msg = await endpoints.hello();
  console.log(msg); // Prints "Hello from process 1"
})();

You can browse the code of the example here.

brillout
  • 7,804
  • 11
  • 72
  • 84
3

You most likely want something like a JSON-RPC module for Node. After some quick searching, here is a JSON-RPC middleware module for Connect that would be perfect to use with Express.

Also, this one looks promising too.

leek
  • 11,803
  • 8
  • 45
  • 61
  • JSON-RPC middleware ...the first link...that's basically HTTP REST , right? – TIMEX May 12 '11 at 05:15
  • 1
    JSON-RPC doesn't have to be over HTTP. It is also not exactly RESTful because when using HTTP the only request type is GET. JSON-RPC is a standard for sending remote procedure calls (RPC) similar to XML-RPC. Read more: http://en.wikipedia.org/wiki/JSON-RPC – leek May 12 '11 at 05:26
-3

Update : The library I've made & linked below, isn't maintained currently. Please check out the other answers on this thread.


What you need is called RPC. It is possible to build your own, but depending on the features you need, it can be time consuming.

Given the amount of time I had to invest, I'd recommend finding a decent library that suits your purpose, instead of hand rolling. My usecase required additional complex features like selective RPC calls, for which I couldn't find anything lightweight enough, so had to roll my own.

Here it is https://github.com/DhavalW/octopus.

DhavalW
  • 40
  • 3