javascript - Troubles with offline messages using mqtt.js and Mosca -
i'm trying learn how send offline messages using mqtt.js , mosca based on author's demo , other instructions. below attempted, i'm not sure why listening client works online, not when subscription process includes offline configuration (qos, clientid, clean).
1.start standalone mosca broker using:
npm install mosca bunyan -g mosca -v | bunyan
2.run following scripts (listed below) sequentially:
node subscribe.js // user8 subscribes topic called channel-01 qos=1, closes connection node send.js // txuser sends message on channel-01 node listen.js // user8 reconnects , should see txuser's message
3.attempt identify why listen.js
not receive txuser's message.
here scripts:
subscribe.js user8 subscribes topic called channel-01
qos=1
, closes connection.
var mqtt = require('mqtt'); var client = mqtt.connect({ servers: [{ host: 'localhost', port: 1883 }] , clientid:"user8" , clean:false }); client.subscribe('channel-01', {qos:1} , function(){ console.log("subscriber client: subscribed , closing connection."); client.end(); });
send.js txuser sends message on channel-01
var mqtt = require('mqtt'); var client = mqtt.connect({ servers: [{ host: 'localhost', port: 1883 }] , clientid:"txuser" , clean:false }); client.on('connect', function(){ client.publish('channel-01', '* * * important msg ' + date() + ' * * *' , function() { client.end( function (){ console.log('sender client: published message , closed connection'); }); }); });
listen.js user8 reconnects , should see txuser's message
var mqtt = require('mqtt'); var client = mqtt.connect({ servers: [{ host: 'localhost', port: 1883 }] , clientid:"user8" , clean:false }); client.subscribe('channel-01'); client.on('message', function(topic, message) { // never fired when offline options (qos, clientid, clean) // configured in subscribe.js console.log('listener client: message received = ',message.tostring()); }); settimeout(function() { console.log('listener client: exiting'); client.end(); },10*1000);
package.js
{ "name": "mqtt-test-system", "version": "0.0.1", "dependencies": { "mosca": "1.0.1", "mqtt": "1.6.3" } }
ok figured out. apparently, needed add {qos:1}
in publish statement within send.js
script. should like:
client.publish('channel-01', '* * * important msg ' + date() + ' * * *' , {qos:1}, function() {...etc
to clarify mqtt.js introduction/demo slides, submitted pr author , the updated slides here.
Comments
Post a Comment