moscajs / mosca

MQTT broker as a module
mosca.io
3.2k stars 508 forks source link

Detecting offline client and emitting an event when publishing a QoS 1 packet. #632

Open testark opened 7 years ago

testark commented 7 years ago

Is there any way to detect that (some of) the target clients of a given QoS1 packet are offline and emit an event (such as offline_message) so that one can execute a custom action in such cases (like sending a notification or something)?

aaryadev commented 7 years ago

Setup Last Will and Testament (optional). A broker will publish a message with qos = 0, retain = 0, data = "offline" to topic "/lwt" if client does not send keepalive packet.

Example Syntax mqtt:lwt(topic, message[, qos[, retain]])

testark commented 7 years ago

@aaryadev thanks, but that won't solve my problem.

aaryadev commented 7 years ago

I have set esp8266 mqtt:lwt("iffline, "mydeviceid",0,0) and heartbeat time out is 10 sec so when esp8266 disconnects without mqtt.close() like network failure

mosca server will wait for 10 sec then trigger firebase cloud messaging to my phone

testark commented 7 years ago

@aaryadev yes, but unfortunately that doesn't work on my scenario.

aaryadev commented 7 years ago

@testark What is your scenario.?

chenhuang511 commented 7 years ago

In my case, client (esp8266) often offline then online immediately, but the 'clientDisconnected' event always happen after 'clientConnected'. I change keepAlive from 60s to 15s or use lwt message but it still did not work. I think lwt message is slower than publishing disconnected message to subscribed clients inside 'clientDisconnected' event, because broker must load lwt payload from persistence (in my case is redis).

Is there any way to resolve my issue?

boomfly commented 6 years ago

+1