2

I developed chat application using Twilio Programmable Chat SDK on Android.

Issue :

There are two users A & B joined to a Channel "AB_Private" and doing chat working fine.

Now user A press home button and put application in background. Twilio SDK detect inactivity and start sending ping to server to keep connection a live.

Working fine for 4-5 minutes but after that twilio disconnect from the socket and start retrieving request. But it never connects till user app is in background.

Now when app comes to foreground, twilio reset the session and try to reconnect but it takes more time to connect, sometime it takes up to 5 minutes.

So my question is how can I keep connection alive with Twilio when application is in background?

Any Help will be appreciated.

Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77

1 Answers1

3

Twilio developer evangelist here.

We can't keep the connection when the app is backgrounded, so that's not possible.

The slow reconnection time is probably due to a back off of reconnect attempts which we can try to fix in the SDK.

In the meantime I recommend that you detect when the app is going into background mode and disconnect the chat client. Then, when your user receives another message they will get a push notification at which point you can reconnect the client. Or when the app returns to the foreground, reconnect then.

Let me know if that helps.

Update

This was investigated and deemed a bug within the Twilio SDKs. A fix has just been released to sort this out. Please update to the latest version of the SDK. You can see the changelogs here:

Android:

iOS:

philnash
  • 70,667
  • 10
  • 60
  • 88
  • @ philnash thanks for the immediate response. I understand it is not advisable to keep connection live in background mode. But the problem is retrying time is incremental and when app come in foreground again it takes more time to reconnect. And there is no direct method that we can simply call reconnect. Currently we need to load all the channels again and re join them and set delegates, which takes more time. We need to show typing indicator on recent chat screen where all the channels are listed like whatsapp. Please guide me what approach will more reliable. – Biraj Zalavadia Sep 04 '17 at 05:27
  • You might want to locally store the channels and recent messages so that when the user returns to the app they are able to see and interact with their channels while you reconnect. Since you mention Whatsapp, I notice that when I wake up the Whatsapp application it shows that it is reconnecting while still showing recent conversations. I'd follow that pattern. – philnash Sep 04 '17 at 11:06
  • @ philnash thanks for the suggestion. Actually we are exactly doing the same, everything is managed in local sqlite database and it is working fine. But the problem with recreating Chatclient every time app comes from background is, we need to re fetch all the channels , re-join and set delegates. Because the exists channel objects will not work with the new client. There is not straight methods to perform simply connect and disconnect. So is there any way to have single delegate method that listen all the private channels that user is participant without fetching them and join every time ?? – Biraj Zalavadia Sep 05 '17 at 04:46
  • I note that you've been in contact with support and this has been escalated with the chat team, so it's best left in their hands right now. If anyone can sort this out for you, the chat team can. I'll try to update this question when there's a resolution too. – philnash Sep 05 '17 at 09:59
  • Do we still need to disconnect and reconnect? Or is that pod update all that’s needed? – Elijah Oct 19 '17 at 03:11
  • You shouldn't need to disconnect and reconnect, the SDK should handle that now. The problem was that the reconnection that was built in was using an exponential back off and waited too long to try to reconnect. – philnash Oct 21 '17 at 02:33
  • @philnash this is again encountering in android sdk 2.4.8 – Saurabh Bhandari Jan 27 '20 at 14:00
  • @SaurabhBhandari If this is happening in your application then I recommend you report this via the [Twilio support](https://www.twilio.com/help/contact) who can raise it to the relevant team internally. – philnash Jan 27 '20 at 23:14