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,
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
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 ?