node package manager

queue_client

Node.js module to connect to Viki's queue system

Installation

npm install queue_client

Queue

Initialize

service = require 'queue_client'
service.connect(host, port, callback)

Host and port are optional and default to localhost and 5672 respectively.

Subscribe

Before being able to consume from a queue, you must first subscribe to some resources:

service.connect ->
  service.subscribe('gaia-applications', ['application', 'user'], callback)

This creates a queue named gaia_applications which will monitor the application and user resources, i.e. it will subscribe to resources.application.# and resources.user.#. It is possible to change the root of the route path by using the .route(new_route) command as described below (see routing).

Unsubscribe

You can unsubscribe from some resources:

service.connect ->
  service.unsubscribe('gaia-applications', ['user'], callback)

This leaves a queue named gaia_applications which only monitors the application resource, i.e. it will subscribe to resources.application.#. It is possible to change the root of the route path by using the .route(new_route) command as described below (see routing).

Deletion

You can delete a queue and all of its queued events:

service.connect ->
  service.delete('gaia-applications', callback)

Routing

By default, all the messages will be routed under resources.RESOURCE_NAME.ACTION, e.g. resources.videos.create.It is possible to modify the root of the route.

service.connect ->
  service.route 'delayed_jobs.subbing', ->
    service.create_message('compile', '3v', callback)

This commands will create a message under the route delayed_jobs.subbing.compile.create.

On the other hand, by default subscriptions are routed via resources.RESOURCE_NAME.#, e.g. resources.videos.#.

service.connect ->
  service.route 'services.gaia', ->
    service.subscribe('gaia-consumer', [apps], callback)

This will create a new queue called gaia-consumer that is subscribed to services.gaia.apps.#.

Note that the part .resource.action is always present in both examples, changing the route only affects the root of it.

Messages

Messages have always the following structure:

action: mandatory, it will always be 'create', 'update' or 'delete'
resource: mandatory
id: mandatory
payload: optional attribute.

Writing

Use the create_message, update_message and delete_message methods:

service.create_message('application', '38a')
service.update_message('user', '9003u')
service.delete_message('container', '50c')

An optional payload parameter can be supplied:

service.connect ->
  service.create_message('application', '38a', {name: 'gaia'})

This will create a message in the queue that looks like:

{action: 'create', resource: 'application', id: '38a', payload: {name: 'gaia'} }

Consumption

Consumption involves using the built-in runner and providing a routing object:

router =
  application:
    create: (message) -> console.log(message); true
  video:
    create: (message) -> console.log(message); true
    delete: (message) -> console.log(message); true
service.connect ->
  service.run QUEUE_NAME, router, ITERATIONS, callback

Router have RESOURCES with nested ACTIONS that map to funcions. ACTIONS can be create, update or delete.The RESOURCE can be any resource. Note that if noone is registered for listening the kind of resource you are sending, the message will just be dropped. There's no need to close the queue, simply return true when the event has been successfully processed. Note that a message is only acknowledged to the queue when the processing method returns true.

If an exception is raised while processing the message, the runner will call the callback with the error and exist.

ITERATIONS is the number of iterations to run, i.e. of messages to process. 0 means loop forever.