Order status, chat, live location, presence: many mobile apps need data to appear without the user pulling to refresh. There are several ways to build that, and the right one depends on how fresh the data has to be and what the mobile platform allows. Here is how I choose, plus a WebSocket client that survives bad networks.

The options

ApproachHow it worksGood forWatch out for
PollingClient asks every N secondsSlow-changing data, simple backendsWasted requests, battery, delay up to N seconds
Long pollingServer holds the request until there is newsModerate updates, no socket supportConnection churn, server resources
Server-Sent EventsOne-way stream from serverLive feeds, notifications to the clientOne direction only
WebSocketsTwo-way persistent connectionChat, live tracking, collaborative stateYou own reconnection and state sync
Managed realtime (Supabase Realtime, Firebase)Database changes pushed to clientsApps already on that backendVendor model and quotas
Push notifications (FCM/APNs)OS delivers a messageWaking a closed or background appNot a data channel; delivery is best effort

A realistic app often combines them: a WebSocket or realtime channel while the app is open, and push notifications to bring the user back.

When a managed service is enough

If your data already lives in Supabase or Firebase, listening to changes is a few lines:

dart
final channel = supabase
    .channel('public:messages')
    .onPostgresChanges(
      event: PostgresChangeEvent.insert,
      schema: 'public',
      table: 'messages',
      filter: PostgresChangeFilter(
        type: PostgresChangeFilterType.eq,
        column: 'chat_id',
        value: chatId,
      ),
      callback: (payload) => onNewMessage(Message.fromJson(payload.newRecord)),
    )
    .subscribe();

Remember to unsubscribe when the screen is disposed, and make sure row-level security prevents users from receiving rows they should not see.

When you need your own WebSocket

For custom protocols (live driver location, order-status events from your own backend), you own the connection. Mobile makes this hard in three ways: networks drop silently, the OS suspends background apps, and users move between Wi-Fi and cellular.

A reconnecting client

dart
class RealtimeClient {
  RealtimeClient(this._uri);
  final Uri _uri;

  final _controller = StreamController<Map<String, dynamic>>.broadcast();
  Stream<Map<String, dynamic>> get events => _controller.stream;

  WebSocketChannel? _channel;
  int _attempt = 0;
  bool _closed = false;

  void connect() {
    if (_closed) return;
    _channel = WebSocketChannel.connect(_uri);
    _channel!.stream.listen(
      (raw) {
        _attempt = 0; // healthy connection resets backoff
        _controller.add(jsonDecode(raw as String) as Map<String, dynamic>);
      },
      onError: (_) => _scheduleReconnect(),
      onDone: _scheduleReconnect,
    );
  }

  void _scheduleReconnect() {
    if (_closed) return;
    final seconds = min(30, pow(2, _attempt++).toInt());
    final jitter = Random().nextInt(1000);
    Future.delayed(Duration(seconds: seconds, milliseconds: jitter), connect);
  }

  void send(Map<String, dynamic> message) => _channel?.sink.add(jsonEncode(message));

  void close() {
    _closed = true;
    _channel?.sink.close();
    _controller.close();
  }
}

Reconnecting is only half the job. Add these as well:

  • Heartbeats. Send a ping on an interval and treat a missing pong as a dead connection; half-open sockets can look healthy for minutes.
  • Authentication on connect. Send a short-lived token and re-authenticate on every reconnect (see JWT refresh in Flutter).
  • Lifecycle awareness. Close the socket when the app goes to the background and reconnect on resume, using WidgetsBindingObserver.

The part people forget: catching up

Events sent while you were disconnected are gone. After every reconnect, resync over REST: ask the server for everything since the last event id you processed, then resume the live stream.

dart
Future<void> onReconnected() async {
  final missed = await api.eventsSince(lastEventId);
  for (final e in missed) { apply(e); }
}

Give events monotonically increasing ids or timestamps so you can drop duplicates and apply them in order. This also pairs naturally with the local-first approach in offline-first Flutter apps.

Where it belongs in your architecture

The socket is a data source. Expose it as a Stream<OrderUpdate> from a repository interface so BLoCs and tests do not know it is a WebSocket; see Clean Architecture in Flutter.

Key takeaways

  • Match the tool to the freshness you need: polling is fine for slow data, WebSockets for two-way live data.
  • A managed realtime service is the shortest path if you are already on that backend.
  • Own your reconnection: backoff with jitter, heartbeats, re-auth and lifecycle handling.
  • After reconnecting, resync missed events from the server.

I have built real-time order-status updates with WebSockets for delivery apps. If you need live features in your mobile product, contact me.