Skip to content

Implementing Addons

David Barriga Ruano edited this page May 7, 2013 · 25 revisions

Rush gives you the option of implementing your own addons to increase its funcionality. This feature is allowed since the system emits multiple events.

Subscribing to an event

The first step to implement your addons is to subscribe to a channel that emits events. In Rush there are two channels:

  • State of Request Channel: If you subscribe to this channel you will receive an event when a request changes its state (E.g: the request is completed).
  • Request Error Channel: If you subscribe to this channel you will receive an event if an error has ocurred. You can be subscribed to one channel or both.

Returned data

The data received if you subscribed to a channel is an object with the following shared fields:

  • id: The id of the request.
  • topic: The topic header of the request.
  • date: The date when the event has been emitted.

If you are subscribed to the error channel there is an error field in which the error that has ocurred is returned. Example of a returned object from the error channel:

{ id: "bdfa8730-b708-11e2-b48d-97685854ef78",
  date: Tue May 07 2013 13:24:55 GMT+0200 (CEST),
  err: 
   { id: "bdfa8730-b708-11e2-b48d-97685854ef78",
     error: "ENOTFOUND(getaddrinfo)" } }

On the other hand, if you are subscribed to the state channel these fields will be returned in addition to the shared ones:

  • state: The state of the request (completed, pending, error...).
  • task: The task that is being processed.
  • idConsumer: The id of its consumer (emitted only if the request has been assigned to a consumer).
  • result: The result of the request (emitted only if the request has beend assigned to a consumer).

Example of a returned object from the state channel:

{ id: "ed086a00-b704-11e2-b48d-97685854ef78",
  state: "completed",
  date: Tue May 07 2013 12:57:36 GMT+0200 (CEST),
  task: 
   { id: "ed086a00-b704-11e2-b48d-97685854ef78",
     url: "/", /* Relayed request URL */
     headers: 
      { /*task headers*/ },
  result: 
   { id: "ed086a00-b704-11e2-b48d-97685854ef78",
      statusCode: 200,
     encoding: "base64",
     headers: 
      { /*result headers*/ },
     body: "This is the result from the relayed request" } }

Implementing your Addon

First of all, you need to add the following depencency to your module.

var MG = require("./myGlobals").C;

Example of subscribing to the state channel:

function init(emitter) {
  "use strict";
  return function(callback) {
    emitter.on(MG.EVENT_NEWSTATE, function onNewEvent(data) {
      // code of your addon
    }
  }
}

If you want to subscribe to the error channel you only need to change the first parameter of the subscribing function for MG.EVENT_ERR

Clone this wiki locally