Skip to main content

Connection

APM runs a WebSocket server on your local machine that accepts connections from web applications.

Endpoint

ws://localhost:7000/websocket/

Connection Example

const ws = new WebSocket('ws://localhost:7000/websocket/');

ws.onopen = () => {
  console.log('Connected to APM');
};

ws.onmessage = (event) => {
  const data = JSON.parse(event.data);
  console.log('Received:', data);
};

ws.onerror = (error) => {
  console.error('WebSocket error:', error);
};

ws.onclose = () => {
  console.log('Disconnected from APM');
};

Message Types

APM WebSocket API supports different message types identified by the message structure:

Client to Server Messages

Message TypeDescriptionReference
Print Job RequestSend a print job to a thermal printerPrint Jobs
Template UpdateUpdate or create a print templateTemplate Updates
Scale ControlStart/stop listening to scale dataScale Data

Server to Client Messages

Message TypeDescriptionReference
Print Job ResultResult of a print job operationPrint Jobs
Scale ReadingReal-time weight data from scaleScale Data
Template Update ResultResult of template update operationTemplate Updates

Status Endpoint

Check if APM is running and get connected client information:
curl http://localhost:7000/websocket/status

Response

{
  "IsRunning": true,
  "ConnectedClients": 2,
  "ScaleStatuses": [
    {
      "ScaleId": "scale_001",
      "IsConnected": true,
      "LastReading": "2024-01-15T10:30:00Z"
    }
  ]
}

Error Handling

All responses include status information. For errors:
{
  "JobId": "job_12345",
  "Status": "ERROR",
  "ErrorMessage": "Printer not found: PRINTER_001"
}
Common error scenarios:
  • Printer not found: The specified printer ID doesn’t exist in the configuration
  • Template not found: The document type doesn’t have a corresponding template
  • Connection failed: Unable to connect to the printer hardware
  • Invalid JSON: Malformed message sent to the server

Client Identification

Each WebSocket connection is automatically assigned a client ID based on the remote endpoint (IP:Port). The server uses this ID to:
  • Send responses to the specific client that made the request
  • Track scale data subscriptions per client
  • Manage connection lifecycle
Print job results and template update results are sent only to the requesting client (unicast), while scale data can be broadcast to all subscribed clients.

Build docs developers (and LLMs) love