Skip to content

Latest commit

 

History

History
193 lines (148 loc) · 5.51 KB

File metadata and controls

193 lines (148 loc) · 5.51 KB

✅ Supabase Realtime Implementation - Complete

📦 What Was Created

1. Core Files

  • client/src/lib/supabase.ts - Supabase client for frontend
  • client/src/hooks/useRealtimeSubscription.ts - Reusable Realtime hook
  • client/src/components/payments/PaymentsTable.tsx - Example implementation

2. Documentation

  • REALTIME_GUIDE.md - Complete implementation guide
  • REALTIME_QUICKSTART.tsx - Quick reference examples
  • ✅ Updated .env.example with frontend Supabase config

🎯 Next Steps

Step 1: Configure Environment Variables

Local Development (.env file):

# Add these to your .env file (use same values as backend)
VITE_SUPABASE_URL=https://your-project-id.supabase.co
VITE_SUPABASE_ANON_KEY=your_anon_key_here

Vercel Dashboard:

  1. Go to your Vercel project settings
  2. Navigate to Environment Variables
  3. Add:
    • VITE_SUPABASE_URL = Your Supabase URL
    • VITE_SUPABASE_ANON_KEY = Your Supabase anon key (NOT service role key)

Step 2: Enable Realtime in Supabase

  1. Open your Supabase Dashboard
  2. Go to DatabaseReplication
  3. Enable replication for these tables:
    • payments
    • properties
    • units
    • tenants
    • leases

Step 3: Add to Your Components

Add this single line to components that need real-time updates:

import { useRealtimeSubscription } from '@/hooks/useRealtimeSubscription';

// In your component function:
useRealtimeSubscription('table_name', ['queryKey']);

Step 4: Test It Out

  1. Open your dashboard in two browser windows side by side
  2. In one window, add/edit/delete a record (e.g., a payment)
  3. Watch the other window update automatically! ✨

📋 Components to Update

Here's where you should add Realtime subscriptions:

High Priority (Data Changes Frequently)

  • Payments - client/src/pages/dashboard/landlord-payments.tsx

    useRealtimeSubscription('payments', ['payments']);
  • Dashboard Stats - client/src/components/SimpleDashboard.tsx

    useRealtimeSubscription('properties', ['properties']);
    useRealtimeSubscription('payments', ['payments']);

Medium Priority

  • Tenants - client/src/pages/dashboard/landlord-tenants.tsx

    useRealtimeSubscription('tenants', ['tenants']);
  • Leases - client/src/pages/dashboard/landlord-leases.tsx

    useRealtimeSubscription('leases', ['leases']);

Lower Priority (Data Changes Less Frequently)

  • Properties - client/src/pages/dashboard/landlord-properties.tsx

    useRealtimeSubscription('properties', ['properties']);
  • Units - client/src/pages/dashboard/landlord-units.tsx

    useRealtimeSubscription('units', ['units']);

🧪 Testing Checklist

  • Environment variables added (local + Vercel)
  • Supabase Realtime enabled for tables
  • Added subscription to at least one component
  • Deployed to Vercel
  • Tested in browser console (see [Realtime] logs)
  • Tested dual browser windows (see automatic updates)

🎨 Optional Enhancements

Add Visual Feedback

Show a subtle indicator when data is refreshing:

const { data, isFetching } = useQuery({...});

return (
  <div className="relative">
    {isFetching && (
      <div className="absolute top-2 right-2">
        <div className="h-2 w-2 rounded-full bg-primary animate-pulse" />
      </div>
    )}
    {/* Your content */}
  </div>
);

Add Toast Notifications

Notify users when data updates:

import { useToast } from '@/hooks/use-toast';

const { toast } = useToast();

useEffect(() => {
  const channel = supabase.channel('payments_changes')
    .on('postgres_changes', { event: '*', schema: 'public', table: 'payments' }, 
      (payload) => {
        queryClient.invalidateQueries(['payments']);
        toast({
          title: "Payment Updated",
          description: "The payments list has been updated.",
        });
      }
    )
    .subscribe();

  return () => { supabase.removeChannel(channel); };
}, []);

📊 Benefits You'll Get

Instant Updates - No page refresh needed 🔄 Multi-User Sync - All users see changes in real-time 🎯 Simple Integration - Just one line per component 🧹 Auto Cleanup - No memory leaks 📱 Works Everywhere - Desktop, tablet, mobile 🚀 Better UX - Users love seeing live data

🐛 Troubleshooting

Console shows: "Missing Supabase environment variables"

  • Check that VITE_SUPABASE_URL and VITE_SUPABASE_ANON_KEY are set
  • Restart your dev server after adding env vars
  • In Vercel, redeploy after adding env vars

Subscription never shows "SUBSCRIBED"

  • Check that Realtime is enabled for the table in Supabase Dashboard
  • Verify table name matches exactly (case-sensitive)
  • Check browser console for connection errors

Updates not triggering refetch

  • Verify query key matches exactly
  • Check that TanStack Query is configured correctly
  • Look for [Realtime] Invalidated query key in console

"Channel not found" errors

  • Each component creates its own channel - this is normal
  • Channels are cleaned up automatically on unmount
  • Don't reuse channel names across different tables

📚 Additional Resources


You're all set! Your dashboard will now update in real-time. 🎉