I am developing a game with offline capability (e.g. for mobile
devices). Communication is via Socket.IO, with [localStorage][1] as
offline cache for hiscores. Details (client = browser):
* On client load:
- hiscores are retrieved from localStorage,
- updated hiscores are requested from the server,
- unsaved hiscores are sent to server (for saving in database).
* On new hiscores on server: new saved hiscores are broadcasted to all
clients
* On new hiscore entry on client: unsaved hiscores are sent to server
This mostly works fine, for example in the following scenario:
1. Load game.
2. Stop server, for testing.
3. Play game, and submit new hiscores entry.
4. Some time later: start server
In step 4, the hiscores entry gets automatically sent to the server *if*
the server hasn't been offline for too long. At the moment I am using
default Socket.IO settings: Socket.IO tries reconnecting ten times over
a short period of time.
Now, how do I configure Socket.IO so that step 4 always works? Should I
just tell it to try reconnecting forever? What is the recommended way to
handle situations like this?
2. Check [`navigator.offline`][1] and `navigator.online`, and listen to
the corresponding `online` and `offline` events. Take action on
browser state changes:
+ Turned offline: Call Socket.IO's `disconnect`.
+ Turned online:
- Not connected to Socket.IO (`socket.connected` is false):
Call Socket.IO's `connect`, then (re-)send hiscores to server.
- Still connected: Do nothing, just wait (but see below for
possible issues).
Does that look like a robust solution?
Does setting the reconnection attempts to "infinity" really make sense?
The problem I see is that according to documentation an "exponential
back off algorithm" is used, making delays between retries longer and
longer. So, when the user goes back online it may take, say, an hour
until Socket.IO attempts to reconnect. Is it possible to turn off the
"exponential back off algorithm"?
No, setting the reconnect to infinity does not make sense as you cannot turn of exponential back off
(it's there for a reason) But then again, you can just disable reconnect and handle it your self. The only
thing that reconnect is doing is handling disconnect events.
On Monday, October 22, 2012 at 2:02 AM, Felix E. Klee wrote:
> My own solution proposal:
> 1. Set "max reconnection attempts" to "infinity".
> 2. Check [`navigator.offline`][1] and `navigator.online`, and listen to
> the corresponding `online` and `offline` events. Take action on
> browser state changes:
> - Not connected to Socket.IO (`socket.connected` is false):
> Call Socket.IO's `connect`, then (re-)send hiscores to server.
> - Still connected: Do nothing, just wait (but see below for
> possible issues).
> Does that look like a robust solution?
> Does setting the reconnection attempts to "infinity" really make sense?
> The problem I see is that according to documentation an "exponential
> back off algorithm" is used, making delays between retries longer and
> longer. So, when the user goes back online it may take, say, an hour
> until Socket.IO attempts to reconnect. Is it possible to turn off the
> "exponential back off algorithm"?
On Monday, October 22, 2012 12:03:17 PM UTC-4, 3rdEden wrote: > No, setting the reconnect to infinity does not make sense as you cannot > turn of exponential back off > (it's there for a reason)
I don't think this is true anymore, there's a "reconnection limit" option that caps the exponential backoff.
>> No, setting the reconnect to infinity does not make sense as you
>> cannot turn of exponential back off (it's there for a reason)
> I don't think this is true anymore, there's a "reconnection limit"
> option that caps the exponential backoff.
Thanks for pointing that out. Excerpt from [lib/socket.js][1]:
if (self.reconnectionDelay < limit) {
self.reconnectionDelay *= 2; // exponential back off
}
I just saw your message today, i.e. after posting my message "Offline
capable app: reconnection when online?". I may go back to automatic
reconnect then, with: