Customize the LV websocket reconnect strategy

In the LV documentation Deployments and recovery — Phoenix LiveView v1.2.5 it says that LV use a exponential back-off strategy for trying to reconnect the websocket.

is there any way to customize that? I want to change that strategy to be constant (example: try to reconnect every 2 seconds) instead of exponential.

Hey @sezaru,

the LiveSocket constructor passes non-LiveView options to the Phoenix Socket, so the option you want is reconnectAfterMs

https://github.com/phoenixframework/phoenix/blob/6bc3edc68af7ec92721508c4fac931c518f24e62/assets/js/phoenix/socket.js#L57

Thanks for the reply @steffend .

I just tested it but I’m not sure it is doing what I expected. Basically I added this code:

  const reconnectAfterMs = (tries) => {
    console.log(`RECONNECT ${tries}`)
    return 10000;
    // return [10, 50, 100, 150, 200, 250, 500][tries - 1] || 1000;
  };

  const opts = {
    ...
    reconnectAfterMs: reconnectAfterMs,
  };

  return new LiveSocket(path, Socket, opts);

What I expected that would happen is that I would see in the console RECONNECT 1, then an websocket connection failure, then 10 seconds would pass and I would get RECONNECT 1 2 and so on.

What I saw is RECONNECT 1 1, then a lot of attempts to connect to the websocket, then RECONNECT 1 2 way before the 10 seconds mark.

It doesn’t seem like this option is actually limiting the time it takes to retry a connection.

There are no timestamps in that screenshot, but you need to ignore all the live_reload logs, as that is a separate socket that is not affected by your configuration.

Ah, you are totally correct, I didn’t notice that the warnigs were for the live_reload… The LV websocket is working great! Nice!