Repository navigation
RaftStatePublisherSysMod
The StatePublisher module is responsible for handling the publication of state information from other SysMods. It supports subscriptions, dynamic updates, and efficient data transfer by using a hashing mechanism to detect changes in state that might trigger publishing. The module is highly configurable via JSON, allowing for flexible publication settings.
Any SysMod can be a publisher of state information.
Publication sources are created dynamically when SysMods register as data sources via the registerDataSource API. There is no need to pre-configure publication topics - they are automatically added when a SysMod registers with its unique PubTopic. Each publication source must have a unique topic name.
A SysMod with data to publish must register with the StatePublisher, specifying the PubTopic under which it wants to publish its data, along with message generation and state detection callbacks.
For further information see Registering a Data Source for Publishing
The StatePublisher is configured using minimal JSON settings. Publication sources are now created automatically when SysMods register as data sources - there is no need to pre-configure publication topics. Publishing requires explicit subscription via the REST API for ALL interfaces (including BLE and serial). Subscriptions specify publishing rate, trigger type, and minimum time between messages.
The StatePublisher configuration is minimal - publication topics are added automatically when SysMods register:
"Publish": {
"enable": 1
}Note: When a SysMod calls registerDataSource with a topic name, the publication source is automatically created. Clients must explicitly subscribe via the REST API to receive data on any interface (including BLE, WebSocket, and serial).
Once the state change is detected, the system calls the msgGenCB to generate the actual data to be published. This message is then sent through the appropriate communication channel.
Here’s the flow for publishing data:
- Check Interface: Before publishing, the system checks whether the specified communication channel is available for sending data.
-
Generate Message: The
msgGenCBis invoked to generate the message content. -
Send Data: The message is sent using the
CommsCoreIF::outboundHandleMsgfunction.
Subscriptions are created and updated through the /subscription REST API endpoint. This allows clients to dynamically subscribe to specific messages based on the publication name, interface, and rate. All publishing now requires explicit subscription - there are no pre-configured automatic publications.
-
Subscription Request: The client sends a request to the
/subscriptionendpoint with the details of the publications they want to subscribe to. The request contains:-
action:updateupdates the subscription to each topic inpubRecs, or creates it if there isn't one (arateHzof 0 removes it).unsubscriberemoves the subscription totopic. -
pubRecs: The publication records to subscribe to, including the topic, rate, and optional trigger and minimum time between messages. A single record can be given instead astopic,rateHzandminMsparameters (e.g.subscription?action=update&topic=Camera&rateHz=2).
-
Subscriptions are specific to the communication channel on which the request is made, and last while the client is connected:
- A client's subscription is removed when a publish finds no connection on its channel: a WebSocket connection slot with no client, or BLE with no central connected. A WebSocket channel belongs to a connection slot, so otherwise the next client in the slot would get the publishes without subscribing. Clients subscribe when they connect (raftjs does).
- An MQTT channel reports no connection while the broker isn't connected (and an inbound-topic channel always does), so a client's subscription on it is removed by a publish made while MQTT is disconnected.
- Subscriptions created by a SysMod or config with
createSubscription(e.g. the MQTTManagerpubSourcesconfig) are kept while their channel has no connection; they are retried at their normal interval and publish once it connects. (RaftSysMods versions after 1.29.0; earlier they were removed like a client's.) - Channels that don't report connections (serial) keep their subscriptions.
- (In RaftSysMods 1.27 and earlier, a subscription was kept when its connection had gone, and a
rateHzof 0 kept it with a rate of 0, which published as fast asminMsallowed.)
Example Subscription Request:
{
"action": "update",
"pubRecs": [
{
"topic": "devjson",
"rateHz": 1.0,
"trigger": "timeorchange",
"minMs": 100
}
]
}Subscription Parameters:
-
topic(required): Topic name to subscribe to (must match a topic registered by a SysMod viaregisterDataSource) -
rateHz(required): Publishing rate in Hz, or 0 to remove the subscription -
trigger(optional): Override the default trigger type ("change", "time", "timeorchange") -
minMs(optional): Minimum interval between messages in milliseconds
Note: For information about the devjson and devbin topics automatically published by DeviceManager, see Device Data Publishing.
When there is no connection on a subscription's channel, a client's subscription is removed and one created with createSubscription is kept (see Subscription Mechanism). Other publish failures (e.g. a message that couldn't be sent) back off progressively, to reduce overhead:
- ACTIVE: Publishing normally
- LOST: Failing, retried at the normal interval
- BACKOFF_STAGE_1: After 3 consecutive failures, the interval is twice as long
- BACKOFF_STAGE_2: After 10, 10 times as long
- BACKOFF_STAGE_3: After 20, 20 times as long
A successful publish restores the normal rate.
Getting Started
- Quick Start
- Architecture at a Glance
- Writing Your First SysMod
- Adding a Comms Channel
- Adding an I2C Device Type
Scaffolding & Building
- Raft CLI
- Web Apps (Device Types, Flasher)
- SysTypes
- Top-Level SysType
- Build Process
- WebUI Build Pipeline
- File System
- Partitions & Flash
- Local Dev Libraries
- Library Developer Guide
Architecture
Built-in SysMods
- NetworkManager
- WiFi Troubleshooting
- BLEManager
- WebServer
- MQTTManager
- SerialConsole
- CommandSerial
- CommandSocket
- CommandFile
- FileManager
- LogManager
- ESPOTAUpdate
- StatePublisher
- Remote Logging
- Data Source Registration
Comms & Protocols
- Stack Overview
- Comms Channels
- ProtocolExchange
- RICREST Protocol
- Real-Time Streams
- Adding REST Endpoints
- Built-in REST Endpoints
- File Download (OKTO)
- OTA Update Flow
Devices & Buses
- DeviceManager
- Device Manager REST API
- Device Factory & Classes
- Device Type Records
- Adding an I2C Device Type
- Device Data Publishing
- Data Logger
- I2C Bus
- I2C Device Scanning
- I2C ID & Polling
- MotorControl Overview
- MotorControl Config
- MotorControl Commands
- Camera
ROS 2
Helpers
Reference