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.
Related
It is possible to get notify when another client was connected to same topic which is subscribed by same topic in mqtt.
ex: a client subscribed to : app/id another client also subscribed to app/id then both clients will get a message about number of clients subscribed to this particular topic.
if it is possible then please let me know.
The whole point of a pub/sub architecture is to unlink the producer of information (publisher) from the consumer (subscriber). Producers just publish to a topic and the broker deals with routing that message to any consumers that may have a matching topic pattern subscription.
There is no way to count how many clients may receive a given message at any particular time for the following reasons:
A subscriber may be using a wildcard topic pattern that happens to match the topic of the published message.
A subscriber may have a persistent session with a matching topic pattern but if currently off line and will receive the queued message when it next connects
With shared subscriptions there could be any number of clients pooled together subscribed to a given topic pattern, but only one of them will receive any given message as they are load balanced round the group.
There is no guarantee that there are ever any subscribers, in the same way there is no end to end delivery guarantee only between a single client and the broker.
In a sensibly implemented broker, subscription topic pattern matches are only evaluated when a new message is published, so the best you could ever get is how many clients the last message with a given topic was delivered to, as there is no efficient way to calculate the number in advance.
In the same way as your last question about topics, if you think knowing this is required then you need to rethink your design or your use of MQTT.
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.
I know that using MQTT topics devices can subscribe to them. But is there any way that a IoT device can send some message to a target IoT device (by device id or something) without using a topic or is there any standard topic for this scenario?
There is no way to communicate without a topic, but you can create a topic for any purpose. So typically if you wanted to send a message to another client, you would publish it somewhere in the hierarchy of topics to which that client is subscribed.
That could be as simple as something like device/12345/inbound or whatever you prefer. And because topics can have hierarchy, in addition to whatever detail you put in the body, you can also encode categorization of your message into the topic, much as RESTful APIs often do in a URL.
A good reason for using target-specific (or owner-account-specific) topics is that the most easy solutions for MQTT security compartmentalization are topic-scope.
Is it possible to that two clients are subscribed to same topic in MQTT broker. And based on authorization, some of the massages are sent to one client and not other (subscribed on same topic).
No
Any number of clients can be subscribed to a given topic, but you can not limit which of those clients can receive messages on that topic on a per message basis. It is all the topic or none.
There is no direct solution for this approach. But however I have a workaround for this solution.
Say you have two client with ClientId: 1 and 2
Say your topic structure is : stackoverflow/data/{Client-ID}/sensor/ingress
While defining the authorization parameter you can assert with the permission as +/+/{Client-ID}/+/+
This will make sure that, the topic structure will remain the same, but at the same time, the client will be able to publish and subscribe data only from their own topics.(topics which have their client-Id).
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.