Skip to content

Add Nightscout WebSocket support for real-time data updates#606

Open
bjorkert wants to merge 3 commits intodevfrom
feature/nightscout-websocket
Open

Add Nightscout WebSocket support for real-time data updates#606
bjorkert wants to merge 3 commits intodevfrom
feature/nightscout-websocket

Conversation

@bjorkert
Copy link
Copy Markdown
Contributor

Summary

  • Adds optional Socket.IO WebSocket connection to Nightscout for real-time push notifications of data changes
  • WebSocket delta events selectively trigger HTTP fetches only for changed data types (BG, treatments, device status, profile), reducing unnecessary network requests
  • Polling intervals are extended (3x for BG, 5x for treatments/device status, 3x for profile) while WebSocket is authenticated, with immediate fallback to normal polling if the connection drops
  • Opt-in toggle in Nightscout settings (default off) with info button explaining the feature and battery implications

Connect to Nightscout's Socket.IO endpoint to receive push notifications
when new data arrives, instead of waiting for the next poll cycle. The
WebSocket acts as a smart trigger: when a dataUpdate event arrives, only
the relevant data types (BG, treatments, device status, profile) are
fetched based on which keys are present in the delta payload.

When WebSocket is connected and authenticated, polling intervals are
extended (BG 3x, device status 3x, treatments 2→10 min, profile 10→30
min) so HTTP polling becomes a safety net. On disconnect, polling
immediately reverts to normal intervals.

The feature is always-on when Nightscout is configured — no user setting
needed. A read-only connection status is shown in Nightscout settings.

- Add Socket.IO-Client-Swift 16.1.1 via SPM
- Add NightscoutSocketManager for connection lifecycle
- Add NightscoutSocketDataHandler for selective push-trigger logic
- Extend polling intervals when WebSocket is authenticated
- Show WebSocket status in Nightscout settings
- Wire up lifecycle in MainViewController (init, foreground, refresh)
- Add staleness detection (10 min fallback to polling)
- Add webSocketEnabled storage property (default off) so users opt in
- Replace read-only status with toggle + info button in Nightscout settings
- Info sheet explains real-time updates, polling fallback, and battery impact
- On toggle off: disconnect socket and trigger full refresh to restore
  normal polling intervals immediately
- On unexpected socket disconnect: post refresh notification so extended
  polling intervals revert to normal without waiting for them to expire
@bjorkert
Copy link
Copy Markdown
Contributor Author

image image

…ess timer

- Disconnect WebSocket when Nightscout URL/token validation fails,
  preventing the old session from streaming data from a previous server
  while polling has switched to the new config
- Reorder removeAllHandlers() before disconnect() so intentional
  disconnects don't fire the event handler that would reconnect to
  an invalid URL
- Remove staleness timer — the extended polling intervals (3x/5x)
  already serve as the safety net when WebSocket data stops flowing
@bjorkert
Copy link
Copy Markdown
Contributor Author

Addressed both review findings:

Stale WebSocket session on config change (high): updateStatusLabel() now calls disconnect() when URL/token validation fails, so the old socket can't keep streaming from the previous server. Additionally, removeAllHandlers() is now called before disconnect() in the teardown path — this prevents intentional disconnects from firing the .disconnect event handler, which would post a refresh and potentially reconnect to the invalid URL.

Staleness timer (medium): Removed entirely. The extended polling intervals (3x for BG/device status, 5x for treatments, 3x for profile) already continue running as a safety net. If the socket authenticates but no data flows, polling still fetches via HTTP at the extended cadence. The staleness timer was redundant and had a bug where it never armed if no dataUpdate was received.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant