eclipse / paho.mqtt-sn.embedded-c

Paho C MQTT-SN gateway and libraries for embedded systems. Paho is an Eclipse IoT project.
https://eclipse.org/paho
Other
314 stars 178 forks source link

Ping Response Resolver in mqttsn client #243

Open nikhil1983 opened 3 years ago

nikhil1983 commented 3 years ago

Hello,

I am currently using MQTT-SN client to connect to MQTTSN gateway. I have my client connected with a keep alive time of 30 seconds.

So during no activity if my mqtt-sn client is unable to send ping request to gateway or doesn't recieve ping response from gateway, how could I find that ? How could I find my whether my client is still connected or not ? Because broker would close connection after 45 second if it doesn't recieve any response from my mqtt-sn client.

I require help in re-initiating a client connection process with gateway as soon as my client get's timedout due to inactivity.

Can anyone please tell me how could I start new connection after getting disconnected. Or which part of the code processes the ping response information.

campescassiano commented 2 years ago

You can send a PINGREQ to the Gateway, and if you have been disconnected, Gateway will not reply.

But, since it is UDP, you may want to retry sending the PINGREQ more N times, for example, and if after all retries it failed to receive the PINGRESP from Gateway, then you may conclude that your connection to the Gateway was lost somehow. This is a good idea just to make sure that it is not a packet routing problem or something like that.

Thus, you can restart your client-side by sending CONNECT, and start over the connection.

One more thing that would be better before sending the CONNECT, is to send DISCONNECT first, just to make sure (or at least as a best effort) try to tell the Gateway "if you still think I am connected to you, please I am DISCONNECTING". Thus, if for any reason the Gateway still thinks your client is connected, now it will know that your client disconnected, and then the Gateway will release all the resources and it will be ready to wait for any new connection.