# Session Start WebSocket Event This document describes the `session.started` WebSocket event that is broadcast when a new game session is created. ## ๐Ÿ“‹ Event Overview When a new session is created, the backend broadcasts a `session.started` event to all subscribed WebSocket clients. This allows bots and other integrations to react immediately to new game sessions. ## ๐Ÿ”Œ WebSocket Connection **Endpoint:** `ws://localhost:5000/api/sessions/live` **Authentication:** Required (JWT token) ## ๐Ÿ“จ Event Format ### Event Type ``` session.started ``` ### Full Message Structure ```json { "type": "session.started", "timestamp": "2025-11-01T20:00:00.123Z", "data": { "session": { "id": 17, "is_active": 1, "created_at": "2025-11-01T20:00:00.123Z", "notes": "Friday Game Night" } } } ``` ### Field Descriptions | Field | Type | Description | |-------|------|-------------| | `type` | string | Always `"session.started"` | | `timestamp` | string | ISO 8601 timestamp when the event was generated | | `data.session.id` | number | The ID of the newly created session | | `data.session.is_active` | number | Always `1` (active) for new sessions | | `data.session.created_at` | string | ISO 8601 timestamp when the session was created | | `data.session.notes` | string/null | Optional notes for the session | ## ๐Ÿš€ Implementation ### Backend Implementation The `session.started` event is automatically broadcast when: 1. **New Session Created**: Admin creates a session via `POST /api/sessions` **Code Location:** `backend/routes/sessions.js` - `POST /` endpoint ```javascript // Broadcast session.started event via WebSocket const wsManager = getWebSocketManager(); if (wsManager) { const eventData = { session: { id: newSession.id, is_active: 1, created_at: newSession.created_at, notes: newSession.notes } }; wsManager.broadcastEvent('session.started', eventData, parseInt(newSession.id)); } ``` ### Client Implementation Example #### Node.js with `ws` library ```javascript const WebSocket = require('ws'); const ws = new WebSocket('ws://localhost:5000/api/sessions/live'); ws.on('open', () => { // Authenticate ws.send(JSON.stringify({ type: 'auth', token: 'your-jwt-token' })); }); ws.on('message', (data) => { const message = JSON.parse(data.toString()); switch (message.type) { case 'auth_success': // Subscribe to the new session (or subscribe when you receive session.started) ws.send(JSON.stringify({ type: 'subscribe', sessionId: 17 })); break; case 'session.started': console.log('New session started!'); console.log(`Session ID: ${message.data.session.id}`); console.log(`Created at: ${message.data.session.created_at}`); if (message.data.session.notes) { console.log(`Notes: ${message.data.session.notes}`); } // Auto-subscribe to the new session ws.send(JSON.stringify({ type: 'subscribe', sessionId: message.data.session.id })); break; } }); ``` #### Python with `websockets` library ```python import asyncio import json import websockets async def listen_for_session_start(): uri = "ws://localhost:5000/api/sessions/live" async with websockets.connect(uri) as websocket: # Authenticate await websocket.send(json.dumps({ "type": "auth", "token": "your-jwt-token" })) async for message in websocket: data = json.loads(message) if data["type"] == "auth_success": print("Authenticated, waiting for sessions...") elif data["type"] == "session.started": session = data["data"]["session"] print(f"๐ŸŽฎ New session started! ID: {session['id']}") print(f"๐Ÿ“… Created: {session['created_at']}") if session.get('notes'): print(f"๐Ÿ“ Notes: {session['notes']}") # Auto-subscribe to the new session await websocket.send(json.dumps({ "type": "subscribe", "sessionId": session["id"] })) asyncio.run(listen_for_session_start()) ``` ## ๐Ÿงช Testing ### Manual Testing Steps 1. **Start the backend server:** ```bash cd backend npm start ``` 2. **Connect a WebSocket client** (use the test script or your own): ```bash # You can modify test-session-end-websocket.js to listen for session.started ``` 3. **Create a new session** in the Picker UI or via API: ```bash curl -X POST http://localhost:5000/api/sessions \ -H "Authorization: Bearer " \ -H "Content-Type: application/json" \ -d '{"notes": "Friday Game Night"}' ``` 4. **Verify the event is received** by your WebSocket client ### Expected Event ```json { "type": "session.started", "timestamp": "2025-11-01T20:00:00.123Z", "data": { "session": { "id": 18, "is_active": 1, "created_at": "2025-11-01T20:00:00.123Z", "notes": "Friday Game Night" } } } ``` ## ๐Ÿค– Bot Integration ### IRC/Kosmi Bot Example When the bot receives a `session.started` event, it should: 1. **Announce the new game session** to users 2. **Auto-subscribe to the session** to receive game.added and session.ended events 3. **Optionally display session info** (notes, ID, etc.) Example bot response: ``` ๐ŸŽฎ Game Night has started! Session #18 ๐Ÿ“ Friday Game Night ๐Ÿ—ณ๏ธ Vote with thisgame++ or thisgame-- during games! ``` ### Implementation Example ```javascript ws.on('message', (data) => { const msg = JSON.parse(data.toString()); if (msg.type === 'session.started') { const { id, notes, created_at } = msg.data.session; // Announce to IRC/Discord/etc bot.announce(`๐ŸŽฎ Game Night has started! Session #${id}`); if (notes) { bot.announce(`๐Ÿ“ ${notes}`); } bot.announce('๐Ÿ—ณ๏ธ Vote with thisgame++ or thisgame-- during games!'); // Auto-subscribe to this session ws.send(JSON.stringify({ type: 'subscribe', sessionId: id })); } }); ``` ## ๐Ÿ” Debugging ### Check WebSocket Logs The backend logs WebSocket events: ``` [Sessions] Broadcasted session.started event for session 18 [WebSocket] Broadcasted session.started to 1 client(s) for session 18 ``` ### Common Issues 1. **Event not received:** - Verify the client is authenticated (`auth_success` received) - Check backend logs for broadcast confirmation - **No subscription required** - All authenticated clients automatically receive `session.started` events - Make sure your WebSocket connection is open and authenticated 2. **Missing session data:** - Check if the session was created successfully - Verify the API response includes all fields 3. **Duplicate events:** - Normal if multiple clients are connected - Each client receives its own copy of the event ## ๐Ÿ“š Related Documentation - [Session End WebSocket Event](SESSION_END_WEBSOCKET.md) - [WebSocket Testing Guide](WEBSOCKET_TESTING.md) - [Bot Integration Guide](BOT_INTEGRATION.md) - [API Quick Reference](API_QUICK_REFERENCE.md) ## ๐Ÿ”— Session Lifecycle Events ``` session.started โ†“ game.added (multiple times) โ†“ vote.received (during each game) โ†“ session.ended ``` ## ๐Ÿ“ Notes - The `session.started` event is broadcast to **all authenticated clients** (not just subscribed ones) - **No subscription required** - All authenticated clients automatically receive this event - Clients should auto-subscribe to the new session to receive subsequent `game.added` and `vote.received` events - The `is_active` field will always be `1` for new sessions - The `notes` field may be `null` if no notes were provided - The timestamp is in ISO 8601 format with timezone (UTC) ## ๐Ÿ’ก Use Cases 1. **Bot Announcements** - Notify users when game night starts 2. **Auto-Subscription** - Automatically subscribe to new sessions 3. **Session Tracking** - Track all sessions in external systems 4. **Analytics** - Log session creation times and frequency 5. **Notifications** - Send push notifications to users ## ๐ŸŽฏ Best Practices 1. **Auto-subscribe** to new sessions when you receive `session.started` 2. **Store the session ID** for later reference 3. **Handle reconnections** gracefully (you might miss the event) 4. **Use polling as fallback** to detect sessions created while disconnected 5. **Validate session data** before processing ## ๐Ÿ”„ Complete Event Flow Example ```javascript const WebSocket = require('ws'); const ws = new WebSocket('ws://localhost:5000/api/sessions/live'); let currentSessionId = null; ws.on('message', (data) => { const msg = JSON.parse(data.toString()); switch (msg.type) { case 'session.started': currentSessionId = msg.data.session.id; console.log(`๐ŸŽฎ Session ${currentSessionId} started!`); // Auto-subscribe ws.send(JSON.stringify({ type: 'subscribe', sessionId: currentSessionId })); break; case 'game.added': console.log(`๐ŸŽฒ New game: ${msg.data.game.title}`); break; case 'vote.received': console.log(`๐Ÿ—ณ๏ธ Vote: ${msg.data.vote.type}`); break; case 'session.ended': console.log(`๐ŸŒ™ Session ${msg.data.session.id} ended!`); console.log(`๐Ÿ“Š Games played: ${msg.data.session.games_played}`); currentSessionId = null; break; } }); ``` ## โœจ Conclusion The `session.started` WebSocket event provides instant notification when new game sessions are created, allowing bots and integrations to react immediately and provide a seamless user experience.