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.1
Authenticate
Connect with your API key via headers or query parameters
2
Establish Connection
Open WebSocket connection to the API endpoint
3
Implement Heartbeat
Send periodic ping messages to maintain connection health
4
Handle Messages
Process responses and push notifications
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:
Authentication via Query Parameter
Alternatively, pass your API key as a query parameter:Establishing a Connection
Quickstart Flow
1
Connect to WebSocket Endpoint
Establish a WebSocket connection with your API key:
2
Discover Available Terminals
Request a list of connected terminals:Receive terminal list:
3
Initiate Payment
Send a payment request to a terminal:
4
Receive Payment Result
The terminal processes the payment and returns the result via push notification:
Heartbeat / Keep-Alive
Maintain connection health with periodic ping/pong messages to prevent timeouts and detect stale connections.Recommended Settings
Implementation
Testing Your Connection
1
Connect to the WebSocket endpoint
Verify successful connection by checking for the
open event2
Send getTerminals action
Verify you receive a
terminalsResponse with available terminals3
Test heartbeat
Send a
ping and verify you receive a pong response4
Test reconnection
Simulate a connection drop and verify automatic reconnection works
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.
Next Steps
Actions Reference
Learn how to send commands to terminals
Push Notifications
Handle real-time terminal and payment updates
Core Concepts
Device enforcement and reconnection resilience
Data Types
Complete schema reference for all data types