one-doing-the-rest-waiting

1.0.9 • Public • Published

OneDoingTheRestWaiting - ODTRW 1.0.9

NPM Version NPM Downloads

one-doing-the-rest-waiting is a RPC helper using Kue package. It is made to handle long async tasks: first call is executed, other later calls will wait for repsonse of the first call.

Installation

npm install one-doing-the-rest-waiting

Example

// producer.js
const rpc = require('one-doing-the-rest-waiting');
const producer = rpc.createProducer({
  prefix: 'example' // prefix for Kue queue
});
 
producer.discover(channel => {
  channel
    .request('very-long-async-task', {
      param1: 'your-param-1',
      param2: 'your-param-2'
    })
    // other tasks with same `your-task-identify` will wait for this request's response
    .waitFor('your-task-identify')
    // handle response
    .onResponse(response => {
      console.log('The task response: ', response);
    })
    .send(); // send your request
});
// consumer.js
const rpc = require('one-doing-the-rest-waiting');
const consumer = rpc.createConsumer({
  prefix: 'example' // prefix for Kue queue
});
 
consumer.register(channel => {
  channel.onRequest((message, done) => {
    // after a very long process, call done()
    setTimeout(() => {
      done({
        response: 'your-result'
      });
    }, 10e3);
  });
});

APIs

createProducer(config)

Factory method to create a Producer instance

createConsumer(config)

Factory method to create a Consumer instance

producer.discover(callback)

  • callback {Function} with parameters
    • channel {ProducingChannel}

Find Consumer instance with same Kue config

producingChannel.request(taskName, data, config)

  • taskName {String}
  • data {Object} - optional
  • config {Object} - optional
    • waitFor {String} Task identify to make other tasks wait
    • ttl {Number} Time-to-live of this message (will be supported next version)
  • return: a Message instance

message.waitFor(taskIdentify)

  • taskIdentify
  • return: current Message instance

Sugar method to set config.waitFor

message.onResponse(callback)

  • callback {Function} with paremeters
    • response {Object}
  • return: current Message instance

Register callback that handles request's response

message.send()

Send the message

consumer.register(callback)

Register a Consumer instance, let other Producers find it, with same Kue config

consumingChannel.onRequest(callback)

  • callback {Function} with parameters
    • request {Object}
    • done {Function}

Register callback that handles requests

License

The MIT License

Package Sidebar

Install

npm i one-doing-the-rest-waiting

Weekly Downloads

0

Version

1.0.9

License

MIT

Last publish

Collaborators

  • mr.d