Skip to content

feat(binding-mqtt): MQTT 5 shared subscriptions in server and client - #3110

Merged
jfallows merged 26 commits into
feature/mqtt-shared-subscriptions-1-kafka-sharefrom
feature/mqtt-shared-subscriptions-2-mqtt-server-client
Nov 17, 2026
Merged

jfallows merged 26 commits into
feature/mqtt-shared-subscriptions-1-kafka-sharefrom
feature/mqtt-shared-subscriptions-2-mqtt-server-client

Conversation

@jfallows

@jfallows jfallows commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Description

Adds MQTT 5 shared subscriptions ($share/<ShareName>/<filter>, MQTT 5.0 section 4.8.2) to binding-mqtt: the server side, acknowledgement reason codes, the client side, the versioned session state and QoS 2 de-duplication. It is the protocol layer that the Kafka-backed delivery in the next PR builds on.

Part of #1801

Server

  • $share/<ShareName>/<filter> is parsed and validated. The application sees the stripped filter plus shareName in MqttTopicFilter. A shared subscription is identified by (ShareName, filter), and there is one application subscribe stream per shared subscription.
  • Retained messages are never requested for a shared subscription, and noLocal is a protocol error (3.8.3-4). Delivery is at min(message QoS, granted). MQTT 3.1.1 clients get SUBACK 0x80 for a $share filter.
  • Optional share route condition (exact name or * glob). Omitted means no constraint on the share name. A route with share does not match non-shared subscriptions.

Acknowledgements

  • MqttSubscribeFlushEx carries the PUBACK or PUBREC reason code. A PUBREC at 0x80 or above ends the exchange with no PUBREL (4.8.2-6) and is flushed as complete with its reason code.
  • MqttPublishFlags gains DUP for a redelivered PUBLISH.
  • Per-filter SUBACK reason codes resolved from routes are kept as they are on develop; a shared subscription that no route matches is answered with a per-filter failure.

Session state

MqttSessionState becomes an enum-switched union (V1, V2, V3). Version 1 is the legacy layout with generated subscription ids, version 2 is the layout released on develop, and version 3 adds shareName. Version 2 is still written unless a subscription is shared, so rolling upgrades stay safe. The wire layouts are pinned by spec tests.

Client (mqtt server to mqtt client pipeline)

QoS 1 and 2 inbound from the upstream broker, with de-duplication of a retransmitted QoS 2 PUBLISH, $share/<name>/<filter> sent upstream, acknowledgements and reason codes relayed, and session state of all versions.

Also in this layer

  • binding-mqtt-kafka and binding-mqtt-kafka.spec get a small adaptation so session state is forwarded in the layout of its own version, and a session-state pass-through spec for a shared subscription.

Stack

This is one layer of a stack of PRs for #1801. Each PR contains only the commits since the previous layer; this PR is based on the previous layer's branch.

# PR Layer Base
1 #3109 binding-kafka: share group stream develop
2 #3110 (this PR) binding-mqtt: shared subscriptions, server and client #3109
3 #3111 binding-mqtt-kafka: shared subscriptions for QoS 0 and 1 #3110
4 #3112 binding-mqtt: SUBACK after application streams begin #3111
5 #3113 binding-mqtt: complete in-flight deliveries when unsubscribing #3112
6 #3114 binding-mqtt-kafka: QoS 2 claim tickets (with engine test store assertions) #3113
7 #3115 binding-mqtt-kafka: epoch registry for the lifecycle of a shared subscription #3114
8 #3116 real-broker share stream fixes and mqtt.kafka.proxy example (top of the stack) #3115

Documentation: aklivity/aklivity-docs#181 (merge after the stack).

Test evidence

Run on this branch tip (checkstyle and license checks run in the validate phase of each build):

  • ./mvnw clean verify -pl specs/binding-mqtt.spec,specs/binding-mqtt-kafka.spec,runtime/binding-mqtt,runtime/binding-mqtt-kafka,runtime/binding-asyncapi,specs/binding-asyncapi.spec,incubator/command-dump,incubator/command-log,config/binding-mqtt.conf,config/binding-mqtt-kafka.conf -Dmaven.build.cache.enabled=false: OK (0 failures)

Notes for reviewers

  • The stack is linearized by rebase and force push, so expect SHA changes against any earlier view.
  • Commits keep the test-first order: the spec or test commit comes before the implementation commit.
  • Later layers harden the SUBACK and UNSUBSCRIBE behavior found while wiring this to Kafka; known limitations are tracked in Support MQTT 5 Shared Subscriptions via Kafka share groups #1801.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SiHVoWETrA5Cn7uqKAtsMH

Add an optional shareName to MqttTopicFilter, bump MqttSessionState to
version 2, and keep the version 1 layout readable through
MqttTopicFilterV1 and MqttSessionStateV1. MqttFunctions gain sharedFilter
and sharedSubscription builders, a shared filter begin matcher and a
version 1 session state builder.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
Add an optional share condition to when[].subscribe[] in the schema, the
condition config, builder and adapter. The value is a share name or a
wildcard.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
Add paired network and application scripts, spec ITs and runtime ITs for
shared subscriptions: stream per shared subscription, share name and
filter identity, shared with non-shared filters, QoS change on
re-subscribe, unsubscribe, malformed and no-local rejection, share route
condition, delivery at the lower of message and granted QoS, restore on
reconnect from version 2 and version 1 session state, and MQTT 3.1.1
filters declined in the SUBACK. Correct the topic filter length in the
unsupported shared subscription script.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
Recognize a shared subscription by its $share/ prefix and validate the
share name and topic filter. A shared subscription is identified by its
share name and topic filter, opens one application subscribe stream at the
granted QoS, ignores retain handling and rejects no local. Re-subscribing
updates the granted QoS with a filter flush, and unsubscribing ends the
stream. Messages are delivered at the lower of the message QoS and the
granted QoS. Match the share condition when routing a subscription.

Persist the share name in version 2 of the session state and keep reading
version 1. Decline a shared subscription from an MQTT 3.1.1 client with a
SUBACK failure code and remove the unused shared subscription property.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
…n state by version

Add reasonCode to MqttSubscribeFlushEx so an acknowledgement reason code
can travel with a PUBACK or PUBREC. The session state builder writes
version 1 unless a shared subscription is present, in which case it
writes version 2, so state without shared subscriptions stays readable
by earlier versions.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
…hared subscriptions

Add paired network and application scripts, spec ITs and runtime ITs for
PUBACK and PUBREC reason codes of at least 0x80 on QoS 1 and QoS 2 delivery,
for shared QoS 1 delivery, and for a CONNACK that declines shared
subscriptions. Run QoS 1 and QoS 2 delivery, shared subscribe,
re-subscribe, unsubscribe and mixed shared and non-shared subscriptions
against the client binding, with the shared subscription options a client
sends carrying retain handling that sends no retained messages.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
…ipeline for mqtt

Return the granted QoS in the session state echoed after a subscribe,
add session state builders that carry a reason code, and add network
scripts for the upstream side of a server binding routed to a client
binding. Cover shared and non-shared QoS 1 and QoS 2 delivery with
acknowledgement reason codes, mixed shared and non-shared subscriptions
and a declined shared subscription.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
…on codes through the server-to-client pipeline

Add an IT for a server binding routed to a client binding, a unit test for
the session state codec and reason code classification, and the mixed
shared and non-shared message scenario for the server and client bindings.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
…ssion state by version

Forward the reason code of a PUBACK or PUBREC to the subscribe stream. A
PUBREC with a reason code of at least 0x80 ends the QoS 2 exchange: no
PUBREL follows, the packet identifier is released and the subscribe window
is reopened. Write session state as version 1 unless a subscription has a
share name, and read either version, through a shared codec. Correct the
session data reservation check for a padded session stream.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
…ivery in the client binding

Send a shared subscription upstream as $share/<name>/<filter> in SUBSCRIBE
and UNSUBSCRIBE, carry the requested QoS in the subscription options and
deliver to one application stream per shared subscription. Decode QoS 1 and
QoS 2 PUBLISH packets with their packet identifier, send PUBACK, PUBREC and
PUBCOMP with the reason code the application acknowledged with, and relay
PUBREL to the stream that holds the QoS 2 exchange. Replace a subscription
that is subscribed again instead of unsubscribing it, read and write session
state of either version, and do not treat a server keep alive property as a
repeated shared subscription available property.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
… a version

Forward the session state payload as written instead of wrapping it with
the latest layout, so state of version 1 or version 2 passes through
unchanged.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
… mqtt client

Add paired network, application and upstream scripts and spec ITs for a
QoS 2 PUBLISH retransmitted before the acknowledgement, after PUBREC was
sent, after the exchange completed and after PUBREC with a reason code of
at least 0x80, together with the network scripts of a client that receives
the de-duplicated deliveries.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
…elivering it again

A QoS 2 PUBLISH that reuses the packet identifier of an exchange still in
progress is not delivered again. It is dropped while the acknowledgement is
pending, and answered with the PUBREC reason code already sent once the
acknowledgement was sent. The packet identifier is free again once PUBCOMP
is sent or PUBREC carried a reason code of at least 0x80.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
…ion layouts

Declare the session state as a union switched on its version so the same
flyweight reads and writes version 1 and version 2 layouts. The wire layout
of each version is unchanged.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
…ion union

Replace the version peeking codec with the session state flyweight,
selecting the layout by its version when reading and writing version 1
unless a subscription is shared.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
… in mqtt subscribe dump

The mqtt topic filter now carries an optional share name and the subscribe
flush carries the acknowledgement reason code. Add a shared subscription
scenario that acknowledges a delivery with a reason code, and expect the
extra bytes of the optional share name in the existing subscribe scenario.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoWETrA5Cn7uqKAtsMH
…pts with per-filter acknowledgement reason codes

Use the version-aware session state builder in the qos 2 de-duplication
scripts, flush failed pubrec acknowledgements as complete with their
reason code, and open the shared session state kafka scripts with the
retained topic describe.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoWETrA5Cn7uqKAtsMH
@jfallows
jfallows force-pushed the feature/mqtt-shared-subscriptions-2-mqtt-server-client branch from 0738149 to 95de3bb Compare November 17, 2026 03:50
…n the client binding

Answer a failed qos 2 acknowledgement with PUBREC carrying its reason code
and release the packet id, and record a granted qos from SUBACK as
success in the session state.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoWETrA5Cn7uqKAtsMH
@jfallows
jfallows merged commit a34f7c9 into develop Nov 17, 2026
81 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants