User Tools

Site Tools


sib:listeners:mongolistener

MongoListener

Overview

The MongoListener SIB Listener triggers a dataflow depending on rules set on a mongoDB database.

Workflow

  • Subscribe: .custom.schedules[].topic/RESPONSE/#
    • on message:
      • do nothing for now
  • CRONSchedule: [.custom.schedules.schedule]
    • on trigger:
      • custom.handlers.handler()
      • on data Publish: .custom.schedules[x].topic

Custom configuration

mongolistener/custom.js
var MongoListenerCustom = {
	_id: "mongoListener:1",
	_name: "SIB Mongo Listener Sample",
	handlers: {
		firstCheck: (db, callback)=>{
			if( !MongoListenerCustom.lastrun ) MongoListenerCustom.lastrun = Date.now();
			db.collection( "test" ).findOne( {changed:{$gte:MongoListenerCustom.lastrun}}, (e,d)=>{ callback( d );} );
			MongoListenerCustom.lastrun = Date.now();
		}
	},
	inputFormat: (topic, msg, cb)=>{
		cb(msg);
	},
	loggerCfg: {
		dest: {
			file: false,
			console: true,
			mqtt: true
		}
	},
	mongo: {
		connectionstring: "mongodb://localhost:27017",
		database: "sibtest"
	},
	publishInput: (msg, mqttClient)=>{
		mqttClient.publish( msg._sibheader.topic, JSON.stringify( msg ) );
	},
	mqtt: "mqtt://localhost",
	schedules: [
		{handler: "firstCheck", schedule: "* * * * *", topic: "SIB/Samples/MongoListener/firstCheck"}
	],
	topic: "SIB/Samples/MongoListener"
};
module.exports = MongoListenerCustom;
  • _id: The unique ID of this service
  • _name: A plaintext description of this service
  • handlers: An object containing the handler functions that are executed on the CRON triggering, responsible for getting the changed data from the mongoDB and returning that data formatted to fit a message
    • [handlerName](db, callback): Handler method mapped from the schedules config. Parameters passed are the database instance to use and a callback to return data to on the form callback(data)
  • inputFormat(topic, msg, callback): The method that is run on service activation. The original message is passed as the argument and the return value from this method must be a JSON formatted message conforming to the SIB message standard.
  • loggerCfg: A JSON Object defining what logger actions should be active for this service (see Logger Configuration
  • mongo: Database configuration
    • connectionstring: String on the MongoDB URI format specifying which mongoDB server to connect to
    • database: The name of the database to use
  • publishInput: If defined overrides the standard publishing to the internal MQTT transfer
  • schedules: An array of JSON Objects describing the scheduling and topics to publish messages to
    • schedule: The CRON schedule that decides how often to trigger the data flow
    • topic: The topic to publish the message to
    • handler: The name of the custom.handlers handler to run for this trigger
  • mqtt: The MQTT connection string for the internal MQTT transport
  • topic: The MQTT topic to subscribe to on the internal MQTT transport
sib/listeners/mongolistener.txt · Last modified: 2019/01/18 14:25 by hubbe