Skip to content
CommunicationDocs 1.3.x

Choose an Event Adapter ​

Every Connectum EventBus uses the same EventAdapter contract. Choose a broker from workload and operational requirements, then keep broker-specific tuning in that adapter's generated API reference.

This guide targets the 1.3.x release line. Features marked Since 1.3.0 require the corresponding package at 1.3.0 or later.

Install @connectum/events and the adapter package for your broker; see installation.

Broker Selection Matrix ​

AdapterPersistenceConsumer coordinationOrdering scopeBest starting fit
MemoryNoNonePublish callUnit tests and local prototypes
NATS JetStreamYesDurable consumersSubjectLightweight, low-latency service events
Kafka / RedpandaYesNative consumer groupsPartitionHigh-throughput streams and retained logs
Redis Streams / ValkeyConfigurable by RedisConsumer groupsStreamTeams already operating Redis-compatible infrastructure
AMQP / RabbitMQ / LavinMQDurable queues/exchanges when configuredCompeting consumersQueueRouting topologies and external AMQP contracts

Production adapters support broker redelivery of unacknowledged events, so handlers must be idempotent and acknowledge only after their side effects are complete. Delivery and retention still depend on broker configuration, start position, stable consumer groups, and settlement policy. For AMQP, a broker confirm alone does not prove that any queue accepted the message; see Routing and delivery.

Memory ​

MemoryAdapter() ships with @connectum/events. It has no external dependency, persistence, or consumer groups and is intended for tests and local development.

typescript
import { MemoryAdapter } from '@connectum/events';

const adapter = MemoryAdapter();

Since 1.3.0 nack(true) (and nack()) redelivers the event to the same subscription as a later task, with attempt raised by one, so a handler's retry-by-nack behaves in tests as it does on a broker. nack(false) discards the event. A redelivery that is still waiting when the subscription is removed or the adapter disconnects is dropped, which is what stops a bus that is draining from looping nack, redeliver, nack. A handler that nacks every time is redelivered for as long as its subscription lives: unlike a broker, MemoryAdapter has no attempt cap and no dead-lettering. Before 1.3.0 a requeued event was silently lost.

NATS JetStream ​

Choose NATS for durable subjects, wildcard routing, and a compact operational footprint.

typescript
import { NatsAdapter } from '@connectum/events-nats';

const adapter = NatsAdapter({ servers: 'nats://localhost:4222' });

Start position ​

consumerOptions.deliverPolicy sets where a new consumer starts: "new" (the default) delivers only messages published after the consumer is created, "all" delivers every available message, and "last" delivers the last message of each matching subject. Since 1.3.0, "last" maps to the JetStream policy last_per_subject, so a wildcard pattern such as orders.* starts with the latest message of every subject it matches, not only the single most recent message that matches the filter. For a literal pattern nothing changes.

The policy applies only when the consumer is created. A durable consumer that already exists keeps the policy it was created with; the adapter logs one warning that names the difference (for example deliver_policy=last (requested last_per_subject)) and continues with the existing configuration. To start such a group with the new policy, delete its durable consumer or use a new group name.

Overlapping patterns ​

A subscription that lists patterns matching the same subject (orders.created, orders.*, orders.>) runs its handler once per event. Every pattern keeps its own durable consumer, so the server still sends the event once per matching pattern; the adapter runs your handler for one of those deliveries and acknowledges the others without running it.

The delivery that runs the handler is the one of the most specific pattern among those whose consumer delivers the event: fewer > first, then fewer *, then more tokens, then the pattern text. The order comes from the pattern text alone, so every instance of a group picks the same delivery, and patterns that overlap only in part (a.*.c and a.b.*) need no special case.

  • Existing consumers keep their backlog. Consumers left by earlier versions or by earlier runs of the service keep their position. Events published while the service was down are delivered once, whichever of the overlapping patterns holds them. A pattern added later starts at consumerOptions.deliverPolicy ("new" by default) like any new route. Upgrading or rolling back needs no action.
  • Existing consumers are never modified. The adapter reads an existing consumer and attaches to it; it does not update its configuration, so an earlier version of the adapter can create the same consumer again after a rollback, and consumers provisioned by an operator need only read and pull permissions ($JS.API.CONSUMER.INFO.>, $JS.API.CONSUMER.MSG.NEXT.>, $JS.ACK.>). Each instance works out from where every consumer delivers from what the server reports; nothing is written to the broker. When an existing consumer was configured with another ackWait, maxDeliver or deliverPolicy than the subscription asks for, the adapter keeps the existing one and logs one warning per consumer.
  • Instances with different route sets. While a service is rolled out with a route added to a broader pattern, instances with and without that route run side by side. Nothing is lost, but an event can be handled by both kinds of instance, so a handler may see it twice. This is the at-least-once contract; the window lasts as long as the rollout. Handlers should tolerate redelivery. The same can happen on any server when instances attach at different times while a wider consumer still lags behind a narrower one, and when the consumer of a pattern holds a delivery that was never acknowledged.
  • Network traffic. The server sends one copy per matching pattern. Prefer patterns that do not overlap on a hot subject.
  • Consumers of removed routes. Consumers of patterns you stopped subscribing to stay on the broker, and their pending count grows with every matching event (on a stream with interest retention they keep every message). Once no instance runs the old route set, list them with nats consumer ls <stream> and remove the ones named {group}--{pattern}--{hash} (nats consumer rm <stream> <name>). Do not remove the consumer of a pattern a running instance still subscribes: it stops delivering without an error. A subscribe() that fails half-way leaves the consumers of a named group in place for the next start; only the consumers of an auto-generated group are removed.

Consumer options ​

NatsAdapter() checks its options when it is created. A value it cannot honor throws a TypeError or RangeError that names the option; before 1.3.0 several of them were replaced silently (ackWait: 0 became the default, an unknown deliverPolicy behaved like "new", an empty servers list dialled the local address) or failed only at the first subscribe(). Valid values behave as before; see NATS adapter options if an upgrade starts to throw.

OptionDefaultAllowed
serversrequireda non-empty string, or a non-empty array of non-empty strings
consumerOptions.deliverPolicy"new""new", "all", "last"
consumerOptions.ackWait30000a finite number of milliseconds greater than 0
consumerOptions.maxDeliver5-1 (unlimited) or a positive integer
consumerOptions.prefetchthe client's buffer of 100an integer from 1 to 2147483647
consumerOptions.redeliveryDelay0an integer number of milliseconds from 0 to 2147483647

Slow handlers and duplicates ​

The server starts the ackWait timer of a message when it hands the message to the client, and the handler of a consumer runs one message at a time. The client buffers up to 100 messages ahead of the handler, so a message at the back of a full buffer waits for every message in front of it. When that wait outgrows ackWait the server delivers the message again, and a single consumer sees duplicates. A full buffer of 100 with the default ackWait of 30 s reaches that point when handling takes more than 300 ms per message on average (the quotient of the two, not a measurement).

Measured on JetStream 2.11 with ackWait: 1500: 8 messages of 400 ms each arrived 13 times with the default buffer and 8 times with prefetch: 1. Set consumerOptions.prefetch to a small number for handlers that are slow compared with ackWait / 100, or raise ackWait. A smaller buffer makes the client ask the server for messages more often. The default is unchanged, and handlers must be idempotent either way.

Handler failures and maxDeliver ​

A handler that throws gets its message back with a negative acknowledgement. With the default settings the server delivers it again at once, so the default maxDeliver of 5 is used up within milliseconds (five deliveries in 4 ms on JetStream 2.11 and 2.9.25) and a failure that a retry a second later would have cured is final. After the last delivery the server stops, the message stays in the stream until its retention removes it, and nothing consumes it again.

  • consumerOptions.redeliveryDelay is opt-in. With redeliveryDelay: 1000 the server holds the message for one second after each handler failure. The server keeps the pause, so it does not block the handler or occupy buffer space; it applies to every failure of the adapter's subscriptions, not to an explicit nack() of the handler, and unsubscribe() cannot shorten it: the next consumer of the group receives the message after the pause.
  • A delay does not replace the retry and dead-letter middleware for a message that can never succeed: each delivery still counts against maxDeliver.
  • When a handler failure or nack() gives a message back on the last delivery the consumer allows, the adapter logs one error line that starts with [EventBus/NATS] and names the topic, the delivery number, the consumer, the stream and the limit. A consumer with maxDeliver: -1 never reports it.

Metadata ​

The metadata passed to publish() reaches the handler (ctx.metadata) as it was given: keys keep their case, and keys that start with x- and values that are empty strings arrive too. One key belongs to the adapter: x-event-id carries the event id the adapter generates, and a value for it in the metadata is ignored. Before 1.3.0 the adapter dropped every x- key on publish and every empty value on receive, so a handler that read x-request-id saw it on Redis and in memory but not on NATS.

When the server loses the stream ​

Deleting a JetStream stream deletes its consumers. When the stream comes back, the adapter recreates the consumers of its subscriptions and the handlers receive events again:

  1. The adapter logs [EventBus/NATS] The server lost consumer "..." ... once.
  2. It polls for the stream (the pause grows from 250 ms to 5 s) and, when the stream exists, creates the consumer again from the first message of the new stream: every message in a stream created after the loss was published after it.
  3. It starts reading the new consumer and logs ... exists again; delivery resumes.

unsubscribe() and disconnect() end the wait, and so does a connection that the client closed for good (the adapter logs that the subscription receives nothing more). The adapter does not create a deleted stream itself, and publish() rejects while the stream does not exist. If the stream is lost again while the new consumer is being read, the consumer is created again first.

How soon the adapter notices the loss depends on the server. When the server answers a waiting request for messages with consumer deleted or no responders, the adapter reacts at once. On nats-server 2.9 the server sometimes sends no answer at all to a request that was already waiting; the client then notices the loss only after it misses two idle heartbeats, which took 30 to 45 s in the measurements (nats-server 2.9.25). On 2.10.29 and 2.15.0 this did not happen in 60 deletions each. The adapter keeps the pull request cadence of every subscription as it is, so the delay on 2.9 is a known limit; use nats-server 2.10 or later when a subscription must resume sooner after its stream is recreated.

A subscription that lists overlapping patterns (see above) recreates one consumer per pattern. Until a pattern's own consumer is back, a delivery of another pattern cannot know where the missing one starts and runs the handler itself, so an event can run the handler twice in that window and is never skipped; handlers must be idempotent, as everywhere on this adapter.

A consumer deleted on its own, while its stream still exists, is a different case: an operator removed it, and the adapter does not create it again behind their back. It logs one line (... was deleted while its stream still exists ...), and the subscription receives nothing until the service subscribes again.

Timeout errors ​

The client reports a request that got no answer as TimeoutError: timeout. The adapter puts what was being done in front of that text and keeps the error class and name:

CallMessage
connect()NatsAdapter: connect to <servers> did not complete in time: timeout (20 s by default; connectionOptions.timeout changes it)
connect(), creating the streamNatsAdapter: preparing stream "<stream>" on <servers> did not complete in time: timeout
publish()NatsAdapter: publish to topic "<topic>" on stream "<stream>" was not acknowledged in time: timeout (5 s)
subscribe()NatsAdapter: subscribe to topics ["<topic>", ...] on stream "<stream>" did not complete in time: timeout

A topic longer than 100 characters is cut in the message and its length is given. A server URL with credentials (nats://user:password@host:4222) appears without them. When the broker is unreachable and the client has already given up reconnecting, the client's own RequestError: connection closed is raised instead and is not rewritten.

Kafka or Redpanda ​

Choose Kafka-compatible infrastructure for partitioned ordering, retained logs, and high-throughput stream processing.

typescript
import { KafkaAdapter } from '@connectum/events-kafka';

const adapter = KafkaAdapter({
  brokers: ['localhost:9092'],
  clientId: 'orders-service',
});

Connection options ​

TLS, SASL, timeouts, retry and the client's log level are options of KafkaAdapter() with types that this package owns; none of them names the Kafka client library:

typescript
const adapter = KafkaAdapter({
  brokers: ['kafka-1:9093'],
  ssl: { ca: process.env.KAFKA_CA },
  sasl: { mechanism: 'scram-sha-512', username: 'orders', password: process.env.KAFKA_PASSWORD ?? '' },
  requestTimeout: 30_000,
  retry: { retries: 8 },
  logLevel: 'warn',
  producerOptions: { compression: 'gzip' },
});
OptionValue
ssltrue for the system trust store, or { ca, cert, key, passphrase, rejectUnauthorized, servername }
saslplain, scram-sha-256 and scram-sha-512 with username and password; oauthbearer with an oauthBearerProvider function; aws with authorizationIdentity, accessKeyId, secretAccessKey and optionally sessionToken
connectionTimeout, authenticationTimeout, requestTimeout, reauthenticationThreshold, enforceRequestTimeoutTimeouts in milliseconds, the SASL re-authentication share, and whether the client enforces requestTimeout itself
retrymaxRetryTime, initialRetryTime, factor, multiplier, retries
logLevelnothing, error, warn, info or debug
producerOptions.compressionnone, gzip, snappy, lz4 or zstd

gzip works as shipped. snappy, lz4 and zstd need a codec that your application registers with the client library before the first publish; without one, publishing fails.

Settings that have no portable form go in clientOptions: a log factory, a socket factory, a custom SASL mechanism. Its keys are those of the client library the adapter uses today (KafkaJS); they are not type-checked and not portable, and may change if that library is replaced. The value is merged shallowly over the typed options and can never override brokers or clientId.

The kafkaConfig option of earlier versions is removed and throws a TypeError; see Kafka adapter options.

Validation ​

KafkaAdapter() checks these options when it is called and throws an error that names the option and the value it received, instead of failing later at connect() or subscribe() with a message that does not:

OptionAcceptedError
the options objectan objectTypeError
brokersa non-empty array of non-empty host:port stringsTypeError (not an array, or a bad entry, with its position), RangeError (empty)
clientIda non-empty string, or omittedTypeError
consumerOptions.sessionTimeouta finite number of milliseconds above 3000 (the heartbeat interval)RangeError
retry.retriesa non-negative integerRangeError
retry.maxRetryTime, initialRetryTime, factor, multipliera non-negative finite numberRangeError
sasl.mechanismplain, scram-sha-256, scram-sha-512, oauthbearer or awsRangeError

A SASL mechanism that only the client library knows (a custom one) still goes in clientOptions.sasl, which replaces the typed sasl. See Kafka adapter validation.

Connection errors ​

When a broker's host name resolves to several addresses (localhost often does) and every one refuses the connection, Node reports the failure with an empty message. The adapter fills the reason in from the socket's error code and the broker address, so the error reads Connection error: connect ECONNREFUSED localhost:9092 instead of Connection error: . The error keeps its class and its cause. A publish to a topic name the broker refuses fails with an error that names the topic. Kafka accepts names of ASCII letters, digits, ., _ and -, at most 249 characters; the broker's own error is the cause.

Calling connect() again while an earlier call is still connecting returns the same attempt: both calls settle together, and a failed attempt rejects both with the same error and is not remembered. disconnect() during connect() waits for the attempt and then closes the connection.

Topics that do not exist yet ​

subscribe() on a topic the broker does not have fails at once with an error that names the topic and consumerOptions.allowAutoTopicCreation. A consumer service that starts before the first publisher therefore fails in bus.start() unless the topics exist. Either create the topics before the services start, or set consumerOptions.allowAutoTopicCreation: true and let the broker create them: the broker must allow it (auto.create.topics.enable, on by default in Apache Kafka), and subscribe() then asks again for up to ten seconds, because the broker answers the first request for a missing topic with an error and creates the topic a moment later. If the topic does not appear, the error says that the broker did not create it. Wildcard patterns only match topics that exist and are not affected. Publishing to a missing topic creates it when the broker allows it, as before.

Acknowledgement and redelivery ​

Delivery is at-least-once. The adapter commits a partition offset only from the handler's outcome, never from a client timer:

Handler outcomeOffsetWhat happens next
ack() (EventBus calls it after a successful handler)committednext message
nack(false)committedmessage is skipped; nack(false) itself publishes no DLQ copy
nack(true) or nack()not committedthe message and the rest of the batch are delivered again, in order
handler throws while the message is still uncommittednot committedsame as nack(true); the error is logged with topic, partition and offset
handler throws after ack() or nack(false) committed the offsetcommittedthe message is not redelivered; the error is still logged with topic, partition and offset
adapter used directly, handler returns without settlingnot committedsame as nack(true)

Through the EventBus a handler that returns normally without settling is acknowledged automatically, so call nack(true) when the message must be redelivered. The DLQ middleware publishes a copy only when the handler throws; it then acknowledges the original, so a message the DLQ middleware moved is not redelivered.

A Kafka offset means "everything before it is consumed", so settlement is ordered: the first message that is not committed ends the batch and is the first one fetched again. The first settlement of a message wins; an ack() called after the handler returned is ignored.

An unsettled message is delivered again after consumerOptions.redeliveryDelay milliseconds (default 1000): the adapter pauses the partition for that long. 0 redelivers immediately, so a handler that fails permanently retries in a tight loop and writes one log line per attempt. The maximum is 2147483647. On an otherwise idle consumer the observed gap is a whole fetch cycle (5 s in KafkaJS) even for smaller values.

A message that always fails blocks its partition

Nothing behind a message that fails on every delivery is delivered until the handler succeeds, nack(false) is called, or the DLQ middleware moves it. redeliveryDelay only paces the loop; it does not end it. Bound a permanently failing message with the retry and DLQ middleware.

attempt is always 1 on Kafka: the broker does not count deliveries.

Commit strategy ​

By default every acknowledged message is committed by its own OffsetCommit request, so a batch of 20 acknowledged messages costs 20 requests to the group coordinator. consumerOptions.commitStrategy changes when the commit is sent:

ValueOffsetCommit requestsawait ack() returns
"per-message" (default)one for every acknowledged messageafter the broker accepted the commit
"per-batch"one for the last acknowledged message of the batchat once, nothing is sent yet
typescript
const adapter = KafkaAdapter({
  brokers: ['localhost:9092'],
  consumerOptions: { commitStrategy: 'per-batch' },
});

With "per-batch" the commit is sent when the adapter stops working on the batch, and it is sent on every way that can happen: the end of the batch, a message that is requeued with nack(true) or returned without settling, a handler that throws, the consumer being stopped, and a lost group membership (a failed heartbeat). What was acknowledged is committed in each of these cases; a message that was not acknowledged is never committed, so the ordering rules above are the same in both modes. If the commit for a batch that otherwise ended normally fails, the failure reaches KafkaJS like any other commit failure. If it fails after the batch ended with an error, the original error is the one that reaches KafkaJS and the commit failure is logged.

Wider window of duplicates

With "per-batch" an acknowledgement is not durable until the batch ends. If the process dies, or the broker refuses the commit, between an ack() and the end of the batch, every message acknowledged in that batch is delivered again, up to the size of the batch, instead of at most one. Handlers must be idempotent. A dead-letter copy published before the original is acknowledged can also be published twice. Delivery stays at-least-once.

How many requests "per-batch" saves depends on how many messages KafkaJS returns in one fetch, not on a setting of the adapter. When a batch holds a single message, both modes send the same one request.

Long-running handlers ​

While a handler runs, the adapter keeps sending group heartbeats on its behalf, so processing longer than consumerOptions.sessionTimeout (default 30000 ms) does not by itself expire the group membership. Successful heartbeats allow the handler to commit; a rebalance or connection failure can still cause redelivery. The adapter beats every 3 seconds (the KafkaJS default heartbeatInterval), checking twice per interval, and stops as soon as the handler returns, throws, or its turn ends.

If a heartbeat fails (the broker removed the member or started a rebalance) and the message was not committed, the adapter hands the error to KafkaJS so the consumer rejoins the group instead of redelivering on a membership the broker no longer recognises; the message is delivered again after the rejoin. A message whose offset was already committed stays committed.

The handler is never cancelled by the adapter: the EventBus only aborts ctx.signal after handlerTimeout (default 30000 ms), and a handler that ignores that signal and never returns holds its place in the group indefinitely. Make long handlers honour ctx.signal.

Stopping a consumer ​

Stopping a consumer waits for the fetch request it has open. The broker holds that request until a message arrives or the client's maxWaitTimeInMs (5000 ms in KafkaJS 2.2.4, consumer/index.js, and the adapter does not change it) has passed, so an idle consumer needs up to five seconds to stop: the rest of the current wait. The time depends on where in that five-second cycle unsubscribe() or bus.stop() is called. In a run against Kafka 4.2.0 on Node 26 the stop took 4.0 s, 2.0 s and 0.5 s when it was called 1.0 s, 3.0 s and 4.5 s after the consumer started, and 4.7 s when called 0.3 s after the start. A consumer that is receiving messages stops as soon as the fetch in flight returns.

The consumers of a bus stop at the same time, so the cost is that of the slowest one, not the sum (three consumers together: 2.8 s). Count up to five seconds into the time that bus.stop() and the eventbus shutdown hook add to a server's shutdown (see graceful shutdown) and into terminationGracePeriodSeconds.

On Node 26 a process that uses the Kafka client also prints TimeoutNegativeWarning: <large negative number> is a negative number. Timeout duration was set to 1. It comes from the client's request queue (KafkaJS 2.2.4, RequestQueue.scheduleCheckPendingRequests), not from Connectum; the timer runs after 1 ms, as the message says.

Start position ​

A consumer group with no committed offset starts at the end of the topic (consumerOptions.fromBeginning: false, the default), so messages published before the group first commits an offset are not delivered. Once the group has committed, messages published while it is stopped are delivered on restart. Set fromBeginning: true to read a topic's history with a new group.

A subscription without an explicit group gets a new group named connectum-<uuid> every time subscribe() runs. That group has never committed an offset, so with the default fromBeginning: false a restarted service starts at the end of each topic and the events published while it was down are not delivered. Set group (the group option of the event bus) to a stable name for any subscription that must not lose them.

Wildcard subscriptions ​

* and > are converted to a regular expression over topic names. A pattern longer than 256 characters, wildcard or literal, is refused: subscribe() rejects with Topic pattern exceeds maximum length (256): <length>. Two properties differ from NATS:

  • A pattern that opens with a wildcard never matches topics whose name starts with __. That is the set of topics the brokers mark as internal (Kafka: __consumer_offsets, __transaction_state; Redpanda: __consumer_offsets). Without it, a catch-all > would feed the broker's binary bookkeeping records to your handler. The rule has no switch. A pattern that spells the prefix out (__audit.>), a literal topic name and names with a single leading underscore are unaffected. Other topics a platform keeps are ordinary names, and > receives them: Redpanda's Schema Registry topic _schemas is one, so use a narrower pattern such as orders.> when you do not want it.
  • The topics a wildcard stands for are expanded when subscribe() runs and refreshed every consumerOptions.topicDiscoveryInterval (default five minutes): the adapter lists the broker's topics at that interval and, when a matching topic has appeared, restarts the subscription's consumer to include it. The restart rebalances the consumer group, so consumption pauses for a few seconds and messages being handled at that moment are delivered again; it happens only when there is a new topic, and an unchanged topic list costs one metadata request per wildcard subscription per interval. A discovered topic is read from its first message, whatever fromBeginning says. The checks use an extra admin connection, and subscriptions made only of literal topic names are never checked. A wildcard subscription lists the topics once when it starts even with the checks off (and once more right after subscribing, so that a topic created in between is not lost), so that it can tell a deleted topic from one that never existed (see a topic is deleted). If listing the topics fails when the subscription starts, subscribe() rejects with that error: the same metadata request expands the wildcard, and a discovery started from an empty list would read the history of every existing topic. A check that fails later (for example because the credentials may not describe the cluster) is logged and repeated at the next interval, so a permanent failure logs once per interval. Each discovery is logged with the names of the topics, so a rebalance it causes can be told from one caused by a failing member.
  • Set consumerOptions.topicDiscoveryInterval to false to keep the topic list fixed (the behaviour of earlier versions). Then a matching topic created later is not consumed until the service restarts, and the restart reads it from its end unless fromBeginning is set: what was published to the topic before the restart is never handled by the group. The same window exists with the checks on when the service restarts before a check has seen the topic; it is at most one interval long.

In a group of several members the delay before a new topic is read completely is the longest interval among the members: KafkaJS assigns partitions only from the topic list of the group's leader, and a member drops assigned topics it has not subscribed to itself, so every member has to discover the topic first. Nothing is lost meanwhile. In a measurement with two members at 1 s and 20 s, all 12 messages sent to the new topic arrived, the last one 15.3 s after the topic was created (15.1 s on Redpanda; the exact delay depends on where the 20 s check falls). Give the members of a group the same interval.

A topic is deleted ​

The Kafka client library cannot remove a topic from a running consumer. When a topic that a wildcard had matched is deleted, the consumer crashes on its next fetch, and the library restarts it with the same topic list, which fails on the deleted topic every time. Without help the other topics of the subscription deliver nothing until the library's own metadata refresh clears the state; in a measured run with Kafka 4.2.0 on Node 26 the first message reached the handler 300.7 s after the deletion, after 936 consumer-restore-failed events (topicDiscoveryInterval made no difference). With the replacement described below the same scenario delivered its first message 12.0 s after the deletion, and the lifecycle showed one consumer-lost and one consumer-restored.

The adapter handles it. When a consumer of a wildcard subscription crashes with "unknown topic" and the broker's topic list shows that a topic the wildcard had matched is gone, it replaces the consumer: the new one joins the same group, subscribes the literal topics and the matches that still exist, and starts reading from the group's committed offsets. Messages published to the remaining topics meanwhile are delivered after the replacement, as with any restart of a consumer in a group that has committed (a topic the group has no committed offset for starts where fromBeginning says, except a topic that was found after the subscription started, which is read from its first message again). A matching topic that was created in between is subscribed and read from its first message when discovery is on. If the deleted topic is created again and discovery is on, it is consumed again from its first message. The recovery works with topicDiscoveryInterval: false too; the new consumer then keeps the topic list fixed and subscribes no topic created since the start. It cannot be turned off.

With lifecycle.onLifecycle set you see one consumer-lost and then consumer-restored; the adapter also logs one warning that names the deleted topics. The replacement rebalances the group once, like a discovery restart, so messages being handled at that moment are delivered again. A crash for any other reason, or a crash while the broker still lists every matched topic, stays with the library's own restarts. When the adapter takes a crash over, it tells the library not to restart that consumer, so the old consumer is never started beside its replacement. If creating the replacement fails, the error is logged and the attempt is repeated every five seconds. A replacement whose start fails without an error from the client (for example because the group may not be joined: the client reports it as a crash and returns) counts as failed in the same way: it is disconnected and started again five seconds later, and the crash it caused reaches lifecycle.onLifecycle like any other.

A subscription to a literal topic name is not replaced: if that topic is deleted, the consumer keeps failing until the topic exists again.

Partition keys and metadata ​

Pass PublishOptions.key to set the Kafka record key used by the broker client's partitioner. Without it, the adapter sends a null key. Ordering is scoped to a partition; selecting a key does not make ordering global across the topic.

PublishOptions.metadata becomes UTF-8 Kafka headers. The adapter writes its own x-event-id and x-published-at after user metadata, so those values cannot be overridden. Consumers extract those fields and remove them from ctx.metadata; other headers remain available to the handler. Exact publish fields are in PublishOptions.

Redis Streams or Valkey ​

Choose Redis Streams when the team already operates Redis-compatible infrastructure and needs stream consumer groups without a separate broker stack.

typescript
import { RedisAdapter } from '@connectum/events-redis';

const adapter = RedisAdapter({ url: 'redis://localhost:6379' });

Redis protocol ​

The adapter connects with RESP2 by default, so upgrading ioredis does not change the wire protocol of an existing application. To use RESP3, set protocol: 3 in redisOptions:

typescript
const adapter = RedisAdapter({
  url: 'redis://localhost:6379',
  redisOptions: {
    protocol: 3,
    replyMapping: 'resp3',
  },
});

replyMapping chooses how ioredis returns RESP3 map replies: 'legacy' as flat arrays, 'resp3' as plain objects. The adapter reads stream replies in either shape. replyMapping: 'resp3' requires protocol: 3: with RESP2, explicit or default, RedisAdapter() throws a TypeError when it is called, before opening any connection. Omit replyMapping to keep the ioredis default, 'legacy'. The dedicated blocking connections used for XREADGROUP duplicate the main connection and use the same protocol and reply mapping.

Redelivery of pending entries ​

An entry that is not acknowledged stays in the consumer group's pending list: nack(true) or nack() leaves it there, and so does a handler that throws before the entry is settled. nack(false) acknowledges it, so it is not redelivered. The adapter claims pending entries that have been idle for 30 seconds (XAUTOCLAIM) and delivers them again with the delivery count from the group. Thirty seconds is the minimum idle eligibility, not a delivery deadline: claiming runs every fifth consume-loop iteration, so reads and handlers can add delay.

A handler failure on one redelivered entry is logged with the entry id and affects only that entry: it stays pending and is claimed again after another 30 seconds, while the other entries claimed in the same pass are still delivered. XAUTOCLAIM inspects a limited number of pending entries per call, so the adapter continues each pass where the previous one stopped; entries behind a long run of recently delivered ones are therefore reached too.

Redelivery has no upper bound of its own: the adapter has no equivalent of the NATS maxDeliver, and an entry that keeps failing without the retry and dlqmiddleware stays pending and is claimed again every 30 seconds. This is the standard Redis Streams behavior; the bound belongs to the middleware:

  • Retry plus DLQ. Configure retry and dlq on the bus. retry re-runs the handler inside the same delivery, so those runs do not raise the group's delivery count. When they are exhausted, dlq publishes a copy to the dead-letter topic and acknowledges the original, which leaves the pending list. A poison message with maxRetries: 2 runs the handler three times, produces one dead-letter entry (dlq.handler-runs = 3), and is not redelivered. If the dead-letter publish itself fails, the original is acknowledged without requeue and the failure is reported to the bus observer as dlq-failed.
  • A cap on redeliveries. For a bound on how often the broker hands the same entry out again, for example after handler crashes where no retry state survives, read event.attempt in a custom middleware. On a redelivered entry it carries the group's delivery count (1 on the first delivery, 2 on the first reclaim). The middleware can call ctx.ack() once the count passes your limit, instead of calling next().

Only a delivery that ends in a rethrown error, a crash or nack(true) leaves the entry pending; with retry and dlq configured the common failing handler never reaches the 30-second reclaim at all.

Recovery after Redis loses a consumer group ​

A Redis that restarts without persistence, runs FLUSHALL, or fails over to a replica that never had the stream no longer knows your consumer group. Redis answers the consume loop with NOGROUP. The adapter then creates the group again (XGROUP CREATE ... MKSTREAM) on every stream of the subscription and keeps consuming; no restart of the service is needed. publish() keeps working throughout, because XADD re-creates the stream.

The re-created group starts at the beginning of the stream (0), not at $. After a loss the stream holds what was published since, and $ would drop the entries published before the recovery ran. The price is at-least-once delivery at its widest: when only the group was lost and the stream kept its history, entries the group had already handled are delivered again, so handlers must tolerate duplicates. Each re-created group writes one [RedisAdapter] re-created consumer group ... warning and, when you set lifecycle.onLifecycle, produces a group-recreated event. With an AOF (appendonly yes) the group survives a restart and none of this happens.

Connection and option checks ​

brokerOptions.maxLen and count must be positive safe integers and blockMs a non-negative safe integer. RedisAdapter() throws a RangeError naming the option when a value is outside that range; maxLen: 0 is rejected because it would trim every published entry. blockMs: 0 is valid and blocks without a deadline.

connect() and subscribe() wait for the server to become ready for at most redisOptions.connectTimeout (ioredis default: 10000 ms). A server that accepts the TCP connection and never answers makes them reject with RedisAdapter: connection not ready within N ms (redisOptions.connectTimeout) instead of leaving bus.start() pending. Set connectTimeout: 0 to remove the deadline. See Redis adapter options if an existing service is affected.

Wildcards are not supported ​

Redis Streams has no pattern subscription. A topic containing * or > is rejected when the subscription is made, so bus.start() fails with RedisAdapter: wildcard pattern "..." is not supported. Redis Streams requires explicit topic names. Subscribe to each topic by its exact name, or use the NATS, Kafka or AMQP adapter when you need wildcard routing.

Stream names and retention ​

The topic orders.created maps to the Redis stream key events:orders.created. The adapter stores the protobuf payload as base64 alongside eventId, eventType, and publishedAt; user metadata is stored as meta:<name> fields and restored as ctx.metadata when consumed.

brokerOptions.maxLen adds approximate XADD MAXLEN ~ trimming. It bounds retention, not the number of pending deliveries: trimming can remove entries that a consumer has not processed, so choose it from your retention needs. It must be a positive integer (see Connection and option checks). A new consumer group is created at $ and starts with events published after its creation; a group that Redis lost and the adapter re-created starts at the beginning of the stream. Use the same explicit group across restarts to retain its consumption state; omitting group creates a new random group.

Entries written by other producers ​

The entry layout is the adapter's own, so writing to these streams with a different producer is not supported. An entry written by another producer without eventType, or naming another topic, is acknowledged and dropped: the event type it reports matches no route, and the EventBus acknowledges such a delivery without running any handler or middleware (see Unmatched events).

AMQP or RabbitMQ ​

Choose AMQP for exchange/queue topology, competing consumers, or integration with an externally governed AMQP contract.

typescript
import { AmqpAdapter } from '@connectum/events-amqp';

const adapter = AmqpAdapter({
  url: 'amqp://localhost:5672',
  exchange: 'events',
  exchangeType: 'topic',
});

Connection Lifecycle Events ​

The AMQP, NATS, Redis and Kafka adapters report their connection through lifecycle.onLifecycle, so you can alert on a lost or abandoned broker connection. The callback receives an object whose type field tells the event apart. The type strings and the shared fields are the same everywhere; the event types themselves are local to each package (NatsLifecycleEvent, RedisLifecycleEvent, KafkaLifecycleEvent, AmqpLifecycleEvent). The option is off unless you set it: without it no listener is attached to the broker client and nothing changes. The Memory adapter has no lifecycle option.

typescript
const adapter = NatsAdapter({
  servers: 'nats://localhost:4222',
  lifecycle: {
    onLifecycle: (event) => {
      if (event.type === 'reconnect-failed') alert('NATS connection abandoned', event.error);
    },
  },
});

Rules for the callback, the same for all adapters:

  • It should not throw. A thrown exception, or a returned promise that rejects, is isolated and reported once as a lifecycle-error event (callback, event, error); a failure while handling lifecycle-error itself is dropped. The broker clients run their listeners inside their own recovery code, so an unguarded exception there could stop the recovery or the subscription.
  • A returned promise is not awaited. A slow callback never delays disconnect() or the recovery.
  • Your own disconnect() and unsubscribe() report nothing as a loss.

The table below lists the NATS, Redis and Kafka events; the AMQP events are in AMQP reliability.

EventFieldsNATSRedisKafka
connectedreconnectedAfter the first connect (false) and after every recovery (true, with server)Once per connection on its first ready (false) and after every recovery (true)Once, when the producer has connected (false)
disconnectederror (NATS: server)Once per lossOnce per loss of a connection that had been ready, however many reconnect attempts failNot emitted
reconnectingattempt (NATS), attempt, delay, error (Redis)One per dial; the client gives no delay or errorOne per scheduled attempt; the count restarts on readyNot emitted
reconnect-failederrorTerminal: the client closed itself without your disconnect(), usually after its reconnect budget ran outTerminal: the client ended without your close, which needs a retryStrategy that returns no delayNot emitted
group-recreatedgroup, stream, connection—Redis lost a consumer group and the subscription created it again; once per group—
consumer-lost, consumer-restore-failed, consumer-restoredgroupId, error, willRestore / attempt, willRetry——A subscription's consumer crashed, crashed again before it came back, and joined its group again

NATS ​

reconnecting.attempt counts dials since the loss, one for each address the client tries, and resets on recovery. After reconnect-failed the adapter drops the dead connection: publish() rejects with "not connected" and connect() can start a new one. The client's own staleConnection, forceReconnect, error, ldm, update, ping and slowConsumer events produce nothing. Events of the initial dial that happen before connect() returns are not available from the client.

Redis ​

The adapter holds one publishing connection and one blocking connection for every subscription, so a single loss of the server is reported once per connection; connection says which: "publisher" or { group, patterns }. With N subscriptions that is N + 1 disconnected events. Nothing is reported before a connection's first ready (connect() rejects with the error itself). Setting onLifecycle attaches an error listener to every connection, which replaces the client's own [ioredis] Unhandled error event line on stderr: the cause is in the error field of the events instead.

group-recreated is specific to Redis. It reports a consumer group that Redis lost and the subscription created again (see Recovery after Redis loses a consumer group). stream is the stream key (events:<topic>) and connection is the { group, patterns } of the subscription. Without onLifecycle the adapter still writes one warning line per re-created group.

Kafka ​

The client library reports no loss of the producer's connection, and a consumer rides out a short broker outage without any event, so the adapter does not emit disconnected, reconnecting or reconnect-failed: it would have to guess them from failed sends. What the client does report is a consumer that gave up retrying and crashed. consumer-lost carries willRestore (whether the client restarts the consumer on its own), consumer-restore-failed a counted attempt for each crash before the consumer came back, and consumer-restored the first group join after a crash. A first join, rebalances, the restart that topic discovery causes and your own unsubscribe() produce nothing. When a wildcard subscription loses a matched topic, the crash is reported as consumer-lost and the replacement consumer's first group join as consumer-restored (see a topic is deleted).

Automatic Client Identification ​

When an adapter-specific client or connection name is not supplied, the EventBus derives a service identifier from registered proto service names and the host. An explicit adapter option always wins. Use explicit names when broker ACLs, dashboards, or support procedures depend on stable identifiers.

The derived name is {packages}@{hostname}. {packages} lists each distinct proto package once, joined with /, in the order the services were registered; the list is not sorted. Registering the same services in a different order therefore produces a different name, and with it a different Kafka clientId, NATS connection name, and Redis CLIENT SETNAME. A service whose type name has no proto package contributes its whole type name instead of a package.

Adapter Instances and Factories ​

createEventBus() takes an adapter instance. Construct it once at the composition root and pass it in. This is also how tests replace the broker: inject MemoryAdapter() or another configured test double instead of the production adapter.

typescript
import { createEventBus, type EventAdapter } from '@connectum/events';

export function createOrdersBus(adapter: EventAdapter) {
  return createEventBus({ adapter, routes: [orderEvents], group: 'orders-service' });
}

// Production: createOrdersBus(NatsAdapter({ servers: 'nats://localhost:4222', stream: 'orders' }))
// Tests:      createOrdersBus(MemoryAdapter())

Use an EventAdapterFactory -- a zero-argument function that returns a new adapter -- only when each consumer needs its own broker connection. Since 1.3.0 it is the named type of the adapter option of createBroadcastSubscribers, which accepts either one shared instance or a factory. With a factory, createBroadcastSubscribers calls it once per reactor when it builds the buses, so every reactor gets an independent connection and consumer group:

typescript
import { createBroadcastSubscribers, type EventAdapterFactory } from '@connectum/events';
import { NatsAdapter } from '@connectum/events-nats';

const newAdapter: EventAdapterFactory = () =>
  NatsAdapter({ servers: 'nats://localhost:4222', stream: 'orders' });

const buses = createBroadcastSubscribers({
  adapter: newAdapter,
  reactors: [
    { group: 'pricing', routes: [pricingRoutes] },
    { group: 'audit', routes: [auditRoutes] },
  ],
});

await Promise.all(buses.map((bus) => bus.start()));

The returned buses are not started, and you stop them yourself on shutdown. Two reactors with the same group make createBroadcastSubscribers throw, because a shared group load-balances events instead of delivering each event to every reactor. Passing one shared instance instead fits MemoryAdapter in tests: every bus then publishes and subscribes through the same in-memory adapter. Stop those buses together, because stopping any one of them disconnects the shared adapter and removes the subscriptions of all the others.

Custom Adapters ​

Implement the generated EventAdapter contract when a broker is not covered. Keep serialization, connection lifecycle, subscription cancellation, acknowledgement behavior, and error semantics explicit.

Next Steps ​