Electron, MQTT, Subscribe and Sleep Mode

Hi all, I have been experimenting with the MQTT library https://github.com/hirotakaster/MQTT on an Electron and it seems to work OK in that I can publish and subscribe to a broker (hivemq or cloudMQTT etc)
The issue is that the Electron is power hungry and I need a sleep mode. As soon as I use System.sleep(D1,FALLING,SLEEP_NETWORK_STANDBY,30); I run into trouble. The SLEEP_NETWORK_STANDBY leaves the network on line and I can verify this is so by coming out of the sleep and not having to reconnect. It works OK with a publish, I can come back from sleep and publish to the broker. However there are issues with coming out of sleep and subscribing, picking up a message from the broker which has either been left there while I was sleeping or even now while I am awake.
If I go to sleep with the subscription still open I see the broker trying to send me the message because the current to the electron goes up, presumably the cellular receiving something. But of course nothing happens because the CPU is in sleep mode, presumably the message gets lost.
To prevent the broker sending the message while I am asleep I can unsubscribe before I sleep, this works as confirmed by no cellular activity.
Then on return from sleep I subscribe again to the topic. The last message is retained on the server, I can verify that not only because I ask it to be retained but I can subscribe from a PC program and get it. Unfortunately the re subscription on the Electron does not get the message from the broker.
However if I disconnect from the broker before sleep and then reconnect and resubscribe after sleeping, everything works OK. Any message left on the broker while I am asleep is received OK at the Electron.
While there are examples around of sleep and publish, I have not found any which do sleep then subscribe. Has anyone been able to sort this out and come up with a working example?
I acknowledge that my understanding of MQTT is not extensive. I have however tried various QOS settings and they don’t seem to effect the outcome described above. I have not been able to find how the MQTT library establishes a clean session as described in http://www.hivemq.com/blog/mqtt-essentials-part-7-persistent-session-queuing-messages This does not seem to be addressed in the original Arduino library which the Spark one has been derived from either.
So everyone, are there any suggestions?

You have cleared the clean session flag (=persistent session) on your subscription in conjunction with QoS 1 or 2?

This introduction might be interesting for you (esp. part 3)

Thanks @ScruffR, I think that is one of my points, I would like to set the clean session flag false but I don’t see anyway in the MQTT library to do it, am I overlooking something? There is no mention anywhere in the library code that I can see where we can change it. I have not delved into the packet structure to see what is happening but nothing that I can see in the code itself.

So I ran across the following pull request on the particle MQTT repo - https://github.com/hirotakaster/MQTT/pull/47 - which adds clean session flag support. I’m going to try and it and see what’s up. I don’t think I particularly need a persistent session, but I could see how having one might save some overhead on a re-connect for the Electron.

@zhirschtritt Thanks for pointing that out. I will try it in the next few days, see if it is the solution I need

@zhirschtritt Did you have any luck with that changed MQTT library? I tried it and did not have any success, did not seem to be as stable as the original, even without the clean session change.
The original has now been updated to include a setting for the clean session flag. https://github.com/hirotakaster/MQTT I have tried that and it works providing I don’t unsubscribe/resubscribe or disconnect/reconnect I will try and get into the innards of the code and find out what is happening.
One thing I did notice with the connect options is that you can’t seem to set a Qos = 1 with a clean session = false. I think that is important to be able to do that and will reread the MQTT documentation to see what the go is.
Any feedback appreciated

The clean session = false flag is not getting set in the latest revision of the library. I have submitted an issue as I see it. Having been able then to set up a persistent session I can connect, check for messages using loop() then disconnect and
do a System.sleep(D1,FALLING,SLEEP_NETWORK_STANDBY,30); return after 30 seconds, do a connect and read any messages left on the broker while I was asleep.No need to do an unsubscribe/ subscribe