Disconnections And Heartbeat
Last updated
Was this helpful?
Last updated
Was this helpful?
Blynk always tracks the device's online status in real-time. As soon as the device connects to the server or sends data, the server sets the device status to Online. However, tracking disconnections is more complex.
When a device closes a TCP connection with commands like connection.close()
/ socket.close()
/ Blynk.disconnect()
- the server gets almost real-time notification regarding the closed connection.
This is called graceful disconnection. This might be common when devices work over the cellular network and the developer intentionally chooses to close the connection to control traffic amount.
However, with WiFi or Ethernet connection, the controlled graceful disconnect is rather an exception: devices are assumed to be always online, but can lose connection for various reasons ( WiFi drop, router reboot, etc.) This is called non-graceful disconnection and Blynk uses Heartbeat to find out about it.
The hardware periodically sends a ping
command to the server (every 45 seconds by default). If there is no ping from the device, the server waits for 22 seconds more (half of the heartbeat interval) and after that assumes that the connection was lost. The status change will be applied in 67 seconds ( 45 + 45*0.5).
This is why you won't immediately see the device going Offline even if you set up the Wait period between Connection Lifecycle Events to 0 minutes.
If there is a Wait period configured in Connection Lifecycle Events, the server will wait for the heartbeat as explained above, and then start the wait period (the 67 seconds will be subtracted from the period).
Heartbeat can be set in the firmware with BLYNK_HEARTBEAT
. It can also be updated if/when needed from the device.
Keep in mind that decreasing the ping interval will increase the traffic amount between your device and server. For products that are sensitive to data bandwidth (GSM devices), it's recommended to use very long ping intervals.
Since some of the scenarios are non-trivial, we've prepared a few examples to better explain them:
Setup: Connection Lifecycle has Online and Offline statuses with 5 minutes of wait time in-between. Let's check the timeline
Setup: Connection Lifecycle has Online and Offline statuses with 5 minutes of wait time in-between.
Setup: Connection Lifecycle has Online and Offline statuses with 5 minutes of wait time in-between.
Setup: Connection Lifecycle has Online and Offline statuses with 2 minutes of wait time in-between. Heartbeat is 45 sec.
Setup: Connection Lifecycle has Online and Offline statuses with 1 minute of wait time in-between. Heartbeat is 45 sec.