braverhealth / phoenix-socket-dart

Cross-platform and stream-based implementation of Phoenix Sockets
https://pub.dev/packages/phoenix_socket
BSD 3-Clause "New" or "Revised" License
73 stars 37 forks source link

If my phoenix server is stopped, the client keeps on reconnecting every millisecond. #34

Closed akashvibhute closed 4 months ago

akashvibhute commented 3 years ago

@matehat I still get this issue. If my phoenix server is stopped, the client keeps on reconnecting every millisecond if I don't pass any value to reconnectDelays. If I pass a list of Durations, only the first duration is taken and it keeps on reconnecting with the same interval instead of increasing as per the list. On flutter 2.2 and latest version of the package. I thought this was an issue with riverpod providers, but I can reproduce it with initializing the socket connection directly in the main method itself or as a global variable imported to join a channel.

Originally posted by @akashvibhute in https://github.com/braverhealth/phoenix-socket-dart/issues/6#issuecomment-845200357

Configuration -

final socketProvider = Provider<PhoenixSocket>(
  (ref) => PhoenixSocket(
    "wss://devserver/socket/websocket",
    socketOptions: PhoenixSocketOptions(
        timeout: Duration(seconds: 10),
        reconnectDelays: const [
          Duration(milliseconds: 1000),
          Duration(milliseconds: 2000),
          Duration(milliseconds: 4000),
        ],
        dynamicParams: () async {
          final authState = ref.read(authStateProvider.notifier);
          return {"token": authState.getAccessToken()};
        }), // this isn't relevent for this issue.
  )..connect(),
);

I'm just calling useProvider(socketProvider); in the main method. Same issue without riverpod as well.

I set the minium retry time to 1 sec, as by default it was attempting every millisecond and producing too many logs.

Reproduction logs

Restarted application in 825ms.
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:18:56.939882: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:18:56.956051: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:18:56.958758: Message encoded to [null, 0, phoenix, heartbeat, {}]
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:18:57.538519: Message decoded from [null, 0, phoenix, phx_reply, {response: {}, status: ok}]
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:18:57.539151: [phoenix_socket] Heartbeat completed
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:18:57.539814: Socket open
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:22.451241: Socket closed with reason  and code 1005
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:22.452441: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:23.452022: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:23.453258: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:23.453695: Message encoded to [null, 1, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:23.484705: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:23.485027: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:23.485542: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:23.485790: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:23.488674: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:23.488998: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:24.486638: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:24.487543: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:24.488076: Message encoded to [null, 2, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:24.508541: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:24.509775: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:24.511036: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:24.511312: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:24.511960: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:24.512206: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:25.512518: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:25.513490: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:25.513851: Message encoded to [null, 3, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:25.534022: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:25.535298: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:25.537307: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:25.537723: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:25.538317: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:25.538643: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:26.538532: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:26.539567: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:26.539914: Message encoded to [null, 4, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:26.558737: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:26.559032: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:26.559376: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:26.559549: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:26.559921: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:26.560095: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:27.560536: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:27.561375: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:27.561664: Message encoded to [null, 5, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:27.580908: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:27.581271: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:27.581745: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:27.582385: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:27.582994: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:27.583266: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:28.583647: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:28.584679: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:28.585136: Message encoded to [null, 6, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:28.603407: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:28.603667: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:28.603978: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:28.604134: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:28.604520: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:28.604731: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:29.605537: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:29.606410: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:29.606772: Message encoded to [null, 7, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:29.624923: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:29.625161: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:29.625418: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:29.625569: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:29.625979: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:29.626197: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:30.626691: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:30.627550: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:30.627962: Message encoded to [null, 8, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:30.647598: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:30.647932: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:30.648270: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:30.648437: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:30.648833: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:30.649026: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:31.649583: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:31.650399: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:31.650670: Message encoded to [null, 9, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:31.669689: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:31.669871: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:31.670079: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:31.670186: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:31.670440: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:31.670744: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:32.671484: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:32.672420: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:32.672822: Message encoded to [null, 10, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:32.691527: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:32.691707: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:32.691981: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:32.692115: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:32.692441: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:32.692630: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:33.693650: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:33.694682: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:33.694998: Message encoded to [null, 11, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:33.712079: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:33.712313: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:33.712564: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:33.712737: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:33.713051: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:33.713193: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:34.713489: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:34.714253: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:34.714522: Message encoded to [null, 12, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:34.732819: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:34.733069: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:34.733431: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:34.733686: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:34.734180: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:34.734409: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:35.734616: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:35.735640: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:35.736036: Message encoded to [null, 13, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:35.754110: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:35.754467: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:35.754887: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:35.755112: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:35.755684: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:35.755991: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:36.756617: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:36.757588: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:36.757954: Message encoded to [null, 14, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:36.776345: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:36.776591: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:36.776947: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:36.777197: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:36.777772: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:36.778015: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:37.778601: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:37.779607: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:37.779993: Message encoded to [null, 15, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:37.799642: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:37.800877: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:37.803034: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:37.804397: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:37.805396: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:37.805660: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:38.803484: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:38.804587: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:38.804944: Message encoded to [null, 16, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:38.825095: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:38.825411: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:38.825712: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:38.825854: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:38.826211: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:38.826431: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:39.827524: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:39.828399: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:39.828723: Message encoded to [null, 17, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:39.848001: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:39.848355: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:39.848851: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:39.849115: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:39.849670: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:39.851111: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:40.850623: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:40.851635: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:40.852103: Message encoded to [null, 18, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:40.870060: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:40.870342: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:40.870728: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:40.870948: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:40.871449: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:40.871673: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:41.871539: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:41.872508: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:41.872873: Message encoded to [null, 19, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:41.891164: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:41.891485: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:41.891863: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:41.892013: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:41.892366: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:41.892595: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:42.893542: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:42.894498: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:42.894871: Message encoded to [null, 20, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:42.913078: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:42.913463: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:42.913887: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:42.914111: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:42.914650: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:42.914891: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:43.915655: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:43.916601: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:43.916914: Message encoded to [null, 21, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:43.934943: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:43.935203: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:43.935578: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:43.935871: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:43.936380: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:43.936622: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:44.936456: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:44.937418: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:44.937785: Message encoded to [null, 22, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:44.955016: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:44.955362: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:44.955889: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:44.956146: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:44.956627: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:44.956873: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:45.957772: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:45.958865: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:45.959289: Message encoded to [null, 23, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:45.978039: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:45.978323: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:45.978583: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:45.978727: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:45.979044: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:45.979187: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:46.979477: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:46.980243: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:46.980509: Message encoded to [null, 24, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:47.000084: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:47.000358: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:47.000631: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:47.000864: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:47.001156: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:47.001290: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:48.001606: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:48.002852: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:48.003258: Message encoded to [null, 25, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:48.021347: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:48.021591: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:48.021942: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:48.022151: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:48.022720: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:48.022969: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:49.023539: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:49.024509: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:49.024895: Message encoded to [null, 26, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:49.044635: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:49.044975: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:49.045277: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:49.045422: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:49.045734: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:49.045875: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:50.046567: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:50.047503: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:50.047910: Message encoded to [null, 27, phoenix, heartbeat, {}]
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:50.077475: Error on socket
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:50.077739: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:50.078135: Socket closed with reason WebSocket could not establish a connection and code null
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:50.078377: Trigger channel exceptions on 0 channels
flutter: [phoenix_socket.socket] SEVERE 2021-05-21 09:19:50.078870: [phoenix_socket] Heartbeat message failed with error
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:50.079095: Trigger channel exceptions on 0 channels
flutter: PhoenixException: socket closed
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:51.079577: Attempting to connect to wss://devserver/socket/websocket?token&vsn=2.0.0
flutter: [phoenix_socket.socket] FINEST 2021-05-21 09:19:51.080562: Waiting for initial heartbeat roundtrip
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:51.080967: Message encoded to [null, 28, phoenix, heartbeat, {}]
flutter: [phoenix_socket.message] FINEST 2021-05-21 09:19:51.250710: Message decoded from [null, 28, phoenix, phx_reply, {response: {}, status: ok}]
flutter: [phoenix_socket.socket] FINE 2021-05-21 09:19:51.250811: [phoenix_socket] Heartbeat completed
flutter: [phoenix_socket.socket] INFO 2021-05-21 09:19:51.250861: Socket open

Initially phoenix server was running and connection was fine on app startup. Observed issue when server was stopped and reconnect attempts did not respect the specified durations. Connection established at the end again when phoenix server was restarted.

Please let me know if you need anything else.

akashvibhute commented 3 years ago

Also the phoenix server is behind nginx reverse proxy if that helps, so nginx returns 502 response if phoenix is down,

ol88 commented 3 years ago

I experienced the same problem 2 days ago. Not sure if something changed or just coincidence as I never worked on reconnection before then...

The root cause is here in socket.dart:

When the websocket is closed either by the server going offline or no internet connection (airplane mode will trigger it easily), _onSocketClosed() is called from the _onSocketError callback.

  void _onSocketError(dynamic error, dynamic stacktrace) {
     ...
    _onSocketClosed();
  }

Currently, _onSocketClosed does not specify any reconnection delay:

  void _onSocketClosed() {
    if (_shouldReconnect) {
      _delayedReconnect();
    }
   ...
  }

And _delayedReconnect only takes the _options.reconnectDelays.first when a delay is not explicitly specified. The default value for _options.reconnectDelays.first is 0 so with default options, we have an infinite loop, when a reconnectDelays list is specified in PhoenixSocketOptions, only the first value is taken into account.

  Future<PhoenixSocket?> _delayedReconnect([Duration? delay]) async {
    if (_reconnecting) return null;

    _reconnecting = true;
    await Future.delayed(delay ?? _options.reconnectDelays.first);

    if (!_disposed) {
      _reconnecting = false;
      return connect();
    }

    return null;
  }

I have found a temporary work around which is to listen to closeStream events on the socket, in my case, I just close the socket and start a reconnection logic somewhere else in my app which will create and connect with a brand new socket.

_socket = PhoenixSocket(_websocketURL, socketOptions: _socketOptions);

_socket!.closeStream.listen((event) {
       // Catches all events, probably not ideal.
      _customReconnectionLogicHere();

      // Or can be more specific for the various code which may be received.
      if (event.code == 1002) {
        _customReconnectionLogicHere();
      }
});
ol88 commented 3 years ago

Digging a bit further but I'm not familiar enough with the code to understand the intent, there may be a asynchronous error somewhere in the reconnection logic.

As we see above, a socket error triggers a chain of events leading ultimately to a call to connect() in _delayedReconnect().

connect() does implement its own correct delayed reconnection logic IF no error was caught during the websocket creation and connection.

When no internet is available, onError callback is triggered which calls _onSocketError in the catch clause and restarts the loop by calling _onSocketClosed() which has no reconnection delay.

Future<PhoenixSocket?> connect() async {
    ...

    try {
      _ws = WebSocketChannel.connect(_mountPoint);
      _ws!.stream
          .where(_shouldPipeMessage)
          .listen(_onSocketData, cancelOnError: true)
            ..onError(_onSocketError)
            ..onDone(_onSocketClosed);
    } catch (error, stacktrace) {
      _onSocketError(error, stacktrace);
    }

Since _delayedReconnect() (a future) is not awaited by _onSocketClosed(), _onSocketClosed() and _onSocketClosed() do complete and some of the below code is executed (as seen in the logs) and quickly fails since the websocket _ws is already set back to null in _onSocketClosed().

    _socketState = SocketState.connecting;

    try {
      _socketState = SocketState.connected;
      _logger.finest('Waiting for initial heartbeat roundtrip');
      if (await _sendHeartbeat(ignorePreviousHeartbeat: true)) {
        _stateStreamController.add(PhoenixSocketOpenEvent());
        _logger.info('Socket open');
        completer.complete(this);
      } else {
        throw PhoenixException();
      }

The below reconnection logic is never reached

    } on PhoenixException catch (err) { 
      print(err);
      final durationIdx = _reconnectAttempts++;
      _ws = null;
      _socketState = SocketState.closed;

      Duration duration;
      if (durationIdx >= _reconnects.length) {
        duration = _reconnects.last;
      } else {
        duration = _reconnects[durationIdx];
      }

      // Some random number to prevent many clients from retrying to
      // connect at exactly the same time.
      duration += Duration(milliseconds: _random.nextInt(1000));

      completer.complete(_delayedReconnect(duration));
    }

    return completer.future;
  }

I'm not sure if the intent was for the reconnection logic inside connect() to handle all cases but the call to _onSocketError() short-circuits that intent or if the delayed reconnection logic should simply be duplicated somewhere inside _onSocketClosed(). I guess the latter I think @matehat will know better.

redDwarf03 commented 2 years ago

hello @matehat, i don't know if it's related to this open issue, but i can't leave the connection Capture d’écran 2022-08-09 à 15 10 56

matehat commented 1 year ago

Fixed by #50

matehat commented 1 year ago

Released in 0.6.1

matehat commented 1 year ago

hello @matehat, i don't know if it's related to this open issue, but i can't leave the connection Capture d’écran 2022-08-09 à 15 10 56

@redDwarf03 I don't think it is. Can you retry with the latest version? It also includes a fix for the heartbeat.

redDwarf03 commented 1 year ago

hello @matehat, i don't know if it's related to this open issue, but i can't leave the connection Capture d’écran 2022-08-09 à 15 10 56

@redDwarf03 I don't think it is. Can you retry with the latest version? It also includes a fix for the heartbeat.

Hello. Cool, i'm happy you're working on your lib !! Sadly, i just test with then new version and i have always the heartbeat

ks0m1c commented 1 year ago

There was a recent patch in 1.7.0-dev that fixed heartbeat being sent after disconnect and causing abnormal disconnects

redDwarf03 commented 1 year ago

Thank you for the information. Any impacts for the dart lib?

matehat commented 4 months ago

@redDwarf03 can you retry with the latest release?

And if it's still the case, can you reopen a new ticket, as the original issue was fixed.