IOT Mosquitto mqtt how to test on localhost

26.3k views Asked by At

I'm just playing around with mosquitto ans mqtt protocol following the very good video https://www.youtube.com/watch?feature=player_embedded&v=WE7GVIFRV7Q

trying to test it on my localhost

in a terminal window I run :

mosquitto_sub -t "nodeconf/eu" -v

but when I run this snippet:

var mqtt    = require('mqtt');
var client  = mqtt.connect();
 client.on('connect', function () {
  client.subscribe('nodeconf/eu');
  client.publish('nodeconf/eu','Hello');
});


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

I don't see (in my terminal window) any Hello.

What's wrong, please ?

BTW I'm also looking for good tutorial and guide on the topic thanks.

1

There are 1 answers

0
Teixi On

You have to add a console.log to your second (the javascript) client to see why it doesn't publishes the Hello properly.

But you can do a typical test with the mosquitto clients:

1) Subscribing to a topic:

mosquitto_sub -d -h localhost -p 1883 -t "myfirst/test"

2) Other client publishes a message content to that topic:

mosquitto_pub -d -h localhost -p 1883 -t "myfirst/test" -m "Hello"

3) All the subscribed clients to this topic will automatically get the message.