messagehub

Simple message, queue and pubsub system compatible with RabbitMQ

messagehub

Simple message, queue and pubsub system compatible with RabbitMQ.

Communicates events and jobs across processes and servers.

npm install messagehub

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

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.

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

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