MQTT
Connect can serve clocks that talk to an MQTT broker instead of over HTTP. As over HTTP, a punch is stored before the clock is told it was received. This page covers when a message is acknowledged or held, what that asks of your broker, and how to set up per-device credentials and topic ACLs.
An MQTT listener connects out to a broker. It subscribes to each of its endpoints’ device topics. It takes in what the clocks publish exactly as it takes in HTTP. It publishes replies and commands on each device’s reply topic.
Several Hour Advantage instances on one broker share the work through one shared subscription per listener ($share/houradvantage-<listener>/<topic>), so each message goes to one instance. Setting up listeners and endpoints is in networking.
When a message is acknowledged
Section titled “When a message is acknowledged”Over HTTP a clock is told OK only once what it sent is stored. It sends again whatever it was not told OK for.
Over MQTT the listener acknowledges a message (PUBACK) once it is taken in, or once it is set aside. Set aside means kept in the database, sealed, to be taken in later.
A message is set aside when what it waits for may take long. Examples are a person allowing a clock, a package put right, or a tenant resumed. Held unacknowledged, it would hold up every message after it on the listener’s connection, other tenants’ clocks too.
A message that waits only a moment (over a rate, or not stored) is held unacknowledged and tried again. The first retry is after half a second. Each wait after that is twice as long, up to a minute. Each attempt goes to the endpoint the topic routes to at that moment, so a setup change counts at the next attempt.
Delivery is at least once. A message the broker sends again is taken in again. This happens when the connection was lost before its acknowledgement was heard.
A message taken in twice reaches sinks twice with the same event ids. A sink keeps the first event of an id, as Time and Attendance does. So a punch taken in twice is one punch.
| What became of the message | HTTP | MQTT |
|---|---|---|
| Taken in, events stored | 200 | Acknowledged. Then the reply and any queued commands are pushed. |
| Events could not be stored | 503 | Held, tried again. |
| Device over its rate, tenant over its rate | 503 with Retry-After, or 429 | Held, tried again. |
| Endpoint stopped, tenant suspended or stopped | 503 | Set aside, taken in once the endpoint is started or the tenant resumed. |
Device waiting to be allowed (new_devices = "hold") | 403 | Set aside, taken in once the device is allowed. |
| Device not registered, endpoint refuses new devices | 403 | Set aside, taken in once the device is registered. |
| Device did not present its key | 401, refusal recorded on the device | Set aside. The refusal is recorded on the device, as over HTTP. Taken in once the device is given its key. |
| Too many devices waiting, tenant at its device limit | 429 | Set aside, tried again. |
| Package could not identify the device | 400 | Set aside, tried again. |
| Package failed on the message, or has no route for it | 500 or 404, kept as a device failure | Set aside, tried again, each attempt kept as a device failure. |
| Device blocked | 403 | Acknowledged and not taken in. |
| No endpoint takes the topic | (no such path: 404) | Acknowledged and not taken in. |
Body over limits.max_body_bytes | 413 | Acknowledged and refused as 413 (oversized messages). |
A package that delivers by cursor or none (delivery in its manifest) tells its device nothing went wrong when its events could not be stored. The device will not send them again. A cursor package fetches them again, since its cursors are not moved. A none one moves on.
Those events are not in the log’s events count. The message’s record in the traffic log has them as not_stored. The log line says the message was answered but its events could not be stored. houradvantage_events_not_stored_total{endpoint} counts them.
Messages set aside
Section titled “Messages set aside”A message set aside is kept with its device, the time it came, and why it waits. When the package could not tell which device sent it, it is kept with its endpoint instead. What it carried is sealed with the install’s key (HOURADVANTAGE_SECRET_KEY), because it may hold a person’s biometrics.
No API returns it. Device failures record what went wrong and where in the package, never what the message carried.
The traffic log keeps the start of each message’s body as it came, set aside or not, up to limits.traffic_body_bytes. Set it to 0 to keep none.
It is taken in through the same steps as any message, as of when it came. Its events, the device’s last sighting and the traffic log say then.
That happens the moment what it waits for changes, on whichever instance makes the change. The change can be any of these: the device allowed, registered or given a key, its tenant resumed, its endpoint started or changed, or its package changed.
Otherwise it is tried again when it is due. One still not taken in waits 30 seconds, then twice as long each time, up to an hour.
A device’s messages set aside are taken in oldest first, one instance at a time. Once the device is let in, messages it sends from then on may be taken in before those still set aside. Punches say their own time.
A message set aside goes when it is taken in, when its device is blocked or removed, and with its tenant.
It is also dropped, without being taken in, in three cases. Its endpoint is gone. No endpoint takes its topic any more. Or it has been kept longer than limits.set_aside_days (default 7). The tenant is then told, once for each device, how many were dropped and why. The event is device.set_aside.expired. Its reason is expired, endpoint_gone, unroutable, device_blocked or device_removed.
They are counted (houradvantage_set_aside_expired_total{tenant}) and logged at warn.
A device that waits to be allowed and has messages set aside is not forgotten for not calling. Pending devices that stop calling are forgotten after 24 hours. Its messages wait for it, up to limits.set_aside_days.
For as long as they are kept, it holds one of its endpoint’s max_pending_devices places and one of its tenant’s pending_devices. A punch is worth more than the place. The limits still bound how many there are.
A tenant may have up to limits.tenants.set_aside messages set aside (default 10000). A tenant’s own limits.set_aside overrides it, and 0 is no limit. See tenant limits.
Past that limit, a message cannot be set aside. It is counted (houradvantage_set_aside_at_cap_total{tenant}) and logged at warn at most once a minute per tenant.
One that no route of its package takes cannot be taken in however long it waits, and is not held. It is acknowledged and kept as an unparsed event of its endpoint. The event has the message as it came (its start and a hash of it if over 16 KiB) and its topic. The tenant and the endpoint’s sinks see it.
Any other message is held unacknowledged instead, and the broker keeps it: a punch is not dropped.
Without HOURADVANTAGE_SECRET_KEY nothing is set aside, because the message would be kept in the clear. Such a message is held unacknowledged instead. It holds up the listener’s other messages after ack_wait_secs (below) until what it waits for changes.
An instance with an MQTT listener and no key warns of it as it starts. Set the key.
A blocked device’s messages are acknowledged, because blocking says “not this one”. They are counted (houradvantage_mqtt_blocked_total{endpoint}) and logged at most once a minute per device, with the number of repeats.
Messages on a topic no endpoint takes are acknowledged. This happens when an endpoint’s topic changed, or the endpoint was removed, while messages for it were on their way. They are counted (houradvantage_mqtt_unroutable_total{listener}) and logged at warn at most once a minute per listener, with how many came.
If the connection to the broker is lost, or the listener stops, what the listener holds stays unacknowledged. The broker sends it again to the listener’s persistent session when it is back.
A message in the middle of being stored when that happens is stopped at its next step. By then it was stored and acknowledged on that connection, or stored and is sent again (its acknowledgement waited for one that came before it), or let go.
Messages are acknowledged in the order they came, as MQTT asks of a receiver (3.1.1 and 5.0, section 4.6). They are handled side by side all the same. A message that is held, or slow, holds back the acknowledgements of those after it, not their handling. Each message’s reply and commands are pushed once it is acknowledged.
A message that has held the acknowledgements back for longer than the listener’s ack_wait_secs (default 30) stops the listener starting new messages until it is taken in. The new messages wait, unacknowledged, and the broker’s in-flight window fills.
Only two kinds of message do that. One is held for a short while (a rate, a database that does not answer). The other could not be set aside (no secret key, the tenant at its cap). One that waits on a person is set aside, and acknowledged.
Oversized messages
Section titled “Oversized messages”A message carrying more than limits.max_body_bytes is acknowledged and refused as HTTP answers 413. It is in the traffic log (status 413, with its size and not what it carried). It is logged as any refused message is, and counted (houradvantage_mqtt_oversized_total{endpoint}), so the clock shows as refused. Nothing of it is taken in.
The listener takes any packet the protocol allows from the broker and holds the limit itself. A client that refused the packet would end the connection, and the broker would give the same packet to the persistent session again on every reconnect.
What this asks of the broker
Section titled “What this asks of the broker”| Requirement | Why |
|---|---|
| An in-flight window for what is not yet acknowledged | Each message not yet acknowledged takes a place in the broker’s window of unacknowledged messages for the listener’s client. With the window full, the broker sends the listener nothing more and queues the rest. Messages that wait on a person are set aside and acknowledged, so what is held is short lived. Mosquitto’s default max_inflight_messages (20) is enough for most. A busier listener (many clocks at once) wants more. The listener states the window it was given (window, required). A listener with none is refused when it is added, naming the setting to copy. The listener holds no more unacknowledged messages than that. So what an instance has in hand is the broker’s window and nothing of its own. It starts every message it holds at once. Each waits only for its device’s turn and a database connection. A broker whose window is lifted (0 in Mosquitto) cannot be described to the listener: give it a number. |
| A queue for an instance that is away | Set max_queued_messages (Mosquitto drops past it) and persistent_client_expiration for the longest outage to ride out. |
| Shared subscriptions | For several instances on one broker (instance ids). |
Before the first connection
Section titled “Before the first connection”The persistent session that keeps a clock’s messages while an instance is away exists only once the listener has connected to the broker once. Until then, a broker such as Mosquitto acknowledges a clock’s QoS 1 publishes with nobody subscribed to the topic. The messages are lost. The clock believes they were delivered. “Until then” means the first start, or a listener the install does not let reach its broker, whose status says so.
So start the listener and check that it is connected before the clocks are pointed at the broker. When it is connected, hourctl listeners shows no error, and houradvantage_mqtt_* counts move.
Where the broker can hold a subscription of its own for the listener’s client id, create it first. That is a persistent session created beforehand, or the broker’s own queue for the topic filter.
A listener that is not connected shows why in its status. A broker the install does not let a tenant reach says that, and what allows it (tenant_destinations), not a failure of the connection.
The listener’s view says whether it has connected since it started on the instance asked (connected_once). hourctl listeners shows failed (never connected) for a listener that has not. Until one instance has, the broker holds no session for it.
An instance scaled away while others stay ends its broker session only when it holds nothing. One that still holds a message (one that could not be set aside) keeps its session. That message waits for the same instance id to come back.
Over mqtts:// the listener checks the broker’s certificate against the host’s root store (in the container image, its ca-certificates). SSL_CERT_FILE and SSL_CERT_DIR override it. To trust a private authority, add it to the host’s store, or point SSL_CERT_FILE at a bundle that holds it (trusted roots).
Commands go only to allowed devices
Section titled “Commands go only to allowed devices”The listener pushes a device its reply and its queued commands only after one of its messages was taken in. A command queued for a device is pushed at once only when the device is allowed.
A device waiting to be allowed, unknown or blocked is sent nothing: no people lists, PINs or templates. What is queued for it waits, as over HTTP, where such a device is refused before it is handed anything. Once it is allowed, its next message brings what waited.
Setting up the broker
Section titled “Setting up the broker”Connect knows a device by its topic, and the broker decides who may publish there. Set the broker up so that a device can only be itself.
One credential per device
Section titled “One credential per device”Give each clock its own credential:
- a client certificate, with the device’s id as its common name, on a TLS listener that requires certificates; or
- a username and password of its own, the username being the device’s id.
Give the Hour Advantage instances a credential of their own, allowed to subscribe to the device topics and publish on the reply topics.
Topic ACLs per device
Section titled “Topic ACLs per device”A device may publish only on its own topic and subscribe only to its own
reply topic. For an endpoint with topic = "clocks/{device}/up" and
reply_topic = "clocks/{device}/down":
Mosquitto (acl_file). %u is the username and %c the client id. With use_identity_as_username true, a certificate’s common name is the username:
# Hour Advantageuser houradvantagetopic read clocks/+/uptopic write clocks/+/down
# every clock: its own topics onlypattern write clocks/%u/uppattern read clocks/%u/downEMQX (acl.conf, file authorization). ${username} is the username, or the certificate’s common name with peer_cert_as_username = cn:
{allow, {user, "houradvantage"}, subscribe, ["$share/+/clocks/+/up", "clocks/+/up"]}.{allow, {user, "houradvantage"}, publish, ["clocks/+/down"]}.{allow, all, publish, ["clocks/${username}/up"]}.{allow, all, subscribe, ["clocks/${username}/down"]}.{deny, all}.Blocking a clock
Section titled “Blocking a clock”Blocking a device in Connect stops all traffic with it inside Connect. Nothing it sends is taken in, and nothing is sent to it. Connect cannot stop it reaching the broker.
You block a device with hourctl device block, or with PATCH /api/tenants/{tenant}/devices/{endpoint}/{device} and status blocked. See devices.
The block emits a device.blocked event. The event carries the tenant, endpoint and device in its envelope, and the device’s topic and reply topic in its data. Have your own system revoke the clock’s broker credential (its certificate, or its username) when that event arrives.