Skip to content

Configure the NATS Connection

This guide shows how to configure the NATS connection for a Py ToloMEO service.

How Services Resolve Configuration

Services read configuration through the Settings dataclass, populated via the inner Meta class. The NATS connection itself is managed by NATSClientManager, which is instantiated by NATSMessageStrategy.

Environment Variables

Variable Default Description
DEVICE_SERIAL_NUMBER 1122334455667788 Device identifier used in the SenML base name

Set this before starting the service:

DEVICE_SERIAL_NUMBER=abc123 python -m my_service.main

Configure the NATS Server URL

The default NATS server URL is nats://localhost:4222. To connect to a different server, subclass NATSClientManager and override connect:

import nats
from tolomeo.messaging.client import NATSClientManager


class CustomNATSClientManager(NATSClientManager):
    async def connect(self) -> None:
        self.client = await nats.connect(
            servers="nats://my-server:4222",
            user="alice",
            password="secret",
        )

Then pass your manager to a custom strategy and wire it into the service:

from tolomeo.messaging import NATSMessageStrategy
from tolomeo.services import NATSService


class CustomStrategy(NATSMessageStrategy):
    def __init__(self, logger=None):
        super().__init__(
            logger=logger,
            client_manager=CustomNATSClientManager(logger),
        )


class MyService(NATSService):
    class Meta:
        plugin_class = MyPlugin
        strategy_class = CustomStrategy

Publishing Cadence

Note

Py ToloMEO publishes sensor data as soon as a plugin pushes it via push_reading — the service reacts to data as it arrives. There is no polling interval to configure.