Skip to content

The pub/sub library for node backed by Redis, MongoDB, AMQP (RabbitMQ), ZeroMQ, MQTT (Mosquitto) or just plain node!

Notifications You must be signed in to change notification settings

ldstein/ascoltatori

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ascoltatori   Build Status Coverage Status

TIP: Ascoltatori is an italian word which means listeners. An Ascoltatore is therefore a single listener.

Ascoltatori is a simple publish/subscribe library supporting the following brokers/protocols:

  • Redis, a key/value store created by @antirez.
  • MongoDB, a scalable, high-performance, document-oriented database.
  • Mosquitto and all implementations of the MQTT protocol.
  • RabbitMQ and all implementations of the AMQP protocol.
  • ZeroMQ to use Ascoltatori in a P2P fashion.

Find out more about Ascoltatori reading the dox generated documentation

NPM NPM

Install

Install the library using npm.

$ npm install ascoltatori --save

Install the library using git.

$ git clone git://github.com/mcollina/ascoltatori.git
$ cd ascoltatori
$ npm install

Getting Started

Ascoltatori focuses on providing a simple and unique abstraction for all supported brokes. Here a simple example using Redis.

var ascoltatori = require('ascoltatori');

ascoltatori.build(function (ascoltatore) {

  // subscribes to a topic
  ascoltatore.subscribe('hello', function() {
    console.log(arguments);
    // { '0': 'hello', '1': 'a message' }
  });

  // publishes a message to the topic 'hello'
  ascoltatore.publish('hello', 'a message', function() {
    console.log('message published');
  });
});

Wildcards

All ascoltatori support the use of wildcards, so everything should work smoothly on every broker. You might find some differences, and in that case file a bug report, so we can fix them.

The wildcard character + matches exactly one word:

var ascoltatori = require('ascoltatori');

ascoltatori.build(function (ascoltatore) {

  ascoltatore.subscribe("hello/+/world", function() {
    // this will print { '0': "hello/there/world", '1': "a message" }
    console.log(arguments); 
  });

  ascoltatore.subscribe("hello/+", function() {
    // this will not be called
    console.log(arguments); 
  });

  ascoltatore.publish("hello/there/world", "a message", function() {
    console.log("message published");
  });
});

The wildcard character * matches zero or more words:

var ascoltatori = require('ascoltatori');

ascoltatori.build(function (ascoltatore) {

  ascoltatore.subscribe("hello/*", function() {
    // this will print { '0': "hello/there/world", '1': "a message" }
    console.log(arguments); 
  });

  ascoltatore.subscribe("*", function() {
    // this will print { '0': "hello/there/world", '1': "a message" }
    console.log(arguments); 
  });

  ascoltatore.subscribe("hello/there/world/*", function() {
    // this will print { '0': "hello/there/world", '1': "a message" }
    console.log(arguments); 
  });

  ascoltatore.publish("hello/there/world", "a message", function() {
    console.log("message published");
  });
});

Of course, you can mix * and + in the same subscription:

var ascoltatori = require('ascoltatori');

ascoltatori.build(function (ascoltatore) {

  ascoltatore.subscribe("hello/+/world/*", function() {
    // this will print { '0': "hello/foo/world/bar/42", '1': "a message" }
    console.log(arguments); 
  });

  ascoltatore.publish("hello/foo/world/bar/42", "a message", function() {
    console.log("message published");
  });
});

Brokers

Ascoltatori supports different brokers. Here we show how to use each of them.

Redis

var ascoltatori = require('ascoltatori');
var settings = {
  type: 'redis',
  redis: require('redis'),
  db: 12,
  port: 6379,
  host: localhost
};

ascoltatori.build(settings, function (ascoltatore) {
  // ...
});

MongoDB

MongoDB uses Capped Collections to implement the pub/sub pattern.

var ascoltatori = require('ascoltatori');
var settings = {
  type: 'mongo',
  url: 'mongodb://127.0.0.1/ascoltatori',
  pubsubCollection: 'ascoltatori',
  mongo: {} // mongo specific options
};

ascoltatori.build(settings, function (ascoltatore) {
  // ...
});

It is also possible to reuse an existing mongodb connection:

var ascoltatori = require('ascoltatori');
var MongoClient = require('mongodb').MongoClient;

MongoClient.connect('mongodb://127.0.0.1/ascoltatori', {}, function (err, db) {
  var settings = {
    type: 'mongo',
    db: db,
    pubsubCollection: 'ascoltatori'
  };
  ascoltatori.build(settings, function (ascoltatore) {
    // ...
  });
})

MQTT (Mosquitto)

var ascoltatori = require('ascoltatori');
settings = {
  type: 'mqtt',
  json: false,
  mqtt: require('mqtt'),
  host: '127.0.0.1',
  port: 1883,
  keyPath: 'path/to/cert.key',
  certPath: 'path/to/cert.cert',
  rejectUnauthorized: true
};

ascoltatori.build(settings, function (ascoltatore) {
  // ...
});

AMQP (RabbitMQ)

var ascoltatori = require('ascoltatori');
var settings = {
  type: 'amqp',
  json: false,
  amqp: require('amqp'),
  exchange: 'ascolatore5672'
};

ascoltatori.build(settings, function (ascoltatore) {
  // ...
});

ZeroMQ

var ascoltatori = require('ascoltatori');
var settings = {
  type: 'zmq',
  json: false,
  zmq: require("zmq"),
  port: "tcp://127.0.0.1:33333",
  controlPort: "tcp://127.0.0.1:33334",
  delay: 10
};

ascoltatori.build(settings, function (ascoltatore) {
  // ...
});

Memory

var ascoltatori = require('ascoltatori');
ascoltatori.build(function (ascoltatore) {
  // ...
});

JSON

By default, every ascoltatore built by the ascoltatori.build wraps every published message in a JSON format. This behaviour can be triggered off by passing the { json: false } option.

require('ascoltatori').build({ json: false }, function(a) {
  // ...
});

Domain support

Ascoltatori supports the node.js domain API. Use it calling the registerDomain function on your Ascoltatore and it will take care of routing the exceptions to the given domain. Look at this example:

var ascoltatori = require('ascoltatori');
var domain = require('domain');

var d = domain.create();
d.on('error', function() {
  console.log(arguments);
});

ascoltatori.build(function (ascoltatore) {
  ascoltatore.registerDomain(d);

  ascoltatore.subscribe('hello/*', function() {
    throw new Error();
  });

  ascoltatore.publish('hello/42', 'a message', function() {
    console.log('message published');
  });
});

Debugging

Ascoltatori supports the debug package and triggers the logs based on an external enviroment variable.

$ DEBUG=ascoltatori:mqtt node exaples/mqtt_topic_bridge.js

The following debug flags are supported:

  • ascoltatori:amqp
  • ascoltatori:trie
  • ascoltatori:mqtt
  • ascoltatori:prefix
  • ascoltatori:redis
  • ascoltatori:zmq
  • ascoltatori:ee2

Reliability

Due to the various transports Ascoltatori uses, it is impossible to garantee one of the various reliability properties across all of the transports. However, the MQTT and AMQP ascoltatori provides at-least-once semantics, which means that the message might be received more than once, but at least once.

Contributing

Fork the repo on github and send a pull requests with topic branches. Do not forget to provide specs to your contribution.

Running specs

  • Fork and clone the repository
  • Run npm install
  • Run npm test

Coding guidelines

Follow felix guidelines.

Feedback

Use the issue tracker for bugs. Tweet us for any idea that can improve the project.

Links

Authors

Contributors

Special thanks to the following people for submitting patches.

LICENSE - "MIT License"

Copyright (c) 2012-2013 Matteo Collina and Contributors, http://matteocollina.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

The pub/sub library for node backed by Redis, MongoDB, AMQP (RabbitMQ), ZeroMQ, MQTT (Mosquitto) or just plain node!

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 99.6%
  • Shell 0.4%