Last active
September 25, 2022 04:13
-
-
Save adamvr/2920833 to your computer and use it in GitHub Desktop.
Tests for mqtt.js against mosquitto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var mqtt = require('mqtt'); | |
| mqtt.createConnection(1883, 'localhost', function(err, client) { | |
| if (err) { | |
| console.dir(err); | |
| return process.exit(-1); | |
| } | |
| var events = ['connack', 'puback', 'pubrec', 'pubcomp']; | |
| for (var i = 0; i < events.length; i++) { | |
| client.on(events[i], function(packet) { | |
| console.dir(packet); | |
| }); | |
| }; | |
| client.connect({keepalive: 1000}); | |
| client.on('connack', function(packet) { | |
| setInterval(function() { | |
| client.publish({ | |
| topic: 'test0' | |
| , payload: 'test' | |
| , qos: 0 | |
| }); | |
| client.publish({ | |
| topic: 'test1' | |
| , payload: 'test' | |
| , qos: 1 | |
| , messageId: 1 | |
| }); | |
| client.publish({ | |
| topic: 'test2' | |
| , payload: 'test' | |
| , qos: 2 | |
| , messageId: 2 | |
| }); | |
| }, 10000); | |
| setInterval(function() { | |
| client.pingreq(); | |
| }, 1000); | |
| }); | |
| client.on('pubrec', function(packet) { | |
| client.pubrel({messageId: 2}); | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var mqtt = require('mqtt'); | |
| mqtt.createConnection(function(client) { | |
| var events = ['connect', 'publish', 'pubrel', 'subscribe', 'disconnect']; | |
| for (var i = 0; i < events.length; i++) { | |
| client.on(events[i], function(packet) { | |
| console.dir(packet); | |
| }); | |
| }; | |
| client.on('connect', function(packet) { | |
| client.connack(0); | |
| }); | |
| client.on('publish', function(packet) { | |
| switch(packet.qos) { | |
| case 1: | |
| client.puback({messageId: packet.messageId}); | |
| break; | |
| case 2: | |
| client.pubrec({messageId: packet.messageId}); | |
| break; | |
| default: | |
| console.log('errors?'); | |
| } | |
| }); | |
| client.on('pubrel', function(packet) { | |
| client.pubcomp({messageId: packet.messageId}); | |
| }); | |
| client.on('pingreq', function(packet) { | |
| client.pingresp(); | |
| }); | |
| client.on('disconnect', function(packet) { | |
| client.stream.end(); | |
| }); | |
| }).listen(1882); |
Hello paultanner,
I am new in MQTT so could you help me for MQTT and Mosquitto in windows that how can i use it in cordova phonegap with windows. Please Help me
Thanks in advance.
I am confused. Are the apis called here a part of the original github repo and it is just that the wiki isnt updated yet ?
Hello,
I tried sending pubrec after receiving the message using
client.pubrec(..)
but I'm getting the error client.pubrec is not a function.
any suggestion?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thx @mcollina, that explains the discrepancies.
What's really confusing is that https://npmjs.org/package/mqttjs points to https://github.com/adamvr/MQTT.js
The NPM entry needs to say that there *is no github repo" for that (old) version and suggest that install MQTT.js manually. Better still there should be an NPM entry for the new version.
Thx for including the required pub/sub examples:
https://github.com/adamvr/MQTT.js/blob/master/bin/mqtt_pub
https://github.com/adamvr/MQTT.js/blob/master/bin/mqtt_sub
It would be nice to reference those from the README
Paul
PS. my pub/sub examples are for the old version (for which there's no github repo) so I won't post them.