# Socket Broker A WebSocket broker application that handles topic-based messaging between frontend clients and backend services with API key authentication. ## Features - **WebSocket Server** (Port 3000): Handles client connections and topic subscriptions - **HTTP API** (Port 8080): Allows backend services to publish messages to topics - **API Key Authentication**: Secure access control for publishing messages - **Topic-based messaging**: Clients subscribe to topics and receive messages when published - **Real-time communication**: Instant message delivery to all subscribed clients - **Health monitoring**: Built-in health checks and status endpoints ## Architecture ``` Frontend Clients ←→ WebSocket Server (Port 3000) ↕ Backend Services ←→ HTTP API (Port 8080) [🔐 API Key Required] ``` ## Security **⚠️ IMPORTANT**: The HTTP API endpoints that publish messages require a valid API key for authentication. This ensures that only authorized backend services can send messages to your WebSocket clients. ### API Key Configuration Set the `API_KEY` environment variable to secure your application: ```bash # Set a strong, random API key export API_KEY="your-secure-api-key-here" # Or use in docker-compose API_KEY=your-secure-api-key-here docker-compose up ``` ### Authentication Methods The API accepts the API key in three ways: 1. **Authorization Header** (Recommended): ```bash Authorization: Bearer your-api-key-here ``` 2. **X-API-Key Header**: ```bash X-API-Key: your-api-key-here ``` 3. **Query Parameter**: ```bash POST /publish?api_key=your-api-key-here ``` ## Quick Start ### Using Docker ```bash # Build the image docker build -t socket-broker . # Run the container with API key docker run -d \ --name socket-broker \ -p 3000:3000 \ -p 8080:8080 \ -e API_KEY="your-secure-api-key" \ socket-broker ``` ### Using Docker Compose ```bash # Copy environment file cp env.example .env # Edit .env and set your API key nano .env # Start the service docker-compose up -d ``` ### Using Node.js directly ```bash # Set API key export API_KEY="your-secure-api-key" # Install dependencies npm install # Start the application npm start ``` ## Environment Variables | Variable | Default | Description | | ---------- | --------------------------- | ---------------------------------------- | | `WS_PORT` | `3000` | WebSocket server port | | `API_PORT` | `8080` | HTTP API server port | | `API_KEY` | `default-api-key-change-me` | **REQUIRED**: API key for authentication | ## WebSocket API (Port 3000) ### Connection Connect to `ws://localhost:3000` using Socket.IO client. ### Events #### Subscribe to a topic ```javascript socket.emit("subscribe", "user-notifications"); socket.on("subscribed", (data) => { console.log(`Subscribed to ${data.topic}`); }); ``` #### Unsubscribe from a topic ```javascript socket.emit("unsubscribe", "user-notifications"); socket.on("unsubscribed", (data) => { console.log(`Unsubscribed from ${data.topic}`); }); ``` #### Receive messages ```javascript socket.on("message", (data) => { console.log(`Message from topic ${data.topic}:`, data.message); console.log("Data:", data.data); console.log("Timestamp:", data.timestamp); }); ``` #### Error handling ```javascript socket.on("error", (error) => { console.error("Socket error:", error.message); }); ``` ## HTTP API (Port 8080) ### Public Endpoints (No Authentication Required) #### Health Check ```bash GET /health ``` Response: ```json { "status": "healthy", "connections": 5, "topics": 3, "timestamp": "2024-01-01T12:00:00.000Z", "apiKeyConfigured": true } ``` #### List Topics ```bash GET /topics ``` Response: ```json { "topics": [ { "topic": "user-notifications", "subscribers": 2 }, { "topic": "system-alerts", "subscribers": 1 } ] } ``` #### Get Topic Info ```bash GET /topics/{topic} ``` Response: ```json { "topic": "user-notifications", "subscribers": 2 } ``` ### Protected Endpoints (API Key Required) #### Response Codes - **200 OK**: Message published successfully to subscribers - **204 No Content**: Message published successfully but no subscribers for the topic - **400 Bad Request**: Invalid request (missing topic, message, or data) - **401 Unauthorized**: Invalid or missing API key - **500 Internal Server Error**: Server error #### Publish Message ```bash POST /publish Authorization: Bearer your-api-key-here Content-Type: application/json { "topic": "user-notifications", "message": "New message received", "data": { "userId": 123, "type": "email" } } ``` Alternative endpoint: ```bash POST /publish/{topic} Authorization: Bearer your-api-key-here Content-Type: application/json { "message": "New message received", "data": { "userId": 123, "type": "email" } } ``` Response: ```json { "success": true, "message": "Message published to topic 'user-notifications'", "subscribers": 2, "payload": { "topic": "user-notifications", "message": "New message received", "data": { "userId": 123, "type": "email" }, "timestamp": "2024-01-01T12:00:00.000Z" } } ``` **Note**: If there are no subscribers for the topic, the API returns `204 No Content` instead of an error. This indicates that the message was published successfully, but there were no recipients to receive it. ### Authentication Errors If you don't provide a valid API key, you'll get: ```json { "error": "Unauthorized", "message": "API key is required. Use Authorization header or x-api-key header or api_key query parameter." } ``` ## Frontend Example (JavaScript) ```html