18

Are there any RPC modules which work with promises?

On the server I have functions which return promises. I would like to expose them for browser clients to call over websockts or fallbacks. I found some RPC libraries for example dnode, but they expect a callback as parameter.

I would like something like this:

Server:

rpc.expose({
    timeout: function (time) {
        var d = Q.defer();
        setTimeout(function () {
            d.resolve();
        }, time);
        return d.promise;
    }
});

Client:

rpc.timeout(2000).then(function() {
    console.log('done');
});
bara
  • 2,964
  • 2
  • 26
  • 24

3 Answers3

5

I've written an RPC implementation called Wildcard API that lets you do just that:

// Node.js server

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

// We define a `timeout` function on the server
server.timeout = function({seconds}) {
  await sleep({seconds});
};

function sleep({seconds}) {
  return new Promise(resolve => setTimeout(resolve, seconds * 1000));
}
// Browser

import { server } from '@wildcard-api/client';

(async () => {
  // Wildcard makes our `timeout` function available in the browser
  await server.timeout({seconds: 2});
  // 2 seconds later...
  console.log('done');
})();
brillout
  • 7,804
  • 11
  • 72
  • 84
0

Is this what you are looking for?

The "backend" of the example runs both in NodeJS and in browser. There is also a Python version.

The "frontend" of the example (see the complete folder) also runs in NodesJS, browser and Python.

You can find some diagrams here.

This is based on WAMP version 2, and uses Autobahn as a WAMP router.

Disclaimer: I am original author of WAMP / Autobahn and work for Tavendo.

oberstet
  • 21,353
  • 10
  • 64
  • 97
0

Are there any RPC modules which work with promises?

I'm not sure whether it is exactly what you are looking for, but there is ref_send which is based on message passing (see also http://wiki.commonjs.org/wiki/Promises/D).

Implementations are Waterken and Q.js.


You will also want to have a look at Q-connection, "a JavaScript library for communicating asynchronously with remote objects using promises".

Bergi
  • 630,263
  • 148
  • 957
  • 1,375