Archive
Disconnections And Heartbeat

Disconnections And Heartbeat

2024-11-25 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 st

Related articles

How did Angus Cloud die? Best VPNs for China Still Working in 2024 Easy Fix: VPN Connected But Not Working [No Internet Access] Black Myth: Wukong NordVPN Download (2024 Latest) How to Unblock TikTok at School or Anywhere

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.

graceful And non – graceful Disconnect

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.

heartbeat

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 property . It can also be update if / when need from the device .

When changing BLYNK_HEARTBEAT ,keep in mind that it will be multiplied by 1.5. For example: if you configure 10 second heartbeat, the device status will be changed in 15 seconds after not getting a reply.

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.

example

Since some of the scenarios are non-trivial, we’ve prepared a few examples to better explain them:

1. Device disconnects. (Graceful)

setup : connection Lifecycle has Online and Offline status with 5 minute of wait time in – between . let ‘s check the timeline

10:50:00 Device disconnects from server
The server starts 5-minute timer according to settings. Device status is ONLINE

There is no new connection from the Device

10:55:00 Server sets device status to OFFLINE

2 . device disconnect , then reconnect . ( graceful )

setup : connection Lifecycle has Online and Offline status with 5 minute of wait time in – between .

10:50:00 Device disconnects from server
The server starts 5-minute timer according to settings. Device status is ONLINE

10:51:59 Device connects to the server again. Device status remains ONLINE 

3 . Device is disconnects disconnect , reconnect , and disconnect again . ( graceful )

setup : connection Lifecycle has Online and Offline status with 5 minute of wait time in – between .

10:50:00 Device disconnects from server
The server starts 5-minute timer according to settings. Device status is ONLINE

10:51:59 Device is connects connect to the server again . device status is remains remain ONLINE 

10:52:00 Device disconnects from server

10:57:00 Server sets device status to OFFLINE because timer 
started at initial device disconnection time (at 10:50:00) was reset when the device connected back again at 10:52:00

4 . Device is disconnects disconnect . ( non – graceful )

Setup: Connection Lifecycle has Online and Offline statuses with 2 minutes of wait time in-between. heartbeat is 45 sec.

10:50:00 Device sends a message to the server

10:50:01 Device loses WiFI connection. The server is unaware of this disconnection.
The device is consider to be ONLINE . 

The server is waits wait for the heartbeatInterval ( 45 second ) * 1.5 = 67 second . 

10:51:08 The Server is closes close the connection since there is no heartbeat . 
The server initiates wait time between statuses. However, since the server already waited for 67sec, the server will only wait for 53sec more (120sec – 67sec = 53sec).

... no new connection from the device 

10:52:01 Server applies the OFFLINE status.

5. Device disconnects, reconnects, and disconnects again, (Non-graceful)

Setup: Connection Lifecycle has Online and Offline statuses with 1 minute of wait time in-between. heartbeat is 45 sec.

10:50:00 Device sends a message to the server

10:50:01 Device loses WiFI connection. The server is unaware of this disconnection.
The device is consider to be ONLINE . 

The server waits for the heartbeatInterval * 1.5 seconds = 67 seconds. 

10:51:08 The Server is closes close the connection since there is no heartbeat . 
The server checks for wait time between statuses. It has already waited longer than 1 min, so OFFLINE status is applied immediately.

Last updated