Skip to content

NATS Messaging

Services talk to the ToloMEO Edge agent over NATS, using fixed subjects and SenML records.

NATS Fits an Edge Device

NATS is a lightweight publish/subscribe broker. On a device with limited resources it offers:

  • Low overhead — the broker runs in a few megabytes of RAM
  • No per-message storage — no queue persistence needed for real-time sensor data
  • Subject-based routing — services subscribe to specific topic patterns
  • Reply correlation — a reply can carry the request's reply-to value

Py ToloMEO uses the nats-py async client, wrapped by NATSMessageStrategy.

Subjects Start with Their Category

Subjects addressed to one service carry {service} as their second segment; broadcast event subjects do not:

Subject Category Owner Description
commands.{service}.req commands Service Inbound commands from operator
events.data events Service Outbound sensor readings
events.params events Service Outbound command replies and announcements
events.infos events Service Outbound OTA status and info events
heartbeat.{service}.service heartbeat Service Liveness pulse, every heartbeat_interval_ms (10 s)

Records Follow SenML

SenML (RFC 8428) is a standard format for IoT measurements. Py ToloMEO uses a SenML-based structure because it is:

  • Standardized — well-understood in the IoT ecosystem
  • Compact — short field names (bn, n, v, u, t)
  • Extensible — the vs string-value field carries JSON command payloads without breaking the SenML schema

vs holds a JSON string, not a nested object. The outer record stays valid SenML, and a new command needs no change to the envelope.

Services Announce Their Commands and Plugins

A frontend that builds a form for a command, or lists a service's plugins, should not have to guess or poll. Every NATSService announces these records on events.params:

Record (n) Content Sent
commands A JSON schema for every service command and every plugin command Once, at the end of service setup
plugins The plugin class name and the ids of the attached plugins At startup, and after every plugin attach and detach
metrics The output metrics of every attached plugin (sensor services) After every plugins record, when a plugin is attached

Command definitions depend only on code, so they are sent once: a BLE service with no device attached still announces its plugin commands. The plugin list changes at runtime, so attach_plugin and detach_plugin re-send it. Changes close together are merged, and the last record always matches the registry, including ids: [] once the last plugin detaches.

The same data is available on demand: ListServiceCommands and ListPluginCommands return a schemas key, and ListPluginIds returns the ids of the plugins record. See SenML Format for the record layouts.

Payloads Are Validated Before a Command Runs

A command can declare its payload as a Pydantic model, which the dispatcher checks before the command runs. The announced schema comes from the same model, so the form a frontend renders and the check the service applies cannot drift apart.

  • A command without a model is not validated and is announced with an empty object schema.
  • Models ignore unknown fields, so a client that sends extra keys keeps working.
  • The raw payload is still available to the command as context.payload; the validated model is context.params.
  • A payload that is not a JSON object is rejected for commands that declare a model and for commands routed to a plugin.

An invalid payload is logged without a stack trace and answered with an error record naming each offending field:

[{"bn": "urn:cpt:device:sn:abc123:", "n": "error",
  "vs": "Invalid payload for 'BLEDiscover': address: Field required"}]

Commands that report errors in their own reply, such as the OTA commands, answer an invalid payload under the command's name:

[{"bn": "urn:cpt:device:sn:abc123:", "n": "NotifyOTAUpdate",
  "vs": "{\"id\": \"swupdate\", \"result\": \"error\", \"status\": \"updated\", \"error\": \"Invalid payload for 'NotifyOTAUpdate': fw_version: Field required\"}"}]

See Build Your First Service for how to declare a payload model.

Replies Always Go to events.params

A command's reply is always published on events.params. When the request carries a NATS reply-to value, the reply carries the same value in its Reply-To field, so a client can match a reply to its request. The reply-to value is not used as a destination.

See also