Skip to content

Instantly share code, notes, and snippets.

@mpj
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpj/ea02918436081c90325a to your computer and use it in GitHub Desktop.
Save mpj/ea02918436081c90325a to your computer and use it in GitHub Desktop.

Snurra Broker

Snurra Broker is a message bus with a stream interface. Below is an example on how to interact with it. Snurra plays very nicely with libraries like highland.js (http://highlandjs.org/), but Snurra Broker does not depend on it - snurra creates plain vanilla node streams, and will work with any stream library.

_ = require 'highland'
Broker = require 'snurra-broker'
broker = new Broker()

# Calling the broker with a string (a channel name), creates a 
# through stream, which when written to ...
broker('add order').write
  sku: "AZDTF4346",
  qty: 21

# ... will dispatch the same message to all siblings:
_(broker('add order'))
  .map (order) ->
    friendlyName: order.sku + " (" + order.qty + " pieces)"
  .pipe broker 'render order', 
    metadata1: 'hello'
    metadata2: 'world'
    
_(broker.log).map (entry) -> console.log JSON.stringify entry

Output

{
  channel: 'add order',
  data: {
    sku: 'AZDTF4346',
    qty: 21
  }
},
{
  channel: 'render order',
  data: {
    friendlyName: 'AZDTF4346 (21 pieces)'
  },
  metadata: {
    metadata1: 'hello',
    metadata2: 'world'
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment