Back

Auto Reconnect to Subscription

  • 2
  • Flutter
waheedoof
1 Jan, 2024, 14:22

When the subscription auto-closes after 100 second, the key question is: How can we effectively handle scenarios where the user interacts with the app, actively listening, and then leaves the app in the background for an extended period before returning to reuse it? rebuild new subscription every 1 min better or make auto update every 1min for Prevent close the subscription

TL;DR
The user is using Realtime for a chat AI app and wants to know if it's better to send a light request to refresh the subscription every 90 seconds or create a new subscription every 90 seconds. The suggestion is to keep the connection alive by updating a document every minute. It's also mentioned that having a separate endpoint that bypasses Cloudflare might be helpful. When the user leaves the app in the background, it's likely that all connections will be closed for battery saving, but it can be recreated when the user re-enters the app. There may be a limitation with Cloudflare closing idle WebSockets connections after
ideclon
1 Jan, 2024, 14:29

It shouldn't be auto closing after a minute? Is your server behind Cloudflare?

waheedoof
1 Jan, 2024, 14:31

Yes, it is!

ideclon
1 Jan, 2024, 14:32

Yeah, IIRC, CF closes idle WebSockets connections after one to two minutes.

ideclon
1 Jan, 2024, 14:33

I don't think there's any way to disable that

ideclon
1 Jan, 2024, 14:34

If you want to automatically reopen, you could listen for the socket close event, and create a new connection when that happens?

ideclon
1 Jan, 2024, 14:34

It's after 100 seconds

waheedoof
1 Jan, 2024, 14:35

i edited the first message for 100 seconds

ideclon
1 Jan, 2024, 14:38

Well, best would be to have that route bypass CF and not time out (I think that's possible, but not on the free plan. Alternatively, you could have a separate endpoint that doesn't go through CF for the WebSockets)?

Of course, once the user leaves the app in the background, it's likely it'll have all connections closed by battery saving, anyway. You could then recreate the onnection when the user re-enters the app

waheedoof
1 Jan, 2024, 14:38

Should i keep sending a light request to refresh the subscription every 90 seconds or create a new subscription every 90 seconds which is better ?

ideclon
1 Jan, 2024, 14:39

If you have to choose between opening a new connection every minute and updating a document (to keep the connection alive) every minute, I'd say keep the connection alive

waheedoof
1 Jan, 2024, 14:46

No,Now the question is: Sending a light request to refresh the subscription every 90 seconds or create a new subscription but when user interact with the app after 90 seconds (or more, may be 5 min) which is better ? what is the choosing caused ?

ideclon
1 Jan, 2024, 14:52

I assume by interact with the app you just mean the app is active?

waheedoof
1 Jan, 2024, 14:54

I mean that the user is clicking on any part of the app screen.

ideclon
1 Jan, 2024, 14:56

Well, what are you using Realtime for?

waheedoof
1 Jan, 2024, 15:01

I am using this for a chat AI app. There are instances when the user leaves the app without closing it.

ideclon
1 Jan, 2024, 16:03

But what are you doing with Realtime?

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more