Sendbird Reconnect Implementation

Hi, I wanna ask a question about sendbird auto reconnect feature.

So currently sendbird is implementing the disconnect when we are minimizing the app (ApplicationOnPause) and reconnecting again when we are back to the app (ApplicationOnResume).

But on our cases, we want to implement the auto reconnection in Activity Lifecycle level. So it will disconnect the sendbird when we are moving to another activity and reconnecting again when we are back to our sendbirdActivity.

Is it possible to do such things? Because im trying to use
Sendbird.disconnect - at OnPauseActivity and
Sendbird.reconnect / Sendbird.connect - at OnResumeActivity it does not work. The fetch wont work properly (we must reinit channel collection / message collection to get current messages that are received on disconnected state)

Hi @Naufal_Azzaahid, thank you for posting to the community! We are currently investigating this issue and will update you shortly. :zap:

Hello, Naufal

Can you please check my repository here:

  1. Clone / Download code.

  2. Go to SendBirdClass.java and enter your APP ID there.

  3. Run the application.

  4. MainActivity will show a CONNECT button. Click on it.

  5. Once connected, the app will list all your GROUP CHANNELS.
    Check the onPause() and onResume() from MainActivity please, if this is what you need.

  6. When clicking on any of the GROUP CHANNELS from the list, you will be redirected to ChatActivity.
    This activity will list all the messages in that channel.
    Check again onPause() and onResume()

Check this video with this demo working:

@walter.rodriguez

Wow that was really fast. Thanks a lot walter.
Will have a check on it!

@walter.rodriguez

After I see the code I just notice that you did not use the syncManager (Channel Collection / Message Collection).
On our apps, we are using those, and the problem is after reconnect, the fetch method does not work properly (we must reinit channel collection / message collection to get current messages that are received on disconnected state)

I see… I just added SyncManager to that repo… But did a very basic implementation. No sure if that’s what you’re implementing.
If you have some code to share we can see what could be happening or what to fix here if there’s a problem. Thanks!

Hi @Naufal_Azzaahid, did the solution provided by @walter.rodriguez work? If it did not, we are afraid our current disconnect-connect is not intended to be used in this kind of scenario.

Instead, could you please provide us some more context regarding why you want to do this? Maybe we can provide some other, easier way to solve your problem. If this is the only way, we will try to make it work then. :+1:

@Cjeon @walter.rodriguez

Hi guys thanks for the support!

The solution above did not work, because I depends on MessageCollection and ChannelCollection for the fetch (using fetch method from those collection).

So the implementation is a little bit different.

For the implementation, we are following the SendbirdSyncManager sample that provided from the sendbird itself but in addition we want to have a connect/disconnect mechanism each time we leave a senbird related activity/fragment.

Instead, could you please provide us some more context regarding why you want to do this?

We want to do this to reduce the number of peak connection. Currently our peak connection is always exceeding the limit that sendbird provides.

So if we can implement our desired approach (disconnect and reconnect on activity/fragment level) it would reduce the number of connection whenever the user is not on a sendbird-related screen.

Anyway thanks for the follow up! It would be very nice if you guys can implement the auto reconnection but on activity/fragment level!

1 Like

Hi @Naufal_Azzaahid,
If I understood your case correctly, you only want to keep connection with your Sendbird-related activity/fragment, but having issue with fetch.

Could you try the code below and see if it works?
The key to get new messages/channels by fetch while you were disconnected is that it should be called with SendBirdSyncManager.getInstance().resumeSync().

Also if this doesn’t work, it’ll be helpful for us if you could provide the code snippet on how you’ve implemented your connect()/disconnect() and fetch().

    // Assuming SyncManager setup is done, 
    // add following codes in onResume() and onPause().

    @Override
    public void onResume() {
        super.onResume();

        // connect
        SendBird.connect(userId, new SendBird.ConnectHandler() {
            @Override
            public void onConnected(User user, SendBirdException e) {
                if (e == null) {
                    // resume sync after connection
                    SendBirdSyncManager.getInstance().resumeSync();

                    // if this is channel list activity/fragment (using ChannelCollection)
                    channelCollection.fetch(new CompletionHandler() {
                        @Override
                        public void onCompleted(SendBirdException e) {
                        }
                    });

                    // if this is chat activity/fragment (using MessageCollection)
                    messageCollection.fetchAllNextMessages(new FetchCompletionHandler() {
                        @Override
                        public void onCompleted(boolean hasMore, SendBirdException e) {
                        }
                    });
                }
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();

        // disconnect
        SendBird.disconnect(new SendBird.DisconnectHandler() {
            @Override
            public void onDisconnected() {
            }
        });
    }

Alo @hoons
Yes you are on point with the problem. And also thanks for providing the solution!

For the MessageCollection it worked!

But for the channelCollection still having the issue with fetch. And the implementation is more like

public void onResume() {
    super.onResume();
    SendBird.connect(userId, new SendBird.ConnectHandler() {
        @Override
        public void onConnected(User user, SendBirdException e) {
            if (e == null) {
                SendBirdSyncManager.getInstance().resumeSync();

                channelCollection.fetch(new CompletionHandler() {
                    @Override
                    public void onCompleted(SendBirdException e) {
                    }
                });
            }
        }
    });
}

@Override
public void onPause() {
    super.onPause();

    // disconnect
    SendBird.disconnect(new SendBird.DisconnectHandler() {
        @Override
        public void onDisconnected() {
        }
    });
}

can you confirm if the channel fetch is working on your side?
Sorry for the trouble!

Hi @Naufal_Azzaahid,
Good to hear that the message collection is working.

I’ve tested myself with the above codes and confirmed it working for both channel/message collection.

Seems like your code is exactly same as mine, can I please know what exactly is an issue with the channel collection?

What I’ve tested is like below.

  1. Channel collection’s sorting is set to LATEST_LAST_MESSAGE (channels sorted in their last message’s timestamp)
  2. I move my app to other fragment/activity (disconnect)
  3. receive message from the second channel in the channel list, say TARGET_CHANNEL
  4. come back to the Sendbird fragment/activity (connect)
  5. I received event from ChannelCollectionHandler.onChannelEvent() with ChannelEventAction.MOVE action for that channel (TARGET_CHANNEL)