149 lines
4.4 KiB
Markdown
149 lines
4.4 KiB
Markdown
# Pager Socket.IO Testing Guide
|
|
|
|
## Quick Start
|
|
|
|
1. **Start your NestJS server:**
|
|
```bash
|
|
npm run start:dev
|
|
```
|
|
|
|
2. **Open the test page:**
|
|
- Open `test-pager-socket.html` in your web browser
|
|
- Or serve it via a simple HTTP server:
|
|
```bash
|
|
# Using Python
|
|
python3 -m http.server 8080
|
|
# Then open http://localhost:8080/test-pager-socket.html
|
|
|
|
# Using Node.js (if you have http-server installed)
|
|
npx http-server -p 8080
|
|
```
|
|
|
|
## Testing Scenarios
|
|
|
|
### Scenario 1: Test as Restaurant Admin/Staff
|
|
|
|
1. **Get your admin JWT token:**
|
|
- Login as admin via your API to get the JWT token
|
|
- Example:
|
|
```bash
|
|
curl -X POST http://localhost:4000/admin/auth/login \
|
|
-H "Content-Type: application/json" \
|
|
-d '{
|
|
"email": "admin@example.com",
|
|
"password": "password"
|
|
}'
|
|
```
|
|
- Copy the `accessToken` from the response
|
|
|
|
2. **Connect to the server:**
|
|
- Enter server URL: `http://localhost:4000`
|
|
- Enter your admin JWT token in the "Admin Token" field
|
|
- Click "Connect"
|
|
|
|
3. **Join restaurant room:**
|
|
- Select "Restaurant (Admin/Staff)" from User Type
|
|
- Click "Join Room"
|
|
- The restaurant ID will be automatically extracted from your token
|
|
|
|
3. **Create a pager request (via API):**
|
|
```bash
|
|
curl -X POST http://localhost:4000/public/pager \
|
|
-H "Content-Type: application/json" \
|
|
-H "X-Slug: your-restaurant-slug" \
|
|
-d '{
|
|
"tableNumber": "5",
|
|
"message": "Need assistance"
|
|
}'
|
|
```
|
|
|
|
4. **Observe the event:**
|
|
- You should see `pager:created` event in the Events Log
|
|
|
|
### Scenario 2: Test as Public User
|
|
|
|
1. **Create a pager request first (to get cookie ID):**
|
|
```bash
|
|
curl -X POST http://localhost:4000/public/pager \
|
|
-H "Content-Type: application/json" \
|
|
-H "X-Slug: your-restaurant-slug" \
|
|
-d '{
|
|
"tableNumber": "3",
|
|
"message": "Need water"
|
|
}' \
|
|
-c cookies.txt
|
|
```
|
|
|
|
2. **Get the cookie ID:**
|
|
- Check the response or cookies.txt file for `pagerId` cookie value
|
|
|
|
3. **Connect and join session:**
|
|
- Connect to the server
|
|
- Select "Session (Public User)" from User Type
|
|
- Enter the cookie ID from step 2
|
|
- Click "Join Room"
|
|
|
|
4. **Update pager status (via API as admin):**
|
|
```bash
|
|
curl -X PATCH http://localhost:4000/admin/pager/{pagerId}/status \
|
|
-H "Content-Type: application/json" \
|
|
-H "Authorization: Bearer YOUR_ADMIN_TOKEN" \
|
|
-d '{
|
|
"status": "acknowledged"
|
|
}'
|
|
```
|
|
|
|
5. **Observe the event:**
|
|
- You should see `pager:status:updated` event in the Events Log
|
|
|
|
## Socket Events Reference
|
|
|
|
### Client → Server Events
|
|
|
|
| Event | Payload | Description |
|
|
|-------|---------|-------------|
|
|
| `join:restaurant` | `{}` (empty, restId extracted from token) | Join restaurant room for admin/staff. **Requires authentication via JWT token in connection auth** |
|
|
| `join:session` | `{ cookieId: string }` | Join session room for public user |
|
|
| `leave:room` | `{ room: string }` | Leave a specific room |
|
|
|
|
**Note:** For `join:restaurant`, the restaurant ID is automatically extracted from the authenticated admin's JWT token. You must provide the token when connecting:
|
|
|
|
```javascript
|
|
const socket = io('http://localhost:4000/pager', {
|
|
auth: {
|
|
token: 'your-jwt-token-here' // or 'Bearer your-jwt-token-here'
|
|
}
|
|
});
|
|
```
|
|
|
|
### Server → Client Events
|
|
|
|
| Event | Payload | Description |
|
|
|-------|---------|-------------|
|
|
| `pager:created` | `{ pager: {...} }` | New pager request created |
|
|
| `pager:status:updated` | `{ pager: {...} }` | Pager status updated |
|
|
| `joined` | `{ room: string, message: string }` | Successfully joined a room |
|
|
| `left` | `{ room: string, message: string }` | Successfully left a room |
|
|
| `error` | `{ message: string }` | Error occurred |
|
|
|
|
## Room Names
|
|
|
|
- **Restaurant Room:** `restaurant:{restId}`
|
|
- **Session Room:** `cookie:{cookieId}`
|
|
|
|
## Testing Tips
|
|
|
|
1. **Open multiple browser tabs/windows** to simulate multiple clients
|
|
2. **Use browser DevTools** to inspect WebSocket connections
|
|
3. **Check server logs** to see gateway activity
|
|
4. **Test reconnection** by disconnecting and reconnecting
|
|
5. **Test room isolation** by joining different rooms in different tabs
|
|
|
|
## Troubleshooting
|
|
|
|
- **Connection fails:** Check if server is running on the correct port
|
|
- **No events received:** Verify you've joined the correct room
|
|
- **CORS errors:** Ensure CORS is enabled in your NestJS app (already configured in gateway)
|
|
- **Events not showing:** Check browser console for errors
|
|
|