How can I build my topics if I have multiple machines and users?


I would like to know how can I build the next scenario.

I have a web application that is communicating with a multiple machines.
The user 1 is using machine A, and the user 2 is using machine B
How can I build my logic or topics so the messages that user 1 is sending to machine A only arrives to that machine, and the messages that user 2 is sending to machine B only arrives to that machine?

Hi @cao95 ,

Thanks for your question. Please help me understand your scenario. From your description is seems like this:

Publisher User 1 → publishes MQTT messages to the topic ‘machine-a’
Subscriber Machine A ← subscribes to the topic ‘machine-a’ and receives only these messages.

Publisher User 2 → publishes MQTT messages to the topic ‘machine-b’
Subscriber Machine B ← subscribes to the topic ‘machine-b’ and receives only these messages.

Probably your scenario is more complex, otherwise you would not open this question. Please feel free to give us more details.

Dasha from HiveMQ Team

Yea that’s the idea.
There is a way to make topics dynamic?
Or is it a good idea to have thousands of topics?

I mean if I have 20 machines and each machine has one user, and I have 10 basic topics and then when the user is connected to the system I have to make that topics dynamic to to find out which user is acting on which machine so the machine A only gets the messages from the user 1 and the machine B only gets the messages from user 2.

For example:

Basic topic: “machineStatus”
Dynamic topic: “machineStatus” + user_id = machineStatus1

And so on, the amount of topics will growth exponentially.

There is any way to do this? Or maybe another way?

Thank you so much!

Hey @cao95 ,

There is actually no problem for HiveMQ broker to have thousands topics. Topic only “exists” while there are subscribers to it. Once all subscribers disconnect and published messages expire, the information about the topic will be cleared from the broker.

May be aa a more elegant solution for your scenario you will find interesting such feature of MQTT Broker as Wildcard Subscriptions.

Then, for example, you can have a tree of topics like these:



And then the User1 can for example use a topic filter #/user1 to subscribe to everything that you want the User1 to “see”.

We have a very good article explaining the single level wildcard (+) and the multi level wildcard (#): MQTT Topics, Wildcards, & Best Practices - MQTT Essentials: Part 5

Wildcard subscriptions might be not enabled in your broker, here you find instructions how to enable: HiveMQ Configuration :: HiveMQ Documentation

I hope this helps,
Dasha from HiveMQ Team

That’s great!

I will take a look for sure, thank you so much :smiley:

Hi Daria!

I’ve taken a look and it seems that is the solution I need.
I’m using SpringBoot for building the logic. Do you have any SpringBoot examples for this?
On the other hand I would like to know (for good practices) when would be for a user the right time to subscribe to a topic.

Thanks a lot!