Module 4
Topic 5

Realtime

Building live, collaborative features with Supabase Realtime — database subscriptions, presence, and streams.

Supabase Realtime Overview Realtime Listeners
What Is Realtime?

Supabase Realtime is a feature that allows you to listen to database changes in real-time and build live, collaborative features into your Flutter apps. It's built on top of PostgreSQL and uses WebSockets for efficient communication.

✅ What You Can Build with Realtime

  • Live Chat – Real-time message updates without polling
  • Collaborative Editing – Multiple users editing the same document
  • Live Dashboards – Real-time data visualization
  • Presence Systems – See who's online and what they're doing
  • Live Feeds – Social media feeds that update in real-time
  • Game Leaderboards – Live score updates

Polling (Traditional)

  • Repeated HTTP requests (every few seconds)
  • Wastes bandwidth and battery
  • High latency (up to the polling interval)
  • More server load
  • Simple to implement

WebSockets (Realtime)

  • Persistent connection
  • Efficient — only sends when data changes
  • Instant updates (milliseconds)
  • Less server load
  • More complex setup
Realtime Channels

Realtime works through channels — logical groupings of subscriptions. You can subscribe to changes on specific tables or even specific rows.

Presence System (Online Users)

Presence allows you to track who's online and what they're doing. It's perfect for chat apps, collaborative tools, and social features.

Streams and Subscriptions

You can convert Realtime subscriptions into Dart Streams for easy integration with Flutter's reactive programming model.

Complete Example: Live Chat App

Here's a complete live chat app using Supabase Realtime for instant message updates.

Step-by-Step Explanation
1.
Enable Realtime – In Supabase Dashboard, enable Realtime for the tables you want to listen to.
2.
Create a channel – Use client.channel('public:table_name') to create a Realtime channel.
3.
Listen to events – Use .onPostgresChange() to listen to INSERT, UPDATE, DELETE events.
4.
Subscribe – Call .subscribe() to start receiving real-time updates.
5.
Update UI – When a change is received, update your UI using setState or StreamBuilder.
Best Practices

✅ Do's

  • Always unsubscribe from channels when leaving a screen
  • Use filters to only listen to relevant data (e.g., specific room)
  • Handle reconnection logic for network interruptions
  • Use StreamBuilder or ValueListenableBuilder for reactive UI
  • Limit the number of channels to avoid performance issues

❌ Don'ts

  • Don't forget to dispose streams and channels
  • Don't listen to all tables — only subscribe to what you need
  • Don't rely on Realtime for critical data consistency (use transactions)
Common Mistakes
❌ Mistake 1: Not enabling Realtime in the dashboard

Realtime must be enabled for each table in the Supabase Dashboard. Without this, subscriptions won't work.

✅ Correct: Enable Realtime for your tables

Go to Database → Replication and enable Realtime for the tables you need.

❌ Mistake 2: Not unsubscribing from channels

Leaving channels open can cause memory leaks and unnecessary network usage.

✅ Correct: Always unsubscribe

Call channel.unsubscribe() in dispose() or when navigating away.

❌ Mistake 3: Not handling connection errors

Network issues can cause Realtime connections to drop. Handle reconnection gracefully.

✅ Correct: Handle reconnection

Use the onReconnection callback or listen to the connection status.

🎯 Key Takeaway

Supabase Realtime enables live, collaborative features in your Flutter apps. Use channels to subscribe to database changes, presence to track online users, and streams for reactive programming. Always enable Realtime in the dashboard and unsubscribe from channels when done.