Messaging
Streaming
Declare {subject, streaming: true} in the broker’s publishers() method and inject
@Streamer({subject}) as MessagingStreamer. Both Kafka and NATS support individual
values, arrays, and RxJS Observables, with ordered publishing, lifecycle callbacks,
and cancellation. Closing the application cancels sources and awaits accepted sends.
NATS initializes JetStream when streaming is first requested. Streaming subscribers
can initialize resources without a local publisher. Existing stream and consumer
names and work-queue retention are preserved; incompatible resources fail setup
without deleting data. Handlers are acknowledged after successful completion;
failed or cancelled execution remains eligible for redelivery. Handlers must therefore
be idempotent. The subscription’s batch option controls the pull batch size.
Broker-bound types and decorators
Declare each broker with @MessagingAdapter and register its class in the module’s
transporters. Decorating a class
does not open a connection; setup callbacks run during application registration.
// orders.broker.ts
import {MessagingAdapter, Transporter, type BrokerPattern} from '@heronjs/ebs';
@MessagingAdapter({transporter: Transporter.Nats, servers: ['nats://localhost:4222']})
export class OrdersBroker {
publishers(): BrokerPattern<Transporter.Nats>[] {
return [{subject: 'orders'}, {subject: 'order-events', streaming: true}];
}
}
@MessagingAdapter({transporter: Transporter.Nats, servers: ['nats://audit:4222']})
export class AuditBroker {
publishers(): BrokerPattern<Transporter.Nats>[] {
return [{subject: 'orders'}];
}
}// orders.service.ts
import {MessagingService, Subscribe, Publisher, Streamer, Payload, Transporter, type MessagePayload, type MessagingPublisher, type MessagingStreamer} from '@heronjs/ebs';
import {OrdersBroker} from './orders.broker.js';
type Order = {id: string};
@MessagingService({adapter: OrdersBroker})
export class OrdersService {
constructor(
@Publisher({subject: 'orders', options: OrdersBroker}) private readonly publisher: MessagingPublisher<Order, Transporter.Nats>,
@Streamer({subject: 'order-events', options: OrdersBroker}) private readonly streamer: MessagingStreamer<Order, Transporter.Nats>,
) {}
@Subscribe({adapter: OrdersBroker, subject: 'orders', configure: () => ({queue: 'order-workers'})})
handle(@Payload() message: MessagePayload) {
// message.data contains the broker payload bytes; decode and validate before use.
}
async send(order: Order) {
await this.publisher.publish({payload: [order]});
await this.streamer.stream({payload: order, configure: () => ({msgID: order.id})});
}
}Register both classes with transporters: [OrdersBroker, AuditBroker] and the service
with providers: [OrdersService]. Selecting the adapter class keeps equal subjects
on distinct brokers separate. Publisher and streamer bindings are also separate.
Dependencies are lazy until messaging activation; sending from constructors or
early provider initialization is not supported.
Within an application, the framework generates a broker ID from the transporter
and a SHA-256 hash of the normalized server URI set. Endpoint ordering and duplicate
URIs do not affect identity. Multiple decorated classes targeting the same broker
share initialization, publishers and cleanup, and contribute their own handlers.
The first declaration supplies connection and duplicate publisher options; changing
clientId alone does not create another broker. AMQP virtual hosts form part of the
URI identity. Each application owns its connections independently.
For Kafka, use @MessagingAdapter({transporter: Transporter.Kafka, brokers, clientId}).
Subscriptions select the class and transport:
@Subscribe({adapter: KafkaBroker, transporter: Transporter.Kafka, subject: 'orders', configure: () => ({'group.id': 'workers'})}).
NATS subscriptions default to Transporter.Nats; Kafka and AMQP require an explicit
transporter for their SDK option types. Publisher patterns accept producer/topic
configuration; message calls accept key/header configuration. Advanced connection
configuration uses
@MessagingAdapter({transporter: Transporter.Kafka, configure: () => ({'bootstrap.servers': 'localhost:9092'})}).
The existing callback shape returning {transporter, opts} remains supported.
NATS streaming: true subscriptions accept pull-consumer options; explicit
max_messages overrides the subscription’s batch default. AMQP supports bound
queue publishers/subscribers, not streaming or exchange routing. Native callbacks
remain framework-owned. Setup callbacks run during registration; message callbacks
run once per publish envelope or stream invocation.
Transport-only services and subject-only injections remain available when the broker
selection is unambiguous. Select adapter classes explicitly when using multiple
brokers of the same transport or equal subjects on different brokers. Payload
annotations remain explicit; use the transport enum as the second type parameter
of MessagingPublisher and MessagingStreamer to type native message options.
Kafka message headers
Kafka MessagePayload handlers receive headers alongside the unchanged payload in data. Header values retain their original string, Buffer, or repeated-value form; headers is absent when the message has none. Validate header values before using fields such as x-type for event routing. Subscribe.configure continues to configure the consumer.
Framework stream logging callbacks receive {message, data} since 4.6.0. Native broker SDK callbacks retain their SDK signatures.