# Real-Time Communication System — Build Progress

## Architecture
- Module: `backend/src/modules/realtime/`
- Namespace: `/realtime`
- Redis-ready via `ISocketAdapter` abstraction
- Socket state kept in-memory (never in MySQL)
- JWT handshake authentication + role/user room isolation

## Sections

| # | Section | Status |
|---|---------|--------|
| 1 | Backend Foundation (types, interfaces, auth guard) | ✅ DONE |
| 2 | Backend Services (socket.service.ts + gateway/socket.service.ts) | ✅ DONE |
| 3 | Backend Event Emitters (wallet/order/notification/admin) | ✅ DONE |
| 4 | Backend Gateway + Module (socket.gateway.ts, socket.module.ts) | ✅ DONE |
| 5 | Backend Integration (wire Wallet/Order/Notification/Admin services) | ✅ DONE |
| 6 | Backend Tests (unit + integration) | ✅ DONE |
| 7 | Frontend Core (config, client, context, provider, store) | ✅ DONE |
| 8 | Frontend Hooks & Listeners | ✅ DONE |
| 9 | Frontend Components (ConnectionStatus, LiveToast, layout) | ✅ DONE |
| 10 | Frontend Tests + Docs | ⏳ PENDING |

## Generated Files

### Backend — Realtime Module (`backend/src/modules/realtime/`)
| File | Description |
|------|-------------|
| `types/socket.types.ts` | Event names, room helpers, payload types, enums |
| `interfaces/events.interface.ts` | ISocketAdapter, IConnectionManager, IRoomManager, IHeartbeatMonitor, event emitter interfaces |
| `auth/socket-auth.guard.ts` | JWT handshake validation, user status checks, rate limiting |
| `services/socket.service.ts` | In-memory connection registry, room manager, heartbeat, rate limiting |
| `gateway/socket.service.ts` | SocketEventEmitter — implements ISocketAdapter for socket.io transport |
| `events/wallet.events.ts` | wallet.created/updated/locked/unlocked/balanceChanged |
| `events/order.events.ts` | order.created/updated/matched/completed/cancelled |
| `events/notification.events.ts` | notification.created/updated/read/deleted/unreadCount |
| `events/admin.events.ts` | admin.broadcast/announcement/server.maintenance |
| `gateway/socket.gateway.ts` | Main `/realtime` gateway with connection lifecycle, JWT auth, room joining |
| `gateway/socket.module.ts` | RealtimeModule wiring |
| `test/socket-auth.guard.spec.ts` | Unit tests for auth guard |
| `test/socket.service.spec.ts` | Unit tests for connection/room/heartbeat/rate-limit |
| `test/wallet.events.spec.ts` | Unit tests for wallet event emitters |
| `test/order.events.spec.ts` | Unit tests for order event emitters |
| `test/notification.events.spec.ts` | Unit tests for notification event emitters |

### Backend — Integration Edits
| File | Description |
|------|-------------|
| `app.module.ts` | RealtimeModule registered |
| `package.json` | @nestjs/websockets, @nestjs/platform-socket.io aligned to v11 |
| `modules/wallet/wallet.module.ts` | Imports RealtimeModule |
| `modules/wallet/wallet.service.ts` | WalletEvents injected; emissions after deposit/withdraw/transfer/credit/debit |
| `modules/orders/orders.module.ts` | Imports RealtimeModule |
| `modules/orders/orders.service.ts` | OrderEvents injected; emissions after create/cancel |
| `modules/matching/services/matching.service.ts` | OrderEvents injected; emissions after match |
| `modules/notifications/notifications.module.ts` | Imports RealtimeModule; removed old NotificationGateway |
| `modules/notifications/services/notification.service.ts` | NotificationEvents injected; all emissions migrated |

### Frontend — Socket System (`frontend/`)
| File | Description |
|------|-------------|
| `config/socket.config.ts` | Socket.IO client configuration, event names, heartbeat constants |
| `types/socket.types.ts` | Frontend socket payload types (mirrors backend) |
| `store/socket.store.ts` | Zustand store for socket connection state |
| `context/SocketContext.tsx` | React Context + Provider with lifecycle, JWT auth, reconnection |
| `hooks/useSocket.ts` | All socket hooks: useWalletCreated/Updated/Locked/Unlocked/BalanceChanged, useOrderCreated/Updated/Matched/Completed/Cancelled, useNotificationCreated/Updated/Read/Deleted/UnreadCount, useAdminBroadcast/Announcement/Maintenance, useReconnectHandler, useConnectionStatus |
| `components/socket/LiveToast.tsx` | Toast notifications for admin broadcasts, announcements, maintenance |
| `components/socket/SocketConnectionStatus.tsx` | Connection status indicator with latency, reconnect |
| `app/layout.tsx` | SocketProvider + LiveToast wired into root layout |
