Repository navigation
feat(message-bus): JetStream queue groups and BROADCAST subscription mode - #58
Merged
Merged
Conversation
…ptions PUB_SUB is documented as "within a queue group", and the NATS Streaming and Rusi transports subscribe it that way. The JetStream transport gave every PUB_SUB subscriber its own unnamed ephemeral consumer, so every instance received every message. PUB_SUB now uses one named ephemeral consumer per client id and subject, <clientId>__<subject>, shared by all subscribers, so they compete. Its inactive threshold is set explicitly to the server default (5 s): without it the stored config differs from the requested one and the second subscriber fails with "consumer already exists" (10148). RPC keeps fan-out with a named ephemeral consumer per subscriber, <clientId>__<subject>__<uuid>. STREAM_PROCESSOR is unchanged. This matches NBB's JetStream transport (osstotalsoft/nbb#299). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
BROADCAST is non-durable, at-most-once and has no queue group, so every instance of a service receives every message. It matches SubscriptionTransportOptions.Broadcast in NBB (osstotalsoft/nbb#299). - Appended to the SubscriptionOptions enum after RPC, so existing numeric values do not change. - NATS Streaming: plain subscription, new messages only. - JetStream: ephemeral consumer per subscriber, <clientId>__<subject>__<uuid>, new messages only. - Rusi: qGroup = false, durable = false, new messages only. - Own environment variables per transport (*_BROADCAST_MaxInflight / MaxConcurrentMessages and AckWait / AckWaitTime), with a default concurrency of 100 like PUB_SUB. RPC is unchanged: its name is part of existing environment variable configuration and sendCommandAndReceiveEvent keeps using it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 24c0db6 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
…differs jsm.consumers.add only creates, so a subscriber whose config differs from the shared PUB_SUB consumer's (for example a different JETSTREAM_PUB_SUB_AckWaitTime during a rolling update) failed with "consumer already exists" (10148). On that error the transport now calls jsm.consumers.update, so the latest subscriber's config wins, as with NATS.Net's CreateOrUpdate in NBB. Settings the server cannot update, such as the deliver policy, still fail. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
oncicaradupopovici
self-requested a review
October 27, 2026 16:10
oncicaradupopovici
approved these changes
Oct 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
PUB_SUBis documented as "non-durable, within a queue group", and the NATS Streaming and Rusi transports subscribe it that way: instances of a service compete for messages.The JetStream transport gave every
PUB_SUBsubscriber its own unnamed ephemeral consumer, so every instance received every message. The same mode name delivered differently depending on the transport.There was also no mode meant for delivering every message to every instance.
RPChappens to behave that way, but it exists forsendCommandAndReceiveEvent.Change
JetStream consumers
STREAM_PROCESSOR<clientId>__<subject>PUB_SUB<clientId>__<subject>, sharedRPC<clientId>__<subject>__<uuid>BROADCAST(new)<clientId>__<subject>__<uuid>The shared
PUB_SUBconsumer is created by the first subscriber.jsm.consumers.addonly creates, so a later subscriber whose config differs fails withconsumer already exists(10148). This happens even when the configs look identical, because the server stores defaults that the request leaves out. On that error the transport now callsjsm.consumers.update, so the latest subscriber's config wins, as with NATS.Net'sCreateOrUpdateConsumerAsyncin NBB. Settings the server can't change on an existing consumer, such as the deliver policy, still fail.PUB_SUBalso setsinactive_thresholdexplicitly to the server default (5 s): the consumer is deleted 5 s after the last subscriber stops pulling.New
BROADCASTmodeSubscriptionOptions.BROADCASTis non-durable, at-most-once and has no queue group, so every instance receives every message. Typical uses are refreshing an in-memory cache or pushing to the clients connected to each instance. It matchesSubscriptionTransportOptions.Broadcastin NBB.RPC, so the existing numeric values don't change.qGroup = false, durable = false, new messages only.NATS_BROADCAST_MaxInflight/NATS_BROADCAST_AckWait,JETSTREAM_BROADCAST_MaxConcurrentMessages/JETSTREAM_BROADCAST_AckWaitTime,RUSI_BROADCAST_MaxConcurrentMessages/RUSI_BROADCAST_AckWaitTime), with a default concurrency of 100 likePUB_SUB.RPCstays as it is, not deprecated: its name is part of existing environment variable configuration, andsendCommandAndReceiveEventkeeps using it.The README documents
BROADCAST, its environment variables, and how queue groups map onto JetStream consumers. Two changesets bump@totalsoft/message-busas minor.A service that relied on
PUB_SUBfanning out on JetStream will now see each message on only one instance. It should switch toBROADCAST. Raise the bump to major if you consider this breaking.Consistency
The same behaviour is implemented in:
Broadcastpreset: osstotalsoft/nbb#299fix/jetstream-consumer-groupsTesting
Unit tests:
jestpasses, 41 tests. That includes a new Rusi transport test checking the flagsBROADCASTsends to the sidecar.Build and lint:
tscsucceeds andeslintreports no new warnings.Live check:
message-bushas no automated tests for the JetStream transport. I ran the compiled transport against NATS 2.11 in Docker, with two subscribers per mode and 10 messages each:STREAM_PROCESSORPUB_SUBRPCBROADCASTAfter unsubscribing, the server removed every ephemeral consumer within the 5 s threshold and kept only the durable one.
Config mismatch: two separate processes subscribed with
PUB_SUBusing differentJETSTREAM_PUB_SUB_AckWaitTimevalues (30 s, then 10 s). Both subscribed, the shared consumer's stored ack wait changed from 30 s to 10 s, and the two split 10 messages 5/5.