messagehub

Simple message, queue and pubsub system compatible with RabbitMQ

npm install messagehub
8 downloads in the last month

messagehub

Simple message, queue and pubsub system compatible with RabbitMQ.

Communicates events and jobs across processes and servers.

Installation

npm install messagehub

You also need to connect to a running RabbitMQ server at the host specified.

Documentation

Create

messagehub = require 'messagehub'
myhub = messagehub 'localhost', 'mychannel'

You can also send an AMQP url as the first parameter, or the options hash as describe here.

Events

Emit an event

messagehub = require 'messagehub'
myhub = messagehub 'localhost', 'mychannel'
myhub.emit 'messages.one', {key: "value"}

Observe an event (ALL handlers will trigger)

messagehub = require 'messagehub'
myhub = messagehub 'localhost', 'mychannel'
myhub.on 'messages.one', (data) ->
  console.log "MESSAGES.ONE", data

Jobs

Queue a job

messagehub = require 'messagehub'
myhub = messagehub 'localhost', 'mychannel'
myhub.job 'jobs.one', {key: "value"}

Handle a job (jobs are balanced between workers)

messagehub = require 'messagehub'
myhub = messagehub 'localhost', 'mychannel'
myhub.worker 'jobs.one', (data, done) ->
  console.log "JOBS.ONE", data
  done()

You can put more than one worker in the same file. It will only send you one job per worker registered.

messagehub = require 'messagehub'
myhub = messagehub 'localhost', 'mychannel'

# handler function
handleJobOne = (data, done) ->
  console.log "JOBS.ONE", data
  done()

# register 4 workers
myhub.worker 'jobs.one', handleJobOne
myhub.worker 'jobs.one', handleJobOne
myhub.worker 'jobs.one', handleJobOne
myhub.worker 'jobs.one', handleJobOne
npm loves you