LogoLogo
GitHubBlynk WebsiteLogin to Blynk.Console
  • Introduction
  • Getting Started
    • Supported Hardware
    • Quickstart
      • Next Steps After Quickstart
      • Quickstart Device: Code Overview
      • Troubleshooting
    • Device Activation Methods
      • Manual Device Activation
      • WiFi provisioning
      • Static Token
    • Template Quick Setup
      • Set Up Datastreams
      • Set Up Web Dashboard
      • Set Up Mobile App Dashboard
      • Prepare Your Code
      • Test your Template
    • Send Data From Hardware To Blynk
    • Control Devices (GPIOs and beyond)
    • Events
    • Notifications (Alerts)
    • Sign Up / Sign In
  • General Concepts
    • Developer Mode
    • Device
    • Device Template
    • Connection Lifecycle
      • Disconnections And Heartbeat
    • Users
      • Multi-tenancy
    • Organizations
    • Automations
      • Forward Device Data
  • Message Usage
  • Integration Guides
    • Node-RED
    • The Things Stack
      • Getting Started
      • Device Grouping
      • Automated Device Onboarding
      • System DataStreams
    • Blues
    • NCD Industrial Vibration Sensor
    • Particle - monitor with Blynk
    • Particle - control with Blynk
    • AWS IoT Core
  • Myriota
  • OpenWeather
  • Blynk.Console
    • Overview
    • Dashboards
      • Dashboard Widgets
    • Devices
      • Device profile
        • Dashboard
        • Device Info & Metadata
        • Notifications & Events
        • Developer tools
          • General
          • Datastreams
          • Testing
          • Actions Log
      • Actions with devices
      • Segments
      • Filters
      • Notifications Settings
      • Device Sharing
    • Locations
      • Location Profile
      • Assigning Devices to the Locations
    • Organizations
      • Create a Sub-Organization
      • Working with Sub-Organizations
    • Users
      • User Profile
    • Device Templates
      • Working With Templates
      • Info
        • Offline Ignore Period
        • Manufacturer
        • Template ID
        • Categories
        • Hotspot Prefix
      • Datastreams
        • Datastream Settings
          • Name
          • Alias
          • Virtual Pin
          • Color
          • Data Type
          • Min Value
          • Max Value
          • Default Value
          • Save RAW Data
          • Invalidate Value
          • Wait for confirmation from device
          • Sync with latest server value every time device connects to the cloud
          • Expose to Voice Assistants
        • Virtual Pin
        • Location
        • Enumerable
      • Web Dashboard
        • Multiple Dashboard Tabs
      • Metadata
        • Metadata Tutorial
      • Connection Lifecycle
      • Events
        • Custom Events
          • Event Settings
          • How to Send/Log Events
          • Content Events
        • Notifications Settings
          • Custom Sounds and Critical Notifications
      • User Guides
      • Assets
    • Widgets (Console)
      • Switch
      • Slider
      • Number Input
      • Image Button
      • Web Page Image Button
      • LED
      • Label
      • Gauge
      • Chart
      • Map
      • Image Gallery
      • Custom Chart
      • Heatmap Chart
      • Video
      • Bitmask Table
      • Gradient Ramp
      • Terminal
      • Segmented Switch
      • Alarm & Sound Widget
      • Modules
    • Blynk.Air
      • Shipment Details
      • Shipment Management
        • New Shipping
      • Device shipment statuses
      • User-Controlled Shipments
    • Settings
      • Organization Settings
        • General
        • Users
        • Locations (Job Site or Facilities)
        • Tags
      • Roles and Permissions
      • Developers
        • OAuth2
        • Webhooks
        • Create New Webhook
      • Integrations
    • User Profile Menu
    • Limits
  • Blynk.Apps
    • Overview
    • Mobile Dashboard Editor
    • Device Header Constructor
      • Header Design
      • Header Mini Widgets
        • Connection Status Widget
        • Last Reported Widget
        • Tabs Widget
        • Datastream Value Widget
        • Image Widget
        • Battery Level Widget
        • Signal Level Widget
        • Tags Widget
      • Header Buttons
    • Pages
    • Widgets (app)
      • Common Widget Settings
      • List of Datastreams types supported by Widgets
    • Widgets Controllers
      • Button
      • Styled Button
      • Icon Button
      • Image Button
      • Slider
      • Vertical Slider
      • Step Slider
      • Vertical Step Slider
      • Joystick
      • zeRGBa
      • RGB Light Control
      • Step H
      • Step V
      • Slope Control
      • Switch
      • Level Slider
      • Level Slider with Switch
    • Widgets Displays
      • Value Display
      • Labeled Value
      • LED
      • Gauge
      • Radial Gauge
      • Enhanced Gauge
      • LCD
      • Simple Chart
      • SuperChart
      • Terminal
      • Video Stream
      • Level H
      • Level V
      • Image Gallery
      • Gradient Ramp
      • Icon
      • Image Animation
      • Lottie Animation
    • Widgets Interface
      • Tabs
      • Menu
      • Map
      • Text Input
      • Numeric Input
      • Time input
      • Segmented Switch
      • Icon Segmented Switch
      • Text
      • Formatted Text
      • Dynamic Spacer
    • Widgets Other
      • Music Player
      • WebPage Button
      • WebPage Image Button
      • Alias Name
    • Main Menu
      • My Profile
      • Organization
      • Settings
      • Help
      • About
      • Log Out
    • Devices
      • Add New Device
    • Automations
    • Notifications & Events
  • Blynk.Edgent
    • Blynk.Edgent overview
    • Blynk.Inject and Blynk.Air
    • OTA: Firmware Over-The-Air updates
  • Blynk.NCP
    • Blynk.NCP overview
    • Supported Connectivity Modules
    • OTA: Firmware Over-The-Air updates
  • Blynk Library - firmware API
    • Installation
      • Install Blynk Library in Arduino IDE
      • Install Blynk Library for Platformio.org
      • Install ESP8266 core for Arduino IDE
    • Configuration
    • Connection Management
    • Device Online/Offline Status
    • Digital/Analog Pins
    • Virtual Pins
    • Widget Properties
    • State Syncing
    • Timers
    • Time (RTC clock)
    • Timezone / Location
    • Log Event
    • Metadata
    • Debug
    • Reboot
    • Over-The-Air Firmware Updates (OTA)
    • Other
    • Limitations and Recommendations
    • Blynk Protocol
  • BLYNK.CLOUD MQTT API
    • Device MQTT API
      • Authentication
      • Topic Structure
      • Datastreams
      • Widget Properties
      • Events
      • Metadata
      • Timezone/Location
      • OTA
      • Miscelaneous
      • Code Examples
  • BLYNK.CLOUD HTTPS API
    • Device HTTPS API
      • Get Datastream Value
      • Get Multiple Datastream Values
      • Get Historical Data From Device
      • Update Datastream Value
      • Update Multiple Datastreams Simultaneously
      • Upload a Set of Timestamped Data
      • Update Widget/Datastream Property
      • Send/Log An Event
      • Get Device Metadata Value
      • Update Device Metadata Value
      • Is Device Connected
      • Upload a File
      • HTTPS API Troubleshooting
    • Platform API
      • Authentication
      • Ogranization API
        • Get Own Organization Info
        • Get Organization Info
        • Search Organizations
        • Create Organization
        • Get Static Tokens
        • Get Organization Tags
        • Get Organization Automations
      • Devices API
        • Get All Devices
        • Search Devices
        • Get Devices by Owner Email
        • Get Devices in user organization
        • Get Recently Activated Devices
        • Get Device Info
        • Get Connection Status
        • Create Device
        • Edit Device
        • Get Datastream Values
        • Update Datastream Value
        • Update Multiple Datastreams Values
        • Import Datastream Values
        • Update Datastream Property
        • Get Datastream Historical Data
        • Get Device Metadata
        • Update Device Metadata
        • Get Device Tags
        • Get Device Timeline Log
        • Log a Device Event
        • Get Actions Log
        • Erase All Data
        • Remove Device Owner
        • Transfer Device
        • Delete Device
      • Users API
        • Get All Users
        • Search Users
        • Create New User
        • Invite User
        • Get User Info
        • Update User Role
      • Templates API
        • Get All Templates
        • Get Template Info
        • Get Template Metadata
        • Get Template Datastreams
        • Get Template Events
    • Security
  • Downloads
    • Blynk Mobile Apps
    • Blynk Library
  • Troubleshooting
    • General Issues
    • Developer Mode
    • Changes from Blynk 0.1
      • Migrating to the new Blynk - Full Guide
    • Glossary
    • Links
  • Commercial Use
    • Deploying Products With Dynamic AuthTokens
    • Deploying Products With Static Tokens
    • Working With Clients
    • Supported topologies
    • Business Plan (White Label Solution)
      • App Publishing Process And Timeline
      • What's Needed To Publish Your Apps And Go Live
      • Branding Materials
      • Custom Email Address For Transactional Emails
      • Application Settings
        • General
        • Design
        • Mobile Apps
        • Sign Up
  • Add-Ons
    • Add-on list
    • Amazon Alexa
    • Google Assistant
    • Localization
    • Database Access
    • Marketing
Powered by GitBook
On this page
  • Graceful And Non-Graceful Disconnect
  • Heartbeat
  • Examples
  • 1. Device disconnects. (Graceful)
  • 2. Device disconnects, then reconnects. (Graceful)
  • 3. Device disconnects, reconnects, and disconnects again. (Graceful)
  • 4. Device disconnects. (Non-graceful)
  • 5. Device disconnects, reconnects, and disconnects again, (Non-graceful)

Was this helpful?

  1. General Concepts
  2. Connection Lifecycle

Disconnections And Heartbeat

PreviousConnection LifecycleNextUsers

Last updated 1 year ago

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.

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 . It can also be updated if/when needed 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.

Examples

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 statuses with 5 minutes 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 disconnects, then reconnects. (Graceful)

Setup: Connection Lifecycle has Online and Offline statuses with 5 minutes 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 disconnects, reconnects, and disconnects again. (Graceful)

Setup: Connection Lifecycle has Online and Offline statuses with 5 minutes 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

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 disconnects. (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 considered to be ONLINE.

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

10:51:08 The Server closes 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 connections 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 considered to be ONLINE.

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

10:51:08 The Server closes 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.

property