-
Notifications
You must be signed in to change notification settings - Fork 1.4k
migration example
Paul Tanner edited this page Aug 27, 2013
·
3 revisions
This document outlines a simple client migration from [email protected] to [email protected]. The same changes are needed for mqttjs to mqtt.
var mqtt = require('mqtt');
mqtt.createClient(1883, 'localhost', function(err, client) {
if (err) throw err;
client.connect({keepalive:10000});
client.on('connack', function(packet) {
if (packet.returnCode !== 0) throw 'Connect error';
client.subscribe({topic: 'example'});
client.publish({topic: 'test', payload: 'test'});
});
client.on('publish', function(packet) {
var t = packet.topic
, p = packet.payload;
console.log('topic: ' + t + ' payload: ' + p);
});
setInterval(client.pingreq.bind(client), 10000);
});
var mqtt = require('mqtt');
var client = mqtt.createClient(1883, 'localhost', {
keepalive: 10000
});
client.on('connect', function() {
client.subscribe('example');
client.publish('test', 'test');
client.on('message', function(topic, message) {
console.log('topic: ' + topic + ' payload: ' + message);
});
});
Note: client.on('suback',function() {}) must be replaced by client.on('subscribe',function() {})