mqtt node.js basic example


var mqtt    = require('mqtt');
var client  = mqtt.connect('ws://iot.eclipse.org:80/ws');

client.on('connect', function () {
    client.subscribe('presense');
    client.publish('presence', 'Hello mqtt');
});

client.on('message', function (topic, message) {
  // message is Buffer
    console.log(message.toString());
    client.end();
});


for testing create a client using iot.eclipse.org(node-red)

server :iot.eclipse.org
port:1880
topic: pick


edited example:

var mqtt    = require('mqtt');
var client  = mqtt.connect('ws://iot.eclipse.org:80/ws');

client.on('connect', function () {
    client.subscribe('pick');               //topic subscribed
    client.publish('presence', 'Hello mqtt');
});

client.on('message', function (topic, message) {
  // message is Buffer
    console.log(message.toString());
    //client.end();
});

Comments

Popular posts from this blog

Script For Login, Logout and View Using PHP, MySQL and Bootstrap

PHP Ajax Login Validation Tutorial

Insert CheckBox and Radio button Data in MySQL Database Using PHP