How to see message MQTT properties when using mosquitto_sub command - mosquitto

I'm running Mosquitto command mosquitto_sub to subscribe to a topic and see the messages published there.
I would like to be able to see the MQTT properties used when someone publishes a message in that topic.
Is there any way?
Thanks

You can use the format option (-F) that takes a pattern, for example:
mosquitto_sub -t <topic> -V mqttv5 -F %C
In the example above we are printing the Content type property (%C).
Don't forget the version option (-V) using mqttv5.
You can find more printable properties here: https://mosquitto.org/man/mosquitto_sub-1.html#outputformat (see MQTT related parameters)

Related

Command to display all delivered messages to a Mosquitto Broker?

I have been using following command to see published messages on the Mosquitto broker:
mosquitto_sub -h IP_ADDRESS -t TOPIC_NAME
However, when I run this command I can see only the recent messages not all published messages. In Mqtt client, I can see messages delivered.
Is there a command to see all messages that were delivered to the broker?
The command only shows messages when we start subscribing?
I have used value 2 for Quality of Service in message properties.
MQTT as a protocol doesn't store all messages, no matter what QOS they were published/subscribed at.
It's also important to remember that QOS is only for one leg at a time. E.g. a message published at QOS2 is ensured to arrive at the broker, but makes no promises about it's delivery to any clients, that is down to what QOS those clients subscribed to the topic at.
MQTT will queue messages for a known client if it has previously subscribed at QOS 1 or 2 and reconnects with the cleanSession flag set to false. This connection needs to exactly the same client id as the previous session.
So if you run the following command:
mosquitto_sub -h [ip addr] -t [topic] -c -i [clientid] -q 2
This will create a persistent session, if you then disconnect (kill mosquitto_sub) when you run the same command again (making sure to keep [clientid] the same the broker will deliver all the messages sent while the client was disconnected. But there is no way to get messages published before the session was established by running the command the first time.
-c tells the client to set the cleanSession flag to false.
-i [clientid] sets the client id
-q 2 sets the subscription QOS to 2
If this is to debug a problem then one possible option would be to ramp up the mosquitto instances logging level then it will log absolutely everything, but this will produce a LOT of output.

Get list of retain messages

I have feeling that my client not sets "retain" message flag. Is ti possible somehow get list of retained messages in Mosquitto MQTT broker? How?
When the message is received by a client it will have the retained bit set in the header, so you should be able to check from any client, how you do that will depend on the client library you are using.
Depending on what rate the client is publishing messages (e.g. there is a long enough gap between messages) you could just use the mosquitto_sub command to check. If you know when the last time the client published just wait until afterwards and then subscribe with the command line tool, if it prints a message immediately then there was a retained message.
The other option is to examine the mosquitto persistence database, included in the source code in the src/db_dump directory there is a tool to walk the content of the database. Retained messages should get written to the database so should allow you check. I don't think the tool is build and included in any of the binary install packages so you will have to build it yourself.
With mosquitto version 2.0.14 MQTT v5.0/v3.1.1/v3.1 broker, you can get a list of retained messages via:
$ mosquitto_sub -u user -P pass -t "#" -v --retained-only

How to recieve messages for offline clients using QoS MQTT in eclipse-mosquitto?

My mosquitto.conf is (This is the entire conf file since everything else is just commented out)
log_dest file /mosquitto/log/mosquitto.log
log_type all
connection_messages true
log_timestamp true
log_timestamp_format [%H:%M:%S]
persistence true
persistence_location /mosquitto/data/
listener 1883
listener 9001
protocol websockets
Then i run
docker exec -it ecs-Eclipse_mosquitto_MQTT-11-Eclipse-mosquitto-MQTT-eafxxxxxxx mosquitto_pub -t presence -m hellothere -q qos=2
(NOTE I dont have any active subscribers yet online)
Navigated to /mosquitto/data and there was indeed a file called mosquitto.db
So i could deduce that my message hellothere is stored in mosquitto.db
Then i ran the command
docker exec -it ecs-Eclipse_mosquitto_MQTT-11-Eclipse-mosquitto-MQTT-eafxxxxxxx mosquitto_sub -t presence -q qos=2
What i expect
For the MQTT broker to send the stored message since subscriber is now online
What actually happens
indefinite waiting for the message
Maybe i am missing something ?
I think the relevant bit of the mqtt spec is:
When a Server takes ownership of an incoming Application Message it
MUST add it to the Session state of those clients that have matching
Subscriptions.
In your case when the message is published there are no clients with matching subscriptions (because you have not run mosquitto_sub at that point) so it does nothing further.
If you want messages to be stored for a client you must first connect, as that client, and subscribe to the relevant topic (with QOS>0 and CleanSession=0). After doing that any messages received whilst the client is offline will be stored and delivered when it reconnects (as long as CleanSession=0).
Note: To do the above with mosquitto_sub you will need to use the --id parameter to set the client id (messages will only be queued for the specific clients that have subscribed to the relevant topics). You will also need the -c flag to disable the 'clean session' flag.
So the steps will be as follows (I have removed the extra docker bits - you can add these back in):
mosquitto_sub -c --id subscriber1 -t presence -q 2
This will subscribe and then wait for any messages; you can safely close it (it's the subscription bit that needs to happen). Next publish your messgae:
mosquitto_pub -t presence -m hellothere -q 2
Now you can start mosquitto_sub again to retrieve the stored messages:
mosquitto_sub -c --id subscriber1 -t presence -q 2
Warning: When you subscribe like this the broker will store any messages received whilst you are offline. This can become an issue if you are testing a broker that processes a lot of messages (because storing all of the messages takes space). Probably not an issue here as you are only testing but something to bear in mind... Running mosquitto_sub without the -c will clear any subscriptions - there are also options in the mosquitto config that allow you to limit the number of messages held and how long they are held.
MQTT doesn't work that way.
Messages are only queued for clients that have previously been connected, it is a pub/sub system not a message queuing system.
A client needs to have been connected in the past and subscribed to the topic the message in question. It then needs to reconnect after the message has been published with the cleanSession flag set to false and using the same client id.
You can read more about this on the HiveMQ blog here

The message wasn't mapped in Eclipse Vorto Semantic Normalizer middleware

I'm flowing this link, all is OK before step 3.4. But when I use this mosquitto_pub command to publish the message to the Bosch IOT hub.
mosquitto_pub -h mqtt.bosch-iot-hub.com -p 8883 -u 4712#t65a6a75c6b4f429981ba16d1b3d5f414 -P secret -t telemetry/t65a6a75c6b4f429981ba16d1b3d5f414/4712 -m '"103.3223","3.2322"' --cafile iothub.crt
But the output log didn't have any message like the tutorial
Why? What's the problem?
It seems that you have not started the normaliser middleware with any outbound handler plugin. So in your case, in order to send any incoming telemetry data to an AMQP topic, you need to start the middleware with the AMQP plugin
Once done so, you should be able to see the logs in the middleware dashboard, under monitoring, appearing as outbound messages.
Sorry for the confusion, I have changed the tutorial in order to make it clearer, see https://github.com/eclipse/vorto/blob/master/docs/tutorials/create_mapping_pipeline.md#4-setting-up-eclipse-vorto-payload-normalization-middleware-with-vorto-amqp-plugin

How do I start Eclipse-Hono client in MQTT?

I'm having trouble in starting MQTT Client in Eclipse Hono.
I'm using The following command to start the client
java -jar hono-example-0.6-exec.jar --hono.client.host=hono.eclipse.org --hono.client.port=15672 --hono.client.username=consumer#HONO --hono.client.password=verysecret --spring.profiles.active=receiver --tenant.id=bob
which starts the client accepting telemetry data produced by the device, but didn't catch the data published through MQTT.
What may be wrong with this approach?
The command you are using does not start an MQTT client but starts the receiver for consuming (AMQP 1.0) messages from devices belonging to tenant bob. In order to see something happening, you need to have a device that belongs to tenant bob publish some data. If you want to use MQTT for that purpose you may want to use the mosquitto_pub command line client as described in the Getting Started guide. However, make sure that you use the correct username and password. From what I can see in the device registry on hono.eclipse.org you have registered a device with id 1112 and auth-id sensor1. So the command to publish should look something like:
mosquitto_pub -h hono.eclipse.org -u sensor1#bob -Pthepasswordyouregistered -t telemetry -m "hello"
Again, make sure to replace thepasswordyouregistered with the real password that you have registered for device 1112.

Resources