# Task 3 — ISP Socket.IO WebSocket Mini-Service

## What was created

### Files
- **`/home/z/my-project/mini-services/isp-socket/package.json`** — Project manifest with `socket.io ^4.7.0` and a `dev` script using `bun --hot` for hot-reload.
- **`/home/z/my-project/mini-services/isp-socket/index.ts`** — Full Socket.IO server on port **3003**.

### Features implemented

| Feature | Description |
|---|---|
| **Online Users Tracking** | Tracks users by socket id; broadcasts updated list to `subscribe:online` subscribers. |
| **System Notifications** | In-memory notification store; broadcasts to all admin-role users; supports `notification:read` to mark as read. |
| **Real-time Dashboard Stats** | Subscribers to `subscribe:dashboard` receive a stats payload on subscribe and whenever stats change (e.g., active user count updates on join/leave). |
| **Ticket Updates** | `ticket:update` event shape defined and ready for API routes to emit via the exported `io` instance. |
| **Payment Notifications** | `payment:received` event shape defined and ready for API routes to emit. |

### Events

#### Client → Server
| Event | Payload | Description |
|---|---|---|
| `join` | `{ userId, name, role }` | Register the user as online |
| `leave` | — | Explicit disconnect |
| `subscribe:dashboard` | — | Subscribe to real-time dashboard stats |
| `subscribe:online` | — | Subscribe to online user list updates |
| `notification:read` | `{ notificationId }` | Mark a notification as read |

#### Server → Client
| Event | Payload | Description |
|---|---|---|
| `online-users` | `{ users: [{ id, name, status, lastSeen }] }` | Current online user list |
| `notification` | `{ id, type, title, message, createdAt }` | New system notification |
| `dashboard:stats` | `{ totalUsers, activeUsers, revenue, … }` | Dashboard statistics snapshot |
| `ticket:update` | `{ ticketId, status, message, updatedAt }` | Ticket status change |
| `payment:received` | `{ paymentId, userId, amount, plan, timestamp }` | Payment received event |

### Inter-service communication
- The `io` instance is exported from `index.ts` **and** attached to `globalThis.__ispSocketIO` so that API routes in the main Next.js app can import or access it to push events (e.g., when a payment is created or a ticket is updated).

### Configuration
- **Port**: 3003
- **Path**: `/` (required for Caddy proxy)
- **CORS**: `origin: '*'`
- **Hot reload**: enabled via `bun --hot`
- **Graceful shutdown**: handles SIGTERM and SIGINT

### Verification
- Service starts cleanly and shuts down gracefully on SIGTERM.
