Mosquitto Sub issue

53 views
Skip to first unread message

Barry Nelson

unread,
Nov 3, 2023, 11:55:43 AM11/3/23
to MQTT
Hi,
I am new to Mosquitto and need some help with the subscriber setup please.
My publisher seems to be working ok with the broker but when I run the subscriber it does nothing.

Here is the log from my broker showing sucessful connection to the publisher device & reciving data from the device:

03-11-2023 T14:54:12: mosquitto version 2.0.18 starting
03-11-2023 T14:54:12: Config loaded from mosquitto.conf.
03-11-2023 T14:54:12: Opening ipv6 listen socket on port 8888.
03-11-2023 T14:54:12: Opening ipv4 listen socket on port 8888.
03-11-2023 T14:54:12: mosquitto version 2.0.18 running
03-11-2023 T14:54:13: New connection from 192.168.2.2:12163 on port 8888.
03-11-2023 T14:54:13: New client connected from 192.168.2.2:12163 as BCS2 (p2, c1, k60, u'admin').
03-11-2023 T14:54:13: No will message specified.
03-11-2023 T14:54:13: Sending CONNACK to BCS2 (0, 0)
03-11-2023 T14:54:13: Received SUBSCRIBE from BCS2
03-11-2023 T14:54:13: BCS2/sub (QoS 0)
03-11-2023 T14:54:13: BCS2 0 BCS2/sub
03-11-2023 T14:54:13: Sending SUBACK to BCS2
03-11-2023 T14:54:13: Received PINGREQ from BCS2
03-11-2023 T14:54:13: Sending PINGRESP to BCS2
03-11-2023 T14:55:15: Received PINGREQ from BCS2
03-11-2023 T14:55:15: Sending PINGRESP to BCS2
03-11-2023 T14:55:15: Received PINGREQ from BCS2
03-11-2023 T14:55:15: Sending PINGRESP to BCS2
03-11-2023 T14:56:00: Received PUBLISH from BCS2 (d0, q0, r0, m0, 'BCS2/pub', ... (7 bytes))
03-11-2023 T14:56:15: Received PINGREQ from BCS2
03-11-2023 T14:56:15: Sending PINGRESP to BCS2

You can see above the puiblisher has sent some data.

I try to run the mosquitto subsriber (on windows, broker is on same PC) it runs but shows nothing after the command has run:

mosuitto_sub -h localhost -t "BCS2/pub" -d -v

I have played around with the above command with different quotes etc, made no difference.. Also tried the topic as BCS2/sub. Tried using username/password of the publisher device as parameters.

Should I see in the mosquitto broker log the subscriber connecting?

What am I doing wrong?
Thanks
Barry

Simon Walters

unread,
Nov 3, 2023, 12:47:35 PM11/3/23
to mq...@googlegroups.com
Hi Barry
Did you subscribe and then publish?

Are you subscribing and publishing on the same topic?
regards
Simon


--
To learn more about MQTT see https://mqtt.org
---
You received this message because you are subscribed to the Google Groups "MQTT" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mqtt+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mqtt/66b8206a-c29d-47f2-a10c-53766db508b3n%40googlegroups.com.

Andy Stanford-Clark

unread,
Nov 3, 2023, 1:50:37 PM11/3/23
to mq...@googlegroups.com
You have to subscribe to the same topic that you publish on… the suscriber is telling the broker “I am interested in publications on topic X”.

From the log, you subscribed to BCS2/sub, but published to BCS2/pub. Totally different topics, so the broker didn’t send the message to the subscriber.

You also need to subscribe before you publish, and leave the subscriber running while you publish from somewhere else (another window, or another device). 
For QoS0 messages, there is no memory in the broker: if there are no subscribers to a particular topic when the publication comes in, it will just drop it on the floor.

Also, you mentioned usernames and passwords - it’s possible your broker is set up not to allow publications (or subscriptions) on the topics you’re using.
Probably best to start without any access control lists at all, and add them in later when you’ve proved it’s all working.

Andy

Barry Nelson

unread,
Nov 8, 2023, 5:54:11 AM11/8/23
to mq...@googlegroups.com
Hi,

Thanks for the information.
I tried to stop/remove the subscriber settings on the barcode device but it wont let me leave them blank.
So it's forcing the barcode scanner to be both a subscriber and a publisher.
Can I have another subscriber to the broker other than the barcode scanner? 2 subscribers to the same topic...

You say I should subscribe first, so if I turn off the barcode scanner (so nothing is connected to mosquitto broker)  run: mosquitto_sub -h localhost -t BCS2/pub -d -v
Nothing shows up in the broker log - is that normal? I would expect to see the sub request in there?

08-11-2023 T10:47:03: mosquitto version 2.0.18 starting
08-11-2023 T10:47:03: Config loaded from mosquitto.conf.
08-11-2023 T10:47:03: Opening ipv6 listen socket on port 8888.
08-11-2023 T10:47:03: Opening ipv4 listen socket on port 8888.
08-11-2023 T10:47:03: mosquitto version 2.0.18 running

I think my first issue is that mosquitto_sub does not seem to work at all.. Do I need to specify a port or something in the mosquitto_sub command?

Thanks
Barry



You received this message because you are subscribed to a topic in the Google Groups "MQTT" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mqtt/KXX8pvT3lqU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mqtt+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mqtt/B1464879-7A4F-4A5B-8EC7-0716DB72A4C0%40stanford-Clark.com.

Andy Stanford-Clark

unread,
Nov 8, 2023, 7:03:42 AM11/8/23
to mq...@googlegroups.com
Hi, 
yes, it doesn’t matter if the scanner is also a subscriber.. just subscribe to “fred” or something so it gets nothing.

>Can I have another subscriber to the broker other than the barcode scanner? 2 subscribers to the same topic…
yes, that is the whole point of pub/sub messaging … one-to-many broker-based messaging.

So you can have as many susbcribers as you like connecting to the broker, each with their own subscriptions (which could all be the same if you want). 
The point here is that the publisher (the scanner) never needs to know or care how many (if any) subscribers there are to its publications - the broker sorts all that out - so if you add another subscriber later, you don’t need to go back and change anything on the publisher (which might be some inaccessible device somewhere).

You appear to be running mosquitto on a non-standard port… the normal port for MQTT is 1883.
So that is where mosquitto_sub will try to connect if you don’t tell it otherwise. 

And yes, you can specify the port for mosquito_sub so it connects to your broker on 8888
add “-p 8888" to the command line.

You’re nearly there, I think ;)

Regards
Andy


Barry Nelson

unread,
Nov 8, 2023, 9:06:28 AM11/8/23
to mq...@googlegroups.com
Hi,

Yes I am there now thanks!
Changed back to default ports to make it easier.

I can now have fun playing around with a load of IP enabled barcode scanners..

Much appreciate your help..

Thanks
Barry


Andy Stanford-Clark

unread,
Nov 8, 2023, 10:56:49 AM11/8/23
to 'Simon Walters' via MQTT
You are most welcome :)

Andy

Reply all
Reply to author
Forward
Message has been deleted
0 new messages