Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOT Mosquitto mqtt how to test on localhost

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.

like image 675
Whisher Avatar asked Oct 24 '25 05:10

Whisher


1 Answers

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.

like image 178
Teixi Avatar answered Oct 26 '25 23:10

Teixi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!