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.
- QlobberFSQ, a shared file system queue.
- Apache Kafka, a high-throughput distributed messaging system.
- Memory-only routing, using Qlobber.
Find out more about Ascoltatori reading the dox generated documentation
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
Ascoltatori focuses on providing a simple and unique abstraction for all supported brokers. Here is a simple example using Redis:
var ascoltatori = require('ascoltatori');
ascoltatori.build(function (err, 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');
});
});
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 (err, 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 (err, 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 (err, 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");
});
});
Ascoltatori supports different brokers. Here we show how to use each of them.
var ascoltatori = require('ascoltatori');
var settings = {
type: 'redis',
redis: require('redis'),
db: 12,
port: 6379,
return_buffers: true, // to handle binary payloads
host: localhost
};
ascoltatori.build(settings, function (err, ascoltatore) {
// ...
});
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 (err, 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 (err, ascoltatore) {
// ...
});
})
var ascoltatori = require('ascoltatori');
settings = {
type: 'mqtt',
json: false,
mqtt: require('mqtt'),
url: 'mqtt://127.0.0.1:1883'
};
ascoltatori.build(settings, function (err, ascoltatore) {
// ...
});
var ascoltatori = require('ascoltatori');
var settings = {
type: 'amqp',
json: false,
amqp: require('amqp'),
exchange: 'ascolatore5672'
};
ascoltatori.build(settings, function (err, ascoltatore) {
// ...
});
Use with amqplib
var ascoltatori = require('ascoltatori');
var settings = {
type: 'amqplib',
json: false,
amqp: require('amqplib/callback_api'),
exchange: 'ascolatore5672'
};
ascoltatori.build(settings, function (err, ascoltatore) {
// ...
});
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 (err, ascoltatore) {
// ...
});
You can use any of the QlobberFSQ constructor options, for example:
var ascoltatori = require('ascoltatori');
var settings = {
type: 'filesystem',
json: false,
qlobber_fsq: require("qlobber-fsq"),
fsq_dir: "/shared/fsq"
};
ascoltatori.build(settings, function (err, ascoltatore) {
// ...
});
If you don't specify fsq_dir
then messages will be written into a directory named fsq
in the qlobber-fsq
module directory.
var ascoltatori = require('ascoltatori');
ascoltatori.build(function (err, ascoltatore) {
// ...
});
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(err, a) {
// ...
});
var ascoltatori = require('ascoltatori');
var settings = {
type: 'kafka',
json: false,
kafka: require("kafka-node"),
connectionString: "localhost:2181",
clientId: "ascoltatori",
groupId: "ascoltatori",
defaultEncoding: "utf8",
encodings: {
image: "buffer"
}
};
ascoltatori.build(settings, function (err, ascoltatore) {
// ...
});
If you publish to a kafka topic that doesn't exist, that topic will be created using the default settings.
If you subscribe to a kafka topic that doesn't exist, that subscription will take affect only when something is published to the kafka topic through this ascoltatori.
Ascoltatori supports the debug package and triggers the logs based on an external enviroment variable.
$ DEBUG=ascoltatori:mqtt node examples/mqtt_topic_bridge.js
The following debug flags are supported:
ascoltatori:amqp
ascoltatori:trie
ascoltatori:mqtt
ascoltatori:prefix
ascoltatori:redis
ascoltatori:zmq
ascoltatori:ee2
ascoltatori:filesystem
ascoltatori:kafka
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.
Use the issue tracker for bugs. Tweet us for any idea that can improve the project.
Special thanks to the following people for submitting patches.
Copyright (c) 2012-2015 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.