LiamBindle / MQTT-C

A portable MQTT C client for embedded systems and PCs alike.
https://liambindle.ca/MQTT-C
MIT License
774 stars 275 forks source link

Preventing client ID collisions #49

Closed anselmobattisti closed 5 years ago

anselmobattisti commented 5 years ago

Hi

I foud a strange situation, If at the same machine multiple clients was created using the same name they are interferint with each other and the conections stop working.

mqtt_connect(&client, 'CLIENTE NAME, NULL, NULL, 0, NULL, NULL, 0, 400);

to overcome this situations i created a function to generate a random name each time the conection will be created.

Searching on the web i found others thereads talking about this kind of situation, like this, https://github.com/mqttjs/MQTT.js/issues/684

Maybe you could add some random number at the end of client's name before start the conection to the MQTT server.

Thanks for you work

learn-more commented 5 years ago

This is an mqtt feature. If you want a clean session, connect using the flag MQTT_CONNECT_CLEAN_SESSION, or indeed generate a random name.

learn-more commented 5 years ago

There is also the option to connect with an empty client name, but MQTT-C does not support that yet. I have some code to fix that, and will send in a pull request for that somewhere this week.

LiamBindle commented 5 years ago

@anselmobattisti, Thanks for the feature request. I agree with @learn-more though.

The MQTT_CONNECT_CLEAN_SESSION flag should be sufficient. Appending a random string to the client ID that you pass to mqtt_connect() make sense if you have multiple client instances for a single "logical client", but I think that's not something MQTT-C should do itself.

I'm going to close this feature request, but feel free to reopen if I've misinterpreted anything.

LiamBindle commented 5 years ago

@anselmobattisti this could also be accomplished with an anonymous client ID (which @learn-more is adding) if the broker you're connecting to supports it.

See #51

anselmobattisti commented 5 years ago

thaks for the answars,

The MQTT nodejs component, for example, appends a random string at the connection name every new connections.

mosquitto | 1570369305: New client connected from 172.19.0.1 as mqttjs_e769b829 (p2, c1, k60). mosquitto | 1570369833: New client connected from 172.19.0.1 as UVIRW (p2, c0, k400). mosquitto | 1570369868: New client connected from 172.19.0.1 as mqttjs_3eea51ba (p2, c1, k60).

The second one is the random i generate inside a docer container in C

In my case the client is a process inside a docker container and many of this containers are strated at the same time.

The MQTT_CONNECT_CLEAN_SESSION should be used where?

LiamBindle commented 5 years ago

The anonymous client ID functionality added by @learn-more is a clean replacement for what you're doing with the second one. I think that will be merged in the next couple days.

The MQTT_CONNECT_CLEAN_SESSION should be used where?

The MQTT_CONNECT_CLEAN_SESSION flag forces the broker to start the client's session "fresh" (throw out any existing state).

From the MQTT 3.1.1 Spec:

3.1.2.4 Clean Session ... If CleanSession is set to 1, the Client and Server MUST discard any previous Session and start a new one. This Session lasts as long as the Network Connection. State data associated with this Session MUST NOT be reused in any subsequent Session [MQTT-3.1.2-6].

I think you probably want to be using this flag. It will cause the broker to terminate the session when the connection closes (otherwise your broker might accumulate open sessions with closed connections).