how can I ban or keep on hold a topic in mosquitto broker? - mosquitto

I need to block or delete the topic from mosquitto broker.
Is there any method to delete the topic from the broker ?

As asked your question doesn't make sense. Topics don't really exist in the broker, what exists is a list of topic patterns that each client is subscribed to. The broker matches the topic field on incoming published messages against each clients set of patterns (because it's possible to subscribe to wildcard topics) and if it matches then it delivers the message to that client.
The another possible interpretation of your question is that you want to remove a retained message. Retained messages are held by the broker and delivered to every client that subscribes to a matching pattern when they subscribe. To do this you need to publish a message with a null payload and the retained bit set to the matching topic. With mosquitto_pub you do that as follows:
mosquitto_pub -t foo/bar -r -n
-t sets the topic
-r sets the retained bit
-n sets a null payload
If you mean how do you stop users from publishing to or subscribing to topics then you need to look at the acl_file section of the mosquitto.conf man page

Related

Route publish message between two MQTT broker

I have a problem with routing publish message between two broker like image below.
How to send message to user B without information about his topic subscribe?
I mean that:
Step 1: I config broker A is bridge broker with broker B
Step 2: User A publish message on topic # to broker A. That's mean broker B will receive message and both of User A and user C will receive this message.
If I only want User C receive message, in STEP 2 I must publish message on exactly topic User B are subscribing. For example:
User A publishes message on topic /home/userB on Broker 1 and User B subscribes topic /home/userB on Broker 2.
And here is my trouble, in practice, Broker 1 represents Organization 1 and Broker 2 represents Organization 2.
User A under Organization 1 can't have information about topic that user B (under Organization 2) subscribe .
The more IOT systems I put in using MQTT, the more I see this use case.
What you need is an MQTT Proxy. So instead of setting up a "bridge" between the two Brokers, you setup a Proxy that has defined rules about what can be passed over to the other Broker. So if someone Publishes to '/home/UserB' on Broker #1, and there is no 'rule' to pass it on to Broker #2, that is as far as it goes.
The idea is that your proxy Connects to the other Broker, and waits to Publish to it. A list is setup of Topics or Topics/# that will be Published over on the other Broker. I have also setup translations as well IE> /home/UserB => /Org1/status/event to hide what the actual original Topic was.
If the amount of cross-organization traffic is not too heavy, you can do this inside a Container.
I'd post some code, but all the MQTT Proxies I have written is Customer IP. Its not terrible difficult code to write. ...maybe I should write a Public Domain version...

Subscribe to topic with multiple levels in Config file?

I am trying to subscribe to multiple topic levels in the configuration file and the aim is whenever I launch the MQTT, the broker has those topics, after which when I enter a wild card it string compare and checks if the topic is on the broker and if yes it extracts the data and if not it jumps to the next branch level
I have tried to write a level topic tree but dont know how to subscribe in the config file, as I am most sure if mosquitto.subscribe will work there or not
Below shows the snippet of the topic tree,
tree is the root node,
sub_branch = tree/sub_branch;
sub_branch_1 = tree/sub_branch_1;
branch_1 = tree/branch/branch_1;
branch_2 = tree/branch/branch_2;
After launch, the topics are on the broker and I use wildcard
tree/#;
this should give me data for all the branches
and if topic = tree/ranch; its an error(wrong topic) and ask for the next one
Any help is much much appreciated!
You do NOT configure topics on the broker, the broker has no knowledge of what topics a client might publish messages to or what topics a client might subscribe to (apart from in any Access Control Lists).
As far as the broker is concerned topics don't exist until a client publishes a message, at which point it does the following things in order:
Checks the ACL for that user/client if one exists to see if there is a pattern matching the incoming topic and accepts or discards the message if there is a match
Assuming it accepts the message it then searches the list of topic patterns for all subscribed clients.
If there is a match for a client it checks that client's/user's ACL to see if there is a match and if allowed sends that message to the client.

Can mosquitto forward the ClientID of a messages sender?

I am using the mosquitto MQTT Broker.
Also, I have multiple (currently 10, but the number will increase) clients that publish some sensor data periodically to topic A. These clients are technically identical, but do have a unique identifier (serial number).
I also have a client that subscribes topic A in order to receive the published messages and persist the sensor valus in a database.
I certainly need to know which Sensor (i.e. client) has sent a particular value.
As a solution, one could just append some Sensor ID to the payload of each published message. But since the sensors access the broker via GSM, I need to keep the traffic low, so I am trying to avoid that.
I assume, the Broker itself knows which message comes from which client, especially when using perisistent connections, i.e. clean_session=False. Is that correct?
If yes, is there any chance that the subscribing clients can obtain the client_id when receiving the message?
Can it be configured in mosquitto? Or is it default behavior and I am missing something?
I am using paho-mqtt 1.3.1 for all clients.
No, the client id is not part of a published message. It is only used to identify the client to the broker when the connection is established in order to determine if stored messages and persistent subscriptions should be honoured.
The easiest solution is to use a separate topic for each sensor but with a shared root. e.g.
sensor 1 publishes to A/1
sensor 2 published to A/2
The client would then subscribe to A/+ this would then receive all the messages and can use the second half of the topic to determine which sensor it came from.
The other options is as you suggested which is to include the id in the payload.
Sending the client-id with payload(message) is possible. But you need to use delimiters in payload(message) at publisher side . Example: Publisher sends the payload as "client-ID=3 - temperature = 29 " . At the subscriber side , you remove the delimiters using strtok() .
There is no configuration available at broker side.
Per my experience with mosquitto, I don't think there is an option for mosquitto to change either the topic or the payload when re-publishing a received message.
However, I think it is just an implementation issue.
Theoretically, I think it is OK and good to support such kind of feature, since it does not violate MQTT specification at all.
(http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/csprd02/mqtt-v3.1.1-csprd02.html#_Toc385349773, Section 3.3.2.1)
However, since the Server is permitted to override the Topic Name,
it might not be the same as the Topic Name in the original PUBLISH Packet.
The pratical solution for your current problem is, as pointed by #hardillb, either publishing using different topics but receiving using a topic with wildcard (+ or #), or, containing publisher information in payload.

MQTT: Subscribe to topics that no client subscribed to

I would like to have a "default" client that gets all MQTT messages on topics that no one subscribed to. Is that possible?
I read about $SYS topics but they don't seem to provide a solution.
No, it is not possible to know what topics every other client is subscribed to easily/in a browser independent way. You may be able to build a list of what topics clients are subscribed to from the info in the $SYS/# topic tree on some broker implementations, but that won't help you find the inverse, which is what you are asking for.
The only option would be to subscribe with the wildcard topic of #. This would get all (assuming no ACLs in place) messages published to all topics.
If you could build the list I mentioned in the fist paragraph then you could use this as a filter.

how to restrict access of the subscribers to the published messages

i have four trucks connected to a mqtt broker, and I have four Apps/devices. the trucks publishes messages and the Apps subscribe to these message
is there any way to restrict the access of the devices to the message published by the trucks? in another words, lets assume truck1 publishes the following messages (truck1_msg1, truck1_msg2, truck1_msg) I want App1 able to subscribe and listen to the messages from truck1 only and never be able to subscribe and see any other messages published by other trucks. is it possible? would you please let me how can I do it?
note: all the trucks and the Apps are connected to the same broker, and lets assume it is Mosquitto
Most MQTT brokers support topic level ACLs for a given user so assuming each truck publishes messages to a distinct topic (or topic prefix as ACLs tend to support wild cards) and each truck and app has it's own user you should be able to arrange any segregation of access you need.
Each broker have different mechanisms for managing these ACLs, for example here are the details for mosquitto.
The documentation for mosquitto's ACL format can be found in it's man page here: https://mosquitto.org/man/mosquitto-conf-5.html
You add an ACL file to the mosquitto.conf with the acl_file option:
acl_file /path/to/acl/file
The ACL file format looks like this:
user <username>
topic [read|write|readwrite] <topic>
You can have multiple topic lines per user.
Details of how to enable user authentication is also in the man page.

Resources