-
-
Save adamvr/2920833 to your computer and use it in GitHub Desktop.
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); |
There are versions of these in the examples folder or the repo. However, I have not got anywhere in trying the client against mosquitto. It would be useful to know:
- is the version in the NPM repo "mqttjs" up to date with this github repo?
- why the calls in the examples do not correspond to the API that the wiki describes?
I would like to be able to use mosquitto as the broker and have clients in MQTT.js that are pub, sub or both. Has anyone tested this? any known gotchas?
UPDATE: I finally remembered to RTFS http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html
From there I could see how to adapt the examples from client_test.js will submit these for inclusion in the repo.
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
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
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.
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?
Any reason these are not part of the MQTT.js github repo? These would work against any MQTT broker, right, not just mosquitto?