Skip to content

Instantly share code, notes, and snippets.

@adamvr
Created February 15, 2012 00:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamvr/1832048 to your computer and use it in GitHub Desktop.
Save adamvr/1832048 to your computer and use it in GitHub Desktop.
A simple script that publishes that it is online when it starts and publishes that it is offline when it quits. Using mqttjs.

purpose

Lively.js is a simple illustration of using MQTT as a device presence indicator

installation

  1. git clone git://gist.github.com/1832048.git lively
  2. cd lively
  3. npm install .
  4. node lively.js ...

functionality

  1. Connects to broker with a will topic set to --topic and will payload set to --not-present
  2. Publishes --present to --topic
  3. Pings until dead
  4. Once dead, publishes --not-present to --topic or relies on the broker to do so.
var mqtt = require('mqttjs')
, optimist = require('optimist');
var argv = optimist
.usage('lively - a script that makes itself look alive\nnode lively.js -t <topic> -h <host> -p <port>')
.options('t', {
demand: true
, describe: 'Topic to publish status messages on'
, alias: 'topic'
})
.options('h', {
demand: true
, describe: 'Broker to publish status messages on'
, alias: 'host'
})
.options('c', {
demand: true
, describe: 'The ID of the lively client'
, alias: 'id'
})
.options('k', {
default: 1000
, describe: 'Keepalive period'
, alias: 'keepalive'
})
.options('p', {
default: 1883
, describe: 'Broker port'
, alias: 'port'
})
.options('present', {
default: '1'
, describe: 'The \'present\' message to publish to the broker'
})
.options('not-present', {
default: '0'
, describe: 'The \'not present\' message to publish to the broker'
})
.argv;
var host = argv.h
, topic = argv.t
, cid = argv.c
, keepalive = argv.k
, pr = argv['present']
, np = argv['not-present']
, port = argv.p;
console.dir(argv);
function statusPacket(status) {
return {
topic: topic
, payload: status
, retain: true
}
}
var client = mqtt.createClient(port, host, function(err, client) {
if (err) { console.log(err); process.exit(1); }
client.connect({client: cid, will: statusPacket(np), keepalive: keepalive});
client.on('connack', function(packet) {
if (packet.returnCode !== 0) return console.log('Bad code');
client.publish(statusPacket(pr));
});
client.on('pingresp', function(packet) {
console.log('Ping resp received');
});
client.interval = setInterval(function() {
client.pingreq();
}, keepalive);
});
function handleSignal(signal) {
process.on(signal, function() {
clearInterval(client.interval);
client.publish(statusPacket(np));
client.disconnect();
});
}
handleSignal('SIGHUP');
handleSignal('SIGINT');
handleSignal('SIGQUIT');
handleSignal('SIGTERM');
{
"author": "Adam Rudd <adam.rudd@uqconnect.edu.au>",
"name": "lively",
"description": "A simple script that publishes that it is online when it starts and publishes that it is offline when it quits. Using mqttjs.",
"version": "0.0.1",
"repository": {
"type": "git",
"url": "git://gist.github.com/1832048.git"
},
"main": "lively.js",
"engines": {
"node": "~0.6.5"
},
"dependencies": {
"mqttjs": ">0.1.3",
"optimist": ">=0.3.1"
},
"devDependencies": {},
"optionalDependencies": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment