Phone heartbeats and availability
Phone heartbeats are the liveness signal between an Android phone and HTTPSMS. They show when a phone last contacted the service, let the service detect a silent phone, and trigger a new push request when the signal is late. This status supports message delivery, but it does not itself block message work; the phone receives work through push delivery and then claims the message from the API.
Availability model
Heartbeat records
A heartbeat is a time-stamped report for one phone number. The report includes the app version and whether the phone is charging, so the dashboard can show recent device activity as well as liveness. The Android agent sends active SIM numbers in one authenticated request to POST /v1/heartbeats; the request checks each reported number against the phone API key before the service stores a record for each number. android/…/HeartbeatWorker.kt• L19-40, api/…/heartbeat_handler.go• L95-150
The service also exposes recent records to an authenticated account through GET /v1/heartbeats. Records are scoped to the account and phone number, and the newest record is the useful measure of current contact. api/…/heartbeat_handler.go• L42-92, api/…/mongo_heartbeat_repository.go• L51-113
References: api/…/heartbeat.go• L9-17 api/…/heartbeat_store_request.go• L10-43 api/…/heartbeat_handler.go• L95-150 android/…/HttpSmsApiService.kt• L134-158
Heartbeat monitors
A heartbeat monitor is the service's state for one account and phone number. It links the number to the registered phone, remembers whether the phone is considered online, and tracks the next delayed check. A monitor is created when the phone becomes monitored, and the service starts or restarts its check when the monitor has no active check. api/…/heartbeat_listener.go• L44-65, api/…/heartbeat_service.go• L187-246
The monitor starts online when it is first created. The service later changes that state only when monitoring finds a failed heartbeat or when a heartbeat arrives after the phone was marked offline. The state is therefore a monitored availability view, not a direct network connection state. api/…/heartbeat_monitor.go• L9-29, api/…/heartbeat_service.go• L127-170
References: api/…/heartbeat_monitor.go• L9-29 api/…/heartbeat_service.go• L187-246 api/…/heartbeat_service_test.go• L113-146 api/…/heartbeat_monitor_repository.go• L11-32
Heartbeat monitoring flow
Scheduled heartbeat collection
The Android agent schedules a network-constrained background worker every 15 minutes. The worker sends heartbeats only for active SIMs and records a local send time after a successful API call. The app can also send the same report when the user asks for one or when the service sends a heartbeat prompt. android/…/MainActivity.kt• L74-79, android/…/MainActivity.kt• L128-145, android/…/HeartbeatWorker.kt• L11-43
The collection path is:
sequenceDiagram
participant A as Android agent
participant H as Heartbeat API
participant S as Heartbeat service
participant M as Monitor
A->>H: POST active phone numbers and device state
H->>S: Validate and authorize phone report
S->>S: Store time-stamped heartbeat
S->>M: Check monitored availability
M-->>S: Mark online if the phone was offline
References: android/…/MainActivity.kt• L128-145 android/…/HeartbeatWorker.kt• L11-43 api/…/heartbeat_handler.go• L109-150 api/…/heartbeat_service.go• L95-146
Delayed heartbeat checks
Each monitor schedules its next check from the timestamp of the last heartbeat. The normal check interval is 16 minutes. A late heartbeat first produces a missed-heartbeat event, which asks the phone to report again through a high-priority FCM message. api/…/heartbeat_service.go• L20-23, api/…/heartbeat_service.go• L317-329
The check is a delayed event rather than a constant poll of every phone. A monitor stores the queue identity for its current check. If that check is no longer active, a new heartbeat can restart the check, and the service can then mark the phone online if the monitor had been offline. api/…/heartbeat_service.go• L127-144, api/…/heartbeat_service.go• L382-408, api/…/heartbeat_service_test.go• L66-111
References: api/…/heartbeat_service.go• L274-329 api/…/heartbeat_service.go• L382-408 api/…/phone_heartbeat_check_event.go• L10-20 api/…/heartbeat_listener.go• L85-107 api/…/heartbeat_service_test.go• L66-111
FCM heartbeat prompt
When a heartbeat is late but the monitor has not yet failed, the service emits a missed-heartbeat event. The phone notification path turns that event into a high-priority FCM data message. The Android Firebase receiver recognizes the heartbeat key and sends a fresh heartbeat instead of treating the message as SMS work. api/…/phone_notification_listener.go• L103-117, api/…/phone_notification_service.go• L80-115, android/…/FirebaseMessagingService.kt• L21-31
The prompt is a recovery action, not proof that the phone is online. The service learns that the phone has recovered only after the phone sends the heartbeat back to the API. android/…/FirebaseMessagingService.kt• L60-81, api/…/heartbeat_service.go• L143-170
References: api/…/phone_heartbeat_missed.go• L10-20 api/…/phone_notification_service.go• L80-115 api/…/phone_notification_listener.go• L103-117 android/…/FirebaseMessagingService.kt• L21-31
Online and offline transitions
Online transition
An online transition occurs when a new heartbeat arrives for a monitor that is currently offline. The service sets the monitor online and emits phone.heartbeat.online, which allows other event consumers such as webhooks to learn about the recovery. The new heartbeat remains the evidence for the transition. api/…/heartbeat_service.go• L127-170, api/…/phone_heartbeat_online_event.go• L10-21
The transition does not create a new phone record or alter message state. It updates availability for the existing monitor and publishes the recovery signal. api/…/heartbeat_listener.go• L111-124, api/…/webhook_listener.go• L148-162
References: api/…/heartbeat_service.go• L127-184 api/…/mongo_heartbeat_monitor_repository.go• L137-158 api/…/phone_heartbeat_online_event.go• L10-21 api/…/webhook_listener.go• L148-162 api/…/heartbeat_service_test.go• L90-99
Offline transition
The monitor declares a phone offline when a check finds that the latest heartbeat is more than four check intervals old but less than five intervals old while the monitor still says online. The service schedules another check and emits phone.heartbeat.offline. A heartbeat listener then marks the monitor offline, while user and webhook listeners can notify external systems. api/…/heartbeat_service.go• L317-379, api/…/heartbeat_listener.go• L110-124
The thresholds create two stages: a missed heartbeat can prompt a phone, while a longer silence can cause the offline transition. The code uses a 16-minute check interval and a four-to-five-interval failure window. The dashboard presents the expected gap as about one hour, so operators should treat the status as delayed liveness evidence rather than an exact connection timestamp. api/…/heartbeat_service.go• L20-23, web/…/[id].vue• L174-190, api/…/heartbeat_service_test.go• L107-111
References: api/…/heartbeat_service.go• L302-329 api/…/heartbeat_service.go• L353-379 api/…/phone_heartbeat_offline_event.go• L10-21 api/…/user_listener.go• L46-68 api/…/heartbeat_service_test.go• L107-111
Message work and availability
Phone work dispatch
Outgoing message work reaches a phone through a phone notification. The service first schedules a pending notification, then emits a delayed send event. At send time it sends an FCM message that contains the message ID; the Android agent uses that ID to fetch the outstanding message and then reports phone-side results. api/…/phone_notification_service.go• L185-243, api/…/phone_notification_service.go• L281-300
The phone claims work by requesting the message from the API. The request is limited to the phone numbers authorized by the phone API key, and the server changes an eligible pending, scheduled, or expired message to sending before it returns it. This claim step prevents a message from being treated as available twice. android/…/FirebaseMessagingService.kt• L83-102, api/…/message_handler.go• L174-218, api/…/gorm_message_repository.go• L244-278
References: api/…/phone_notification_service.go• L185-243 api/…/phone_notification_listener.go• L120-143 api/…/message_service.go• L73-106 api/…/gorm_message_repository.go• L244-278
Offline delivery behavior
The phone_online monitor flag does not directly gate message scheduling or FCM sends. HTTPSMS keeps message work pending or scheduled, and the phone can claim it when it next receives a push and has network access. This is why availability affects when work completes rather than selecting a different phone or stopping all routing. api/…/phone_notification_service.go• L128-168, api/…/heartbeat_monitor.go• L9-29
Delivery still has limits. FCM uses the phone's message expiration setting, and a failed or unclaimed message can move through expiration and retry handling until its configured send-attempt limit is reached. The SMS message lifecycle page covers those terminal states in more detail. api/…/phone_notification_service.go• L128-168, api/…/message_service.go• L874-954, SMS message lifecycle
References: api/…/phone_notification_service.go• L128-168 api/…/phone.go• L19-53 api/…/message_service.go• L874-954 android/…/FirebaseMessagingService.kt• L134-180
Operator view and alerts
Dashboard heartbeat view
The dashboard shows the newest heartbeat for the selected phone and links it to a heartbeat history view. The history view compares adjacent heartbeat timestamps so a long gap is visible, while the phone header uses the newest timestamp and charging state as a compact status signal. web/…/phones.ts• L80-112, web/…/MessageThreadHeader.vue• L112-142
The heartbeat page displays the latest 100 records and marks gaps longer than one hour. This view describes observed contact; it is separate from the service monitor that drives online and offline events. web/…/[id].vue• L137-153, web/…/[id].vue• L199-233
References: web/…/phones.ts• L5-40 web/…/phones.ts• L80-112 web/…/MessageThreadHeader.vue• L112-142 web/…/[id].vue• L137-153
Availability notifications
An offline event can produce an email alert when the account has heartbeat notifications enabled. The same online and offline events can be sent to configured webhooks, so availability can feed both human response and external monitoring. api/…/user_service.go• L329-364, api/…/webhook_listener.go• L131-162
The alert is about missing heartbeats, not a confirmed physical failure. Network loss, stopped background work, disabled SIM activity, missing app credentials, and a stale push token can all prevent a fresh report. The Android agent page covers device setup and background behavior. android/…/HeartbeatWorker.kt• L14-40, api/…/phone_notification_service.go• L80-115, Android phone agent
References: api/…/user_service.go• L329-364 api/…/webhook_listener.go• L131-162 web/…/index.vue• L1255-1267 android/…/HeartbeatWorker.kt• L14-40
Related pages
- Phone connectivity — parent overview of registered phones and their role as SMS endpoints.
- Android phone agent — phone-side authentication, background work, SMS handling, and API calls.
- Phone registration and API keys — phone identity, API-key access, and registration boundaries.
- Push delivery and read receipts — FCM message delivery, phone acknowledgements, and message state updates.
- SMS message lifecycle — message scheduling, retries, expiration, and terminal outcomes.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the Android heartbeat cadence, the server check interval, the missed-heartbeat window, and the dashboard's gap labels aligned. These values describe the same liveness signal at different stages.
- Preserve the
KEY_HEARTBEAT_IDFCM data key across the notification service and Android receiver. A key mismatch turns a liveness prompt into an ignored push. - Keep heartbeat owner and user identity aligned with phone API-key authorization. A report for the wrong number must not change another monitor's state.
- Keep the heartbeat event payload fields stable for the heartbeat listener, user email path, and webhook path.
- When monitor queue expiry or restart behavior changes, check that a new heartbeat can restore the delayed check and online state where expected.
- When message notification timing or expiration changes, check the offline case. A phone that cannot claim work must still reach the intended pending, retry, or expired state.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Android has no active SIM or the user is not logged in | The scheduled worker sends no heartbeat, so the monitor can become stale | Check SIM active state and login state in the phone agent before diagnosing a server fault |
| Heartbeat POST fails because the phone has no network or invalid API access | No new record is stored and no online recovery can occur | Check the phone's network, API key, and client version path |
| A reported phone number is not authorized by the phone API key | The heartbeat request is rejected before the report can update that phone's liveness | Check the API key's phone scope and the number sent by the agent |
| A missed-heartbeat FCM has no token or FCM rejects it | The phone is not prompted to send a recovery heartbeat | Check the registered FCM token and app installation state |
| The latest heartbeat is older than the offline threshold | The monitor becomes offline and offline events can send email or webhook alerts | Confirm the phone can run background work and can reach the API |
| A delayed monitor check expires while heartbeat history is absent | The monitor has no active check until a later heartbeat or monitor update restarts it | Keep check restart behavior aligned with heartbeat storage and monitor state |
| A push arrives but the phone cannot claim the message | The notification may fail or expire, and message retry rules decide the next state | Inspect the outstanding-message claim and phone-side failure report |
| A heartbeat event payload or type changes without updating listeners | Monitor state, alerts, or webhooks can stop changing while heartbeat records still appear | Treat heartbeat event names and payload fields as shared contracts |
Key files
| File | Why to read it |
|---|---|
| android/…/HeartbeatWorker.kt | Shows the normal phone-side heartbeat collection path. |
| android/…/FirebaseMessagingService.kt | Shows how a heartbeat prompt differs from an SMS push and how the phone claims message work. |
| api/…/heartbeat_handler.go | Defines the authenticated heartbeat read and write boundary. |
| api/…/heartbeat_service.go | Defines monitor creation, delayed checks, missed-heartbeat prompts, and online/offline transitions. |
| api/…/heartbeat_service_test.go | Shows the expected restart, recovery, and offline transition behavior for monitor checks. |
| api/…/heartbeat_listener.go | Connects heartbeat events to monitor state and scheduled checks. |
| api/…/phone_notification_service.go | Connects message work and missed-heartbeat recovery to FCM. |
| api/…/gorm_message_repository.go | Shows the server-side claim of outstanding work by a phone. |
| web/…/[id].vue | Shows how operators read heartbeat history and long gaps. |