Menu

1 / 6

resilient_ws: WebSocket and SSE clients for Dart that survive bad networks

DEV Community: mobile·Abdeldjalil Chougui·about 1 hour ago
#QXRVhBfA
#devtomobile#devto#dev#server#token
Reading 0:00
15s threshold

Opening a WebSocket is one line of code. Keeping it open on a phone is the hard part. The user walks into a lift, the phone switches from Wi-Fi to mobile data, the app goes to the background, the access token expires. The socket dies, sometimes with an error and sometimes without a word.

So every realtime feature ends up with the same code around the socket: reconnect, but not in a tight loop. Get a fresh token before reconnecting, because the old one may have expired. Notice a connection that is dead but still looks open. Re-subscribe to the topics. Do something sensible with messages sent while offline. And stop for good when the server says "you are not allowed", instead of retrying forever.

resilient_ws packages that code. It has two realtime clients for Dart and Flutter that keep working on bad mobile networks: a WebSocket client and a Server-Sent Events client. Both reconnect on their own, fetch a fresh token for every connection, and stop cleanly when told to.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.