mproxy icon indicating copy to clipboard operation
mproxy copied to clipboard

Request support of MQTT Last Will and Testament (LWT)

Open ftylitak opened this issue 5 months ago • 3 comments

Issue

Since the introduction of the mproxy in Mainflux (and now Magistrala), it lacks support of listening MQTT Last Will messages from MQTT broker.

Case

We have the case of devices reporting connection status everytime the connect/disconnect to the network through a simple MQTT publish. To cover edge cases of devices that are forcefully unplugged from the power, we also setup Last Will on every connection to report that the device is disconnected if not properly terminated.

This worked perfectly with Mainflux 0.10.0 that we have been using for a while, though after upgrading to mainflux 0.12.0 that has been using mproxy, these messages we not received.

Investigation results

After investigating the source code of mproxy, we ended up to two keypoints that prevent this feature to function with the current mproxy architecture.

a) at the two streams of each session, only messages comming from client to the broker are notified (passed onto nats). (https://github.com/absmach/mproxy/blob/695f58d860bd1c588e106c9ad679a9cfb9b69dd2/pkg/session/stream.go#L77)

b) the Last Will messages are generated from the MQTT broker only if there are active subscribers at that time. Since the mproxy is only packet forwarer, it is not a subscriber so in the general case, those messages are never generated.

Our workaround

To restore the functionality in our deployment:

a) when a publish message has been received we added a hardcoded check whether the suffix of the topic matches "/connectionStatus" that we use, to call notify regardless the stream direction

b) added dummy subscribers listening to those messages, to force MQTT broker to generate Last Wills when needed.

ftylitak avatar Jan 22 '24 09:01 ftylitak