Skip to content
Paul Tanner edited this page Aug 27, 2013 · 3 revisions

Client migration example

Introduction

This document outlines a simple client migration from [email protected] to [email protected]. The same changes are needed for mqttjs to mqtt.

Original

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);
});

New API

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() {})

Clone this wiki locally