Configure a Service¶
This guide covers pointing a service at a NATS broker, with credentials and TLS, from the configuration file.
Prerequisites¶
- A Py ToloMEO service (see Build Your First Service)
- A NATS broker the device can reach
Change the Broker URL¶
Create tolomeo.toml next to your service; the service reads it once at startup, and
Configuration File lists the other places it looks:
Add Credentials¶
Choose exactly one authentication style. Two at once is a configuration error.
Enable TLS¶
Point at a CA bundle and, optionally, the hostname the certificate must present:
[nats]
url = "tls://my-server:4222"
tls_ca_file = "/etc/nats/ca.pem"
tls_hostname = "broker.example"
An unreadable or invalid CA file stops the service at startup, not at the first connection.
Tune Reconnection¶
[nats]
reconnect_time_wait_s = 2 # seconds between attempts; the default
max_reconnect_attempts = 10 # omit to keep the client library's default of 60
Apart from url and reconnect_time_wait_s, a key you leave out is not passed to the client library at all, so the
library's own default applies.
Set the Heartbeat Cadence¶
Pass Options to a Plugin¶
Each plugin reads its own table, named after its plugin id:
The plugin declares the valid keys as a Pydantic model; see Implement a Custom Plugin. A mistyped key fails when the plugin attaches and names the key.
The example services each ship a commented .toml (temperature.toml, ota_mock.toml, ble_mock.toml) showing
every table they read.
Set the Environment¶
| Variable | Default | Description |
|---|---|---|
TOLOMEO_CONFIG |
— | Path to the configuration file; when unset, the standard search paths apply |
DEVICE_SERIAL_NUMBER |
1122334455667788 |
Device identifier used in the SenML base name |
CONFIG_FILENAME |
.devices.yml |
Name of the BLE device list read by BLESensorService |
One process runs one service with one configuration.
Customize the Connection in Code¶
For what the file cannot express (a custom connect sequence, a value computed at runtime), subclass the client manager. Arguments passed explicitly take precedence over the file:
import logging
import os
from tolomeo.messaging import NATSMessageStrategy
from tolomeo.messaging.client import NATSClientManager
from tolomeo.services import NATSService
from .plugin import MyPlugin
class CustomNATSClientManager(NATSClientManager):
def __init__(self, logger: logging.Logger):
super().__init__(logger, url=os.environ["BROKER_URL"])
class CustomStrategy(NATSMessageStrategy):
def __init__(self, logger: logging.Logger | None = None):
logger = logger or logging.getLogger(__name__)
super().__init__(logger=logger, client_manager=CustomNATSClientManager(logger))
class MyService(NATSService):
class Meta:
plugin_class = MyPlugin
strategy_class = CustomStrategy
To change the connect sequence itself, override NATSClientManager.connect instead.
See also
- Configuration File — every key, the search paths and the validation rules
- NATS Messaging — why Py ToloMEO uses NATS and how its subjects are laid out
- SenML Format — the records the service publishes over the connection