Commit ed6ec77
Add Nightscout WebSocket support for real-time data updates (#606)
* Add Nightscout WebSocket (Socket.IO) support for real-time data updates
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 opt-in WebSocket toggle with info sheet and refresh on disconnect
- 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
* Fix stale WebSocket session on config change, remove redundant staleness 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
* Limit WebSocket to foreground and default-on
Disconnect the Nightscout WebSocket when LoopFollow moves to the
background and re-establish it when returning to the foreground.
Polling continues to handle background updates, so the persistent
connection no longer holds the cellular radio out of idle while the
app is not in use. With the battery cost bounded to foreground time,
the toggle now defaults to on. Inline documentation updated to
describe the foreground/background behavior.
* Resume Nightscout polling promptly when the WebSocket disconnects
While the socket is authenticated, each REST poll is rescheduled with a
multiplier on the assumption WS will publish the next reading before the
poll fires. Without an explicit catch-up, those long delays carry over
when the socket goes away — most visibly on background entry, where the
user could sit on stale data for 10–15 minutes after the screen locked.
Fire each Nightscout poll immediately on disconnect. Their actions then
reschedule on the normal un-multiplied cadence, since connectionState is
no longer .authenticated. Gate on the previous state so the reconnect
dance in connectIfNeeded() and other no-op disconnect paths don't trigger
spurious REST round-trips.1 parent de98f2e commit ed6ec77
12 files changed
Lines changed: 423 additions & 26 deletions
File tree
- LoopFollow.xcodeproj
- LoopFollow
- Controllers/Nightscout
- Log
- Nightscout
- Storage
- Task
- ViewControllers
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
440 | 440 | | |
441 | 441 | | |
442 | 442 | | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
443 | 446 | | |
444 | 447 | | |
445 | 448 | | |
| |||
604 | 607 | | |
605 | 608 | | |
606 | 609 | | |
| 610 | + | |
| 611 | + | |
607 | 612 | | |
608 | 613 | | |
609 | 614 | | |
| |||
941 | 946 | | |
942 | 947 | | |
943 | 948 | | |
| 949 | + | |
944 | 950 | | |
945 | 951 | | |
946 | 952 | | |
| |||
1209 | 1215 | | |
1210 | 1216 | | |
1211 | 1217 | | |
| 1218 | + | |
| 1219 | + | |
1212 | 1220 | | |
1213 | 1221 | | |
1214 | 1222 | | |
| |||
1844 | 1852 | | |
1845 | 1853 | | |
1846 | 1854 | | |
| 1855 | + | |
1847 | 1856 | | |
1848 | 1857 | | |
1849 | 1858 | | |
| |||
1881 | 1890 | | |
1882 | 1891 | | |
1883 | 1892 | | |
| 1893 | + | |
1884 | 1894 | | |
1885 | 1895 | | |
1886 | 1896 | | |
| |||
2255 | 2265 | | |
2256 | 2266 | | |
2257 | 2267 | | |
| 2268 | + | |
| 2269 | + | |
2258 | 2270 | | |
2259 | 2271 | | |
2260 | 2272 | | |
| |||
2870 | 2882 | | |
2871 | 2883 | | |
2872 | 2884 | | |
| 2885 | + | |
| 2886 | + | |
| 2887 | + | |
| 2888 | + | |
| 2889 | + | |
| 2890 | + | |
| 2891 | + | |
| 2892 | + | |
| 2893 | + | |
| 2894 | + | |
| 2895 | + | |
| 2896 | + | |
| 2897 | + | |
| 2898 | + | |
| 2899 | + | |
| 2900 | + | |
| 2901 | + | |
| 2902 | + | |
| 2903 | + | |
2873 | 2904 | | |
2874 | 2905 | | |
2875 | 2906 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
186 | 186 | | |
187 | 187 | | |
188 | 188 | | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
189 | 193 | | |
190 | 194 | | |
191 | 195 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
193 | 193 | | |
194 | 194 | | |
195 | 195 | | |
| 196 | + | |
196 | 197 | | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
| 198 | + | |
202 | 199 | | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
| 200 | + | |
208 | 201 | | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
| 202 | + | |
214 | 203 | | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
| 204 | + | |
219 | 205 | | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
| 206 | + | |
225 | 207 | | |
226 | 208 | | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
227 | 218 | | |
228 | 219 | | |
229 | 220 | | |
| |||
Lines changed: 54 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
0 commit comments