Skip to content

Instantly share code, notes, and snippets.

@adamvr
Last active September 25, 2022 04:13
Show Gist options
  • Select an option

  • Save adamvr/2920833 to your computer and use it in GitHub Desktop.

Select an option

Save adamvr/2920833 to your computer and use it in GitHub Desktop.
Tests for mqtt.js against mosquitto
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});
});
});
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);
@paultanner
Copy link

BTW. I wanted to turn on max logging in mosquitto to shed some light. Tried this but it only logged connect.

log_type error
log_type warning
log_type notice
log_type information
connection_messages true
log_timestamp true

I was hitting it with node client_test

@mcollina
Copy link

This gist is 11 month old.
The API has totally changed and the package renamed, look at the repo: https://github.com/adamvr/MQTT.js

@paultanner
Copy link

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.

@kapilkarda
Copy link

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.

@frozenShubh
Copy link

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 ?

@disizme
Copy link

disizme commented Nov 18, 2020

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