A real-time notification server that provides secure WebSocket-based pub/sub functionality with HTTP-based access control.
-
Secure Client Management
- Client ID generation and validation
- Client metadata support
- Automatic client ID expiration
-
Channel Management
- Channel creation with access control rules
- Channel access control (grant/revoke)
- Maximum subscriber limits
- Client pattern-based access rules
-
WebSocket Communication
- Real-time notifications
- WebSocket-only subscription model
- Connection status tracking
- Automatic reconnection support
-
Access Control
- HTTP-based access management
- Channel-level access control
- Client-specific permissions
- Access pattern validation
The system consists of two main components:
-
HTTP API (
openapi.yaml
)- Client management
- Channel creation and access control
- Notification publishing
- History retrieval
-
WebSocket API (
asyncapi.yaml
)- Real-time notifications
- Subscription management
- Connection handling
- Error reporting
- Node.js (v16 or later)
- Redis (v6 or later)
- npm or yarn
-
Clone the repository:
git clone <repository-url> cd notification-server
-
Install dependencies:
npm install
-
Configure environment variables:
cp .env.example .env # Edit .env with your configuration
-
Start Redis:
redis-server
-
Start the server:
npm start
The HTTP API is documented in openapi.yaml
and provides the following endpoints:
-
Client Management
POST /api/clients
- Generate a new client IDGET /api/clients/{clientId}
- Validate a client IDDELETE /api/clients/{clientId}
- Delete a client and all its data
-
Channel Management
POST /api/channels
- Create a new channelPOST /api/channels/{channel}/access/{clientId}
- Grant channel accessDELETE /api/channels/{channel}/access/{clientId}
- Revoke channel access
-
Notification Management
POST /api/notifications
- Publish a notificationGET /api/notifications/{channel}
- Get notification history
The WebSocket API is documented in asyncapi.yaml
and supports the following message types:
-
Connection Messages
{ "type": "connection", "clientId": "client123", "metadata": { "userAgent": "Demo Client", "environment": "development" } }
-
Subscription Messages
{ "type": "subscription", "action": "subscribe", "channel": "channel1" }
-
Notification Messages
{ "type": "notification", "data": { "channel": "channel1", "message": "Hello, world!", "timestamp": "2024-03-20T12:00:00Z", "metadata": { "priority": "high", "tags": ["important"] } } }
- Client IDs are required for WebSocket connections
- Channel access is controlled via HTTP API
- Client IDs expire after a configurable period
- Channel access can be revoked at any time
- Access patterns can be restricted using regex patterns
-
Generate a client ID:
curl -X POST http://localhost:3111/api/clients \ -H "Content-Type: application/json" \ -d '{"metadata": {"userAgent": "Demo Client"}}'
-
Create a channel:
curl -X POST http://localhost:3111/api/channels \ -H "Content-Type: application/json" \ -d '{ "channel": "demo", "rules": { "maxSubscribers": 100, "allowedClients": ["client123"], "allowedPatterns": ["client.*"] } }'
-
Connect via WebSocket:
const ws = new WebSocket('ws://localhost:8080?clientId=client123'); ws.onmessage = (event) => { const data = JSON.parse(event.data); if (data.type === 'notification') { console.log('Received notification:', data.data); } }; // Subscribe to a channel ws.send(JSON.stringify({ type: 'subscription', action: 'subscribe', channel: 'demo' }));
npm test
npm run lint
npm run build
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
MIT License - see LICENSE file for details
curl -X POST http://localhost:3000/api/channels \
-H "Content-Type: application/json" \
-d '{
"channel": "my-channel",
"rules": {
"isPublic": true,
"allowedClientIds": ["client1", "client2"],
"maxSubscribers": 100
}
}'
curl -X DELETE http://localhost:3000/api/channels/my-channel
- Channel creation will fail with a 409 status if the channel already exists
- Channel deletion will fail with a 404 status if the channel doesn't exist
- Both operations require a valid channel name
# Generate a new client ID
curl -X POST http://localhost:3000/api/clients \
-H "Content-Type: application/json" \
-d '{"metadata": {"userAgent": "Demo Client"}}'
# Delete a client and all its data
curl -X DELETE http://localhost:3000/api/clients/client123