Overview
This guide covers how to establish a WebSocket connection, authenticate your POS client, and maintain a healthy connection with proper heartbeat and reconnection handling.Prerequisites
Before connecting, ensure you have:API Credentials
API key provided by Modulus Labs for your POS integration
WebSocket Client
A WebSocket client library for your programming language
Network Access
Outbound access to the WebSocket endpoint over port 443
JSON Parser
Ability to serialize and parse JSON messages
WebSocket Endpoint
Connect to the WebSocket endpoint:Replace
{your-api-endpoint} with your provisioned API endpoint. Contact support@moduluslabs.io to obtain your endpoint URL and credentials.Authentication
WebSocket connections authenticate during the connection handshake. Provide your API key via headers or query parameters.Authentication via Headers
Include thex-api-key header in your WebSocket connection request:
| Header | Required | Description |
|---|---|---|
x-api-key | Yes | Your API key |
x-group-id | No | Group identifier (optional) |
Authentication via Query Parameter
Alternatively, pass your API key as a query parameter:Establishing a Connection
Quick Start Flow
Heartbeat / Keep-Alive
Maintain connection health with periodic ping/pong messages to prevent timeouts and detect stale connections.Recommended Settings
| Setting | Value |
|---|---|
| Ping interval | 30 seconds |
| Pong timeout | 10 seconds |
| Reconnect on failure | Yes |
Implementation
Testing Your Connection
Quick Connection Test
Troubleshooting
Connection Rejected
Connection Rejected
Possible causes:
- Invalid or missing API key
- API key not authorized for WebSocket access
- Network firewall blocking WebSocket connections
- Verify your API key is correct
- Contact support@moduluslabs.io to verify API key permissions
- Check that outbound WebSocket connections are allowed on port 443
Connection Drops Frequently
Connection Drops Frequently
Possible causes:
- Heartbeat not implemented
- Network instability
- Firewall or proxy terminating idle connections
- Implement heartbeat with 30-second ping interval
- Check network connectivity
- Configure proxy/firewall to allow persistent WebSocket connections
forceDisconnect Messages
forceDisconnect Messages
Possible causes:
- Multiple POS instances using the same API key
- Previous connection not properly closed
- Ensure only one POS instance uses each API key
- Request additional API keys for multiple terminals
- Implement proper connection cleanup on application exit
Messages Not Received
Messages Not Received
Possible causes:
- Message handler not processing all action types
- JSON parsing errors
- Connection silently dropped
- Log all incoming messages for debugging
- Add try/catch around JSON parsing
- Implement heartbeat to detect stale connections
Security Best Practices
Protect API Keys
Store API keys in environment variables, not in source code. Never expose keys in client-side JavaScript.
Use Headers
Prefer header-based authentication over query parameters to avoid keys appearing in logs.
Validate Messages
Always validate incoming message structure before processing to prevent errors from malformed data.
Secure Reconnection
When reconnecting, ensure you’re connecting to the legitimate endpoint. Validate SSL certificates.