socket.io-client-dart icon indicating copy to clipboard operation
socket.io-client-dart copied to clipboard

Why our messages are repeating when we navigate from push notifications?

Open amani27 opened this issue 4 years ago • 3 comments

We've used socket_io_client: ^2.0.0-beta.4-nullsafety.0 to capture real time events, and FCM for push notifications. Real time events seem fine when we're using app normally, but when we enter the app on clicking on push notifications, socket events are getting listened multiple times(thrice mostly), causing our data to duplicate socket.on('noti:${getIntAsync(USER_ID)}', (data) { print("incomingNotification() : $data"); } incomingNotification() gets printed in console multiple times.

Do we need to destroy connection when we close the app? If destroyed then opened can we use socket connection again?

When I do disconnect Socket and reconnect it gets initialised but won't connect unless I do a full restart of the app.

/// Initialising socket
socket = io(
        API.SOCKET_BASE,
        OptionBuilder()
            .setTransports(['websocket'])
            .enableAutoConnect()
            .enableReconnection()
            .setReconnectionDelay(500)
            .setReconnectionAttempts(10)
            .build());
/// On successful socket connection
  socket.onConnect((_) {
      print('SOCKET: CONNECTED');

      if (getBoolAsync(LOGGED_IN)) {
        incomingNotification();
      }
    });
    socket.onDisconnect((data) {
      print('SOCKET: DISCONNECTED');
      print(data);
      if (getBoolAsync(LOGGED_IN)) {
        tryReconnect(); // if at any point socket disconnects, and user is logged in try reconnecting
      }
    });
tryReconnect(){
     if (socket.connected != null) {
      if (socket.connected == false) {
        print('SOCKET: MANUAL CONNECT');
        socket.connect();
      }}
}

amani27 avatar Jan 19 '22 04:01 amani27

Can you attach the code that handles the connection, disconnection and the part that attaches the event listeners. It might be that when you relaunch the app, new event listeners might be getting attached. This follows by new and old event listeners being triggered, hence printing the log statement twice or thrice.

Advait1306 avatar Jan 23 '22 18:01 Advait1306

Can you attach the code that handles the connection, disconnection and the part that attaches the event listeners. It might be that when you relaunch the app, new event listeners might be getting attached. This follows by new and old event listeners being triggered, hence printing the log statement twice or thrice.

@Advait1306 I edited my post with the code.

amani27 avatar Jan 24 '22 04:01 amani27

Is this issue still occurring if you close the app completely from the recents menu and then click on a notification? Also, the code in the post doesn't have the part that attaches event listeners.

Advait1306 avatar Jan 24 '22 05:01 Advait1306