Skip to content

Instantly share code, notes, and snippets.

View mpj's full-sized avatar

Mattias Petter Johansson mpj

View GitHub Profile
We couldn’t find that file to show.
mpj / gist:808
Created July 22, 2008 10:27
mpj / gist:8906631
Created February 9, 2014 21:59
start of a bus
var createBus = function() {
var me = {}
var _handlers = {}
var _events = [] = function(name) {
return {
on: function(address) {
return {
### Keybase proof
I hereby claim:
* I am mpj on github.
* I am mpj ( on keybase.
* I have a public key whose fingerprint is 882A BDDF DAC0 B6CB 5EB3 078A 9BE9 D285 0A78 B179
To claim this, I am signing this object:
var findToArray = function(collection, query) {
var deferred = Q.defer();
var cursor = collection.find(query);
collection.find({}).toArray(function(error, result) {
if (error)

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 (, 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 
orderStream = broker('orders')
unhandledOrdersStream =
_(broker('orders')).filter (order) -> not order.shippingdate
unhandledOrdersStream.each (order) ->
# do stuff with order that is not handled
mpj /
Last active August 29, 2015 14:06
Snurra sketch

Snurra - the streaming message bus.

Snurra is a fresh take on message bus pattern by using streams as the interface instead of the more traditional callback pattern.

Snurra syntax

Let's walk through the basic syntax of Snurra.

var _ = require('highland');
var snurra = require('snurra');
var bus = snurra();
mpj / gist:1b6ad55599497340e3c1
Last active August 29, 2015 14:06
snurra philisophy sketch

1. Inspection and Simulation

The primary ambition of Snurra is to make it easier to see what is happening inside your program, mock external dependencies and to verify behavior.

2. Usability over correctness

Snurra tries looks upon the API consumer like a user, rather than a developer that can be expected to fiddle around with unclear interfaces and vague documentation.

3. No magic and no veils

Snurra tries to expose vanilla node streams as cleanly as possible and not abstract them away from the user, so that the user can take advantage of node stream documentation and stream manipulation libraries like highland.js. It is willing to sacrifice being terse or hand-holding in order to be understandable and re-usable.

4. Focus on the message

mpj / gist:58ee72609030adca7020
Created September 26, 2014 16:55
WebSocket = require 'ws'
_ = require 'highland'
isNumber = require 'mout/lang/isNumber'
listen = (account) ->
stream = _()
ws = new WebSocket('ws://');
ws.on 'open', ->
payload = JSON.stringify
"command" : "subscribe"