Loading gists…
A Text snippet, saved for reference.
| 1 | # Migration from Socket.io to Supabase Realtime |
| 2 | |
| 3 | This document outlines the changes made to migrate the Realtime Restaurant Management System from a custom Socket.io implementation to Supabase Realtime. |
| 4 | |
| 5 | ## Rationale |
| 6 | - **Deployment Issues:** Socket.io often requires sticky sessions or complex proxy configurations on many hosting platforms. |
| 7 | - **Scalability:** Supabase Realtime is managed and scales automatically. |
| 8 | - **Reliability:** Direct database-to-client updates reduce backend logic overhead. |
| 9 | |
| 10 | ## Changes Made |
| 11 | |
| 12 | ### Frontend |
| 13 | 1. **Supabase Client:** Created `client/src/supabaseClient.ts` to manage the connection. |
| 14 | 2. **Environment Variables:** Added `VITE_SUPABASE_URL` and `VITE_SUPABASE_ANON_KEY` to the `.env` file. |
| 15 | 3. **Realtime Store:** Refactored `useOrderStore.tsx` to replace `initializeSocket` with `initializeRealtime`. |
| 16 | - It now listens for `postgres_changes` on `orders` and `order_items`. |
| 17 | - Handles `INSERT` and `UPDATE` events automatically. |
| 18 | 4. **Component Updates:** Updated `AdminDashboardView.tsx`, `AdminOrdersView.tsx`, and `WalkInOrder.tsx` to use the new realtime initialization. |
| 19 | |
| 20 | ### Backend |
| 21 | 1. **Removed Socket Initialization:** Updated `server/src/server.ts` to remove the HTTP server socket binding. |
| 22 | 2. **Removed Socket Logic:** Cleaned up `order.service.ts`, `payment.service.ts`, and `credit.service.ts` to remove all manual socket emissions. |
| 23 | 3. **Files to Delete:** (Optional/Recommended) |
| 24 | - `server/src/socket.ts` |
| 25 | - `client/src/api/socket.ts` |
| 26 | |
| 27 | ## Supabase Dashboard Setup (Required) |
| 28 | |
| 29 | To ensure realtime updates work, you **must** configure your Supabase project: |
| 30 | |
| 31 | ### 1. Enable Realtime Replication |
| 32 | Go to **Database > Replication** in your Supabase Dashboard and enable it for: |
| 33 | - `orders` table |
| 34 | - `order_items` table |
| 35 | |
| 36 | ### 2. Row Level Security (RLS) |
| 37 | Ensure RLS is enabled on the `orders` and `order_items` tables. At a minimum, you need a policy that allows reading: |
| 38 | - **Policy:** `Enable read access for all users` (or Restricted to authenticated if using Auth). |
| 39 | - **Target:** `SELECT` |
| 40 | |
| 41 | ## Verification |
| 42 | - Place an order from the customer view or Walk-in view. |
| 43 | - The Admin Dashboard and Orders page should update instantly without refresh. |
| 44 | - Change an order status; the update should reflect across all admin views. |
| 45 |