Commit 2575a6b
authored
Stop reconnecting when page is hidden (#6534)
* Stop reconnecting when page is hidden
This tries to address the problem of seeing the "attempting to reconnect"
flash lingering around for a very long time when resuming a LiveView tab
on mobile. In my tests, this mostly happens when fallback to longpoll is
disabled. In this case, the following happens:
1. User puts their phone to sleep.
2. After a couple of seconds, the browser kills the websocket connection
and Phoenix registers this as an error.
3. Phoenix tries to establish a new WebSocket connection. This hangs.
4. User opens the page again. Instead of immediately establishing the
connection, the WebSocket is stalled for a while. The exact amount
seems a bit random, but I've seen 10+ seconds. During that time, the
WebSocket in the devtools doesn't show any activity, but then suddenly
the connection works again.
If LongPoll is active, at point 3, Phoenix instead tries a LongPoll
connection. In mobile Chrome, the HTTP request fails and Phoenix registers
this as 500 and tries again, backing off. If the user resumes the page -
depending on their luck - they might need to wait for the next backoff
attempt to fire, then the connection is established quickly.
This PR addresses this problem by checking the visibility state using
the `visibilitychange` event, which is recommended for registering a tab
going in the background (for example by a phone going to sleep). We set
a flag that we're hidden and in the case that there's an error, we don't
try to reconnect immediately (since we're hidden). Instead, we reconnect
as soon as the visibilitychange fires again with the page not hidden any
more.
With this change, the connection is immediately established in both mobile
Chrome (Android) and Mobile Safari (iOS), most of the time not seeing the
"trying to reconnect" flash at all.
Also, this PR addresses a problem where the `connectWithFallback` function
would accumulate `onOpen` listeners, causing duplicate callbacks.
Hopefully closes phoenixframework/phoenix_live_view#3896.
Hopefully closes #6149.
* make tests happy1 parent 15b4477 commit 2575a6b
2 files changed
+28
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
| 119 | + | |
119 | 120 | | |
120 | 121 | | |
121 | 122 | | |
| |||
129 | 130 | | |
130 | 131 | | |
131 | 132 | | |
| 133 | + | |
132 | 134 | | |
133 | 135 | | |
134 | 136 | | |
| |||
150 | 152 | | |
151 | 153 | | |
152 | 154 | | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
153 | 166 | | |
154 | 167 | | |
155 | 168 | | |
| |||
178 | 191 | | |
179 | 192 | | |
180 | 193 | | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
181 | 199 | | |
182 | 200 | | |
183 | 201 | | |
| |||
396 | 414 | | |
397 | 415 | | |
398 | 416 | | |
399 | | - | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
400 | 421 | | |
401 | 422 | | |
402 | 423 | | |
| |||
502 | 523 | | |
503 | 524 | | |
504 | 525 | | |
| 526 | + | |
505 | 527 | | |
506 | 528 | | |
507 | 529 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | | - | |
| 9 | + | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | | - | |
| 16 | + | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| |||
0 commit comments