Overview
The WebSocket notification endpoint enables real-time, bidirectional communication between the client and server for receiving notifications about product expiry and other events. Once connected, the server can push notifications to the client without requiring polling.WebSocket connections require authentication via the
access_token query parameter. The connection will be automatically closed if authentication fails.Connection URL
Authentication
JWT access token for authenticating the WebSocket connection. Pass this as a query parameter in the connection URL.
Connection Flow
1. Establishing Connection
When you connect to the WebSocket, you’ll receive a confirmation message:Message type:
CONNECTION_ESTABLISHEDConfirmation message: “Notification WebSocket connected”
The authenticated user’s ID extracted from the access token
2. Receiving Notifications
Once connected, the server will push notifications to the client in real-time. Notifications follow this structure:Unique notification ID
Human-readable notification message
Notification type:
info, warning, or errorName of the product related to the notification
ISO 8601 timestamp when the notification was created
3. Client-to-Server Messages
Clients can send messages to the server for various actions:Mark Notification as Read
Ping (Keep-Alive)
Send the string"PING" to receive a pong response:
Response:
Regular ping messages can help keep the connection alive and detect disconnections.
Error Handling
Authentication Failure
If no access token is provided, the connection will be closed immediately with:- Code: 1008 (Policy Violation)
- Reason: “Access token required”
Disconnection
The WebSocket connection may be closed due to:- Network issues
- Server shutdown
- Client disconnection
- Authentication token expiration
Example Usage
JavaScript/TypeScript
Python
Best Practices
- Reconnection Logic: Implement automatic reconnection with exponential backoff for production applications
- Keep-Alive: Send periodic PING messages to maintain the connection
- Error Handling: Always handle connection errors and unexpected disconnections gracefully
- Token Refresh: Monitor token expiration and reconnect with a fresh token when needed
- Message Acknowledgment: Mark notifications as read to prevent duplicate processing