Back

How to send keep alive packets to maintain a realtime web socket via Cloudflare?

  • 0
  • Flutter
  • Realtime
Tanner Meade
27 Sep, 2023, 00:16

I think the realtime API for appwrite might be standard enough that using that websocket package might be easier than forking the appwrite sdk.

TL;DR
The user is discussing how to maintain a realtime web socket connection via Cloudflare. They are using a different package than the web_socket_client package due to issues with the timeout. The user suggests using retry logic and mentions that the connection may be dropped when the app is in the background or in doze mode. The user shares an updated version of their code using another package that has better error handling. There is no specific solution mentioned in the thread.
Tanner Meade
27 Sep, 2023, 00:16

From looking at the realtime network requests

Joshi
27 Sep, 2023, 00:21

If you try it report back how it goes

Tanner Meade
27 Sep, 2023, 01:53

Got it working:

Tanner Meade
27 Sep, 2023, 01:55

Used a more popular web socket package than the one you linked so I don't have to worry about it being supported long term.

Tanner Meade
27 Sep, 2023, 01:56
Tanner Meade
27 Sep, 2023, 01:59

It'll log the heartbeats:

Joshi
27 Sep, 2023, 02:00

Good to know that it works. web socket channel is somewhat the official version for using websockets, the library I suggested is just a package using it below with their own retry logic and all that jazz on top. the maintainer ain't a noname tho. He is responsible for bloc, mason and other top packages and a well known figure. Nonetheless a good approach you did there

Joshi
27 Sep, 2023, 02:00

Does is counter the cloudflare 100s disconnect?

Tanner Meade
27 Sep, 2023, 02:00

Yes. The logs you see above are to a hosted thru cloudflare appwrite cluster.

Tanner Meade
27 Sep, 2023, 02:02

Didn't see that, I might pop that package in right now because the retry logic is kinda nice.

Joshi
27 Sep, 2023, 02:04

It's really needed. I have worked with on a different project where they used websockets and the official package is literally unusable without retry logic.

Joshi
27 Sep, 2023, 02:05

Because disconnects can happen and especially when the app gets put into the background the realtime connection just gets dropped

Joshi
27 Sep, 2023, 02:05

or when put into doze which can happen after a few seconds if the app is in the background

Tanner Meade
27 Sep, 2023, 02:20

Here's the updated one using the other package. Stays connected and has a bit better error handling too 🎉

Tanner Meade
27 Sep, 2023, 02:20

@Joshi

Joshi
27 Sep, 2023, 02:36

That's great <a:aPES2_Crazy:525603278830960640>

Joshi
27 Sep, 2023, 02:36

Have you tried to put the app into doze?

Joshi
27 Sep, 2023, 02:37

From my experience it takes around 90-120 seconds but would be nice to see if it reconnects afterwards

Tanner Meade
27 Sep, 2023, 03:14

Haven't tried doze. But with the combination of the subscription onDone trigger and AppLifeCycle it should be achievable.

Joshi
27 Sep, 2023, 09:44

It should reconnect even withput AppLifeCycle no?

Tanner Meade
28 Sep, 2023, 21:50

The fancy package is having issues with the timeout not working for Cloudflare and just reconnects the websocket when it fails every 100 secs. I already had retry logic built into the more popular package, so I'm using that one. Not as fancy retry logic, but it's not hard to implement what is needed.

Joshi
29 Sep, 2023, 05:41

Can you go a bit more into detail what you mean

Tanner Meade
29 Sep, 2023, 15:38

I couldn't get the timeout to work with the web_socket_client package. So I'm using other package because I can control what is sent as the heartbeat package and when it is sent too (doesn't need to be sent until 100 sec after the last sent package).

Tanner Meade
29 Sep, 2023, 15:40

It's working well. The only downside is I have to manage the reconnect logic myself which isn't hard.

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