Is there a way to generate unique id numbers for MQTT clients?

4,323 views
Skip to first unread message

Yaakov Belch

unread,
Jun 30, 2016, 5:04:01 AM6/30/16
to MQTT
Is this at all possible with MQTT?

In my application, I have one server process and many client processes subscribed to a  MQTT broker.  To distinguish all the clients at the application level, I need unique id numbers for them.  Currently, I use random numbers --- which obviously could clash with each other.

I tried to think up some scheme to ensure that the client ids will be unique --- and this seems impossible with MQTT: two new clients are really indistinguishable and there is no way how I could reliably break this symmetry.

I know that MQTT brokers internally assigns new unique ids for clients connecting to a broker (when the clients don't provide thier own MQTT client id).  But these ids stay inside the broker --- the client never sees them.  So, they cannot be used on the application level to identify clients.

Have you ever found a way to reliably identify clients in MQTT without resorting to some outside source of uniqueness?

Yaakov Belch

unread,
Jun 30, 2016, 7:52:59 AM6/30/16
to MQTT
More information: my clients are web browser windows --- here a MAC address is not accessible.  In addition, I can have several clients running on the same computer, so a MAC address would not be unique for my clients.

I checked out the UUID standard and found that the UUID uniqueness comes from randomness --- it's still possible to get clashes.  So, UUID seems to be equivalent what I am already doing.

"Although it is possible that broker implementations could create a custom method of communicating those IDs to the client, though a publish unique to that client."
This sounds interesting.  Are there any brokers that actually do something like this?  Any ready-to-use plugins?

Yaakov Belch

unread,
Jun 30, 2016, 7:56:09 AM6/30/16
to MQTT
Here is a reply that I received privately to my original question:

There isn't anything in the MQTT protocol for providing a random 
identifier to clients. Although it is possible that broker 
implementations could create a custom method of communicating those IDs 
to the client, though a publish unique to that client.

However given that you are talking about the application level, it seems 
like a problem that is not specific to MQTT.

The MQTT 3.1.1 specification states:
- The Server MUST allow ClientIds which are between 1 and 23 UTF-8 
encoded bytes in length
- The Server MAY allow ClientId’s that contain more than 23 encoded 
bytes.
- A Server MAY allow a Client to supply a ClientId that has a length of 
zero bytes.


If you are using embedded devices, could you use the MAC address of the 
device?

I suspect most MQTT server implementations will accept 32-characters, 
which would allow you to use UUIDs - which were designed for the purpose 
of decentrally creating unique identifiers.

--------------------

It seems that this confirms my suspicion that it's difficult or impossible to 
create really unique application-level ids and transmit them through MQTT.

Roger Light

unread,
Jul 1, 2016, 10:40:45 AM7/1/16
to mq...@googlegroups.com
Hi Yaakov,

It seems like the limiting factor for a type 4 UUID is the quality of
the randomness. If you can assume you have good quality randomness
then the 122 bits they use are more than adequate. If you can't rely
on your source of random data then you'd have to go with something
specific to the hardware - which it doesn't sound like is possible in
your case.

If you base62 encode the UUID it will fit in 23 characters.

Cheers,

Roger
> --
> To learn more about MQTT please visit http://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 post to this group, send email to mq...@googlegroups.com.
> Visit this group at https://groups.google.com/group/mqtt.
> For more options, visit https://groups.google.com/d/optout.

Nicholas Humfrey

unread,
Jul 1, 2016, 4:23:58 PM7/1/16
to mq...@googlegroups.com, Yaakov Belch
Hello,

There isn't anything in the MQTT protocol for providing a random
identifier to clients. Although it is possible that broker
implementations could create a custom method of communicating those IDs
to the client, though a publish unique to that client.

However given that you are talking about the application level, it seems
like a problem that is not specific to MQTT.

The MQTT 3.1.1 specification states:
- The Server MUST allow ClientIds which are between 1 and 23 UTF-8
encoded bytes in length
- The Server MAY allow ClientId’s that contain more than 23 encoded
bytes.
- A Server MAY allow a Client to supply a ClientId that has a length of
zero bytes.


If you are using embedded devices, could you use the MAC address of the
device?

I suspect most MQTT server implementations will accept 32-characters,
which would allow you to use UUIDs - which were designed for the purpose
of decentrally creating unique identifiers.


nick.
Reply all
Reply to author
Forward
0 new messages