Skip to content
This repository has been archived by the owner on Feb 11, 2020. It is now read-only.

Mosca advanced usage

Jinyong Park (박진용) edited this page Feb 28, 2018 · 14 revisions

All the types of brokers we can use:

On this section we will list all the pubsubsettings that you can use as mosca brokers (plus the options for each one).

- MongoDB

var pubsubsettings = {
  //using ascoltatore
  type: 'mongo',
  url: 'mongodb://localhost:27017/mqtt',
  pubsubCollection: 'ascoltatori',
  mongo: {}
};

Mongo options:

  • url: the mongodb url (default: 'mongodb://localhost:27017/ascoltatori?auto_reconnect')
  • pubsubCollection: where to store the messages on mongodb (default: pubsub)
  • mongo: settings for the mongodb connection

- Redis

var pubsubsettings = {
  type: 'redis',
  redis: require('redis'),
  db: 12,
  port: 6379,
  return_buffers: true, // to handle binary payloads
  host: "localhost"
};

Redis options:

  • port, the optional port to connect to;
  • host, the optional host to connect to;
  • db, the database to connect to (defaults to 0);
  • password, the optional password to use;
  • sub_conn, the optional redis connection to use for the sub and psub commands;
  • pub_conn, the optional redis connection to use for the pub command;
  • redis, the redis module (it will automatically be required if not present).

- Mosquitto (! this uses an existing mqtt broker called mosquitto as the parent broker)

var pubsubsettings = {
  type: 'mqtt',
  json: false,
  mqtt: require('mqtt'),
  host: '127.0.0.1',
  port: 1883
};

Mosquitto options:

  • clientId, the id of the MQTT client (max 23 chars)
  • keepalive, the keepalive timeout in seconds (see the MQTT spec), the default is 3000;
  • port, the port to connect to;
  • host, the host to connect to;
  • mqtt, the mqtt module (it will automatically be required if not present).

- AMQP (RabbitMQ)

var pubsubsettings = {
  type: 'amqp',
  json: false,
  client: {
     host:'127.0.0.1',
     port: 5672,
     login: 'username',
     password: 'pass'
  },
  amqp: require('amqp'),
  exchange: 'ascolatore5672'
};

Amqp options:

  • client, which is passed through to the amqp.createConnection method;
  • exchange, the exchange name;
  • amqp, the amqp module (it will automatically be required if not present);

- QlobberFSQ

You can use any of the QlobberFSQ constructor options, for example:

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

- ZeroMQ

  • port, the zmq port where messages will be published;
  • controlPort, the zmq port where control messages will be exchanged;
  • remotePorts, the remote control ports that will be connected to;
  • zmq, the zmq module (it will automatically be required if not present);
  • delay, a delay that is applied to the ready and closed events (the default is 5ms);

- Nothing

You could just use NO settings at all and store everything in memory and not in a pub/sub broker

Further Configuration

A few known events to listen for:

  • clientConnected : when a client is connected; the client is passed as a parameter.
  • clientDisconnecting : when a client is being disconnected; the client is passed as a parameter.
  • clientDisconnected : when a client is disconnected; the client is passed as a parameter.
  • published : when a new message is published; the packet and the client are passed as parameters.
  • delivered : when a client has sent back a puback for a published message; the packet and the client are passed as parameters.
  • subscribed : when a client is subscribed to a topic; the topic and the client are passed as parameters.
  • unsubscribed : when a client is unsubscribed to a topic; the topic and the client are passed as parameters.

Other options of mosca (the ones we inserted in our moscaSettings var)

Mosca Options:

  • port, the port where to create the server.
  • host, the IP address of the server (see http://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback).
  • backend, all the options for creating the Ascoltatore (we pass the pubsubsettings var on this one) that will power this server.
  • ascoltatore, By default mosca creates an ascoltatore instance under the hood. With this setting you can pass your own ascoltatore instance. (If you set that theres no need to set the backend)
  • maxInflightMessages, the maximum number of inflight messages per client.
  • logger, the options for Bunyan. (Example: {level: 'debug'} )
  • logger.childOf, the parent Bunyan logger.
  • persistence, the options for the persistence. A sub-key factory is used to specify what persistence to use.
  • secure, an object that includes three properties:
    • port, the port that will be used to open the secure server
    • keyPath, the path to the key
    • certPath, the path to the certificate
  • allowNonSecure, starts both the secure and the unsecure server.
  • http, an object that includes the properties:
    • port, the port that will be used to open the http server
    • bundle, serve the bundled mqtt client
    • static, serve a directory
    • stats, publish the stats every 10s (default false).
  • https, an object similar to http, instead use secure Websockets connection if secure object is provided.

Persistence

You can use Mongo, Redis and LevelUp for persistence storage (mostly for the offline messages feature)

Below are the options you can use for persistence.

Mongo persistence options:

  • url, the connection URL of the database
  • ttl, an object containing two values:
    • subscriptions, the time (ms) after which subscriptions will expire. It defaults to 1 hour.
    • packets, the time (ms) after which packets will expire. It defaults to 1 hour.
  • mongo, all the options for the MongoDB driver.
  • connection, a MongoDB client to be reused (optional) - thats if you already have a MongoDB client that you use.

Redis persistence options:

  • port, the Redis' port.
  • host, the Redis' host.
  • password, the Redis' password.
  • redisOpts, the options for the Redis client.
  • channel, the pub/sub channel that will be used to synchronize the various clients. Defaults to 'moscaSync'.
  • ttl, an object containing two values:
    • subscriptions, the time (ms) after which subscriptions will expire. It defaults to 1 hour.
    • packets, the time (ms) after which packets will expire.

LevelUp

LevelUp options:

  • path, the path to the database
  • ttl, an object containing three values:
    • checkFrequency, the frequency at which the the expiration will be checked. It defaults to 1 minute.
    • subscriptions, the time (ms) after which subscriptions will expire. It defaults to 1 hour.
    • packets, the time (ms) after which packets will expire. It defaults to 1 hour.
  • db, the AbstractLevelDown implementation.
  • all other levelup options.