Skip to content

PURISTA API / Modules / @purista/natsbridge

Module: @purista/natsbridge

Package for using a NATS broker as event bridge.
The event bridge supports brokers with and without JetStream enabled.

If JetStream is enabled, subscriptions which are marked as durable are persisted by using JetStream.
If JetStream is not available, subscription fall back to live-subscriptions without any persistence.

Example

typescript
import { NatsBridge } from '@purista/natsbridge'

// create and init our eventbridge
  const eventBridge = new NatsBridge()
  await eventBridge.start()

Table of contents

Classes

Type Aliases

Variables

Functions

Type Aliases

IncomingMessageFunction

Ƭ IncomingMessageFunction: (this: NatsBridge, error: NatsError | null, msg: Msg) => Promise<void>

Type declaration

▸ (this, error, msg): Promise<void>

Parameters
NameType
thisNatsBridge
errorNatsError | null
msgMsg
Returns

Promise<void>

Defined in

natsbridge/src/types/IncomingMessageFunction.ts:5


NatsBridgeConfig

Ƭ NatsBridgeConfig: Prettify<{ commandResponsePublishTwice: "always" | "eventOnly" | "eventAndError" | "never" ; defaultMessageExpiryInterval: number ; emptyTopicPartString: string ; maxMessages: number ; topicPrefix: string } & ConnectionOptions>

the configuration for the NATS event bridge

Defined in

natsbridge/src/types/NatsBridgeConfig.ts:6

Variables

puristaVersion

Const puristaVersion: "1.11.0"

Defined in

natsbridge/src/version.ts:1

Functions

getCommandSubscriptionTopic

getCommandSubscriptionTopic(this, address): string

Parameters

NameType
thisNatsBridge
addressEBMessageAddress

Returns

string

Defined in

natsbridge/src/topic/getCommandSubscriptionTopic.impl.ts:8


getDefaultNatsBridgeConfig

getDefaultNatsBridgeConfig(): Object

Returns

Object

NameTypeDescription
commandResponsePublishTwice"always" | "eventOnly" | "eventAndError" | "never"Indicates if a command response should be published a second time. If the command response gets published, it will be published to the regular topic pattern. If set to never, subscription might not get messages they are expecting because of the timing. If set to always, every command response is published. Because there might not be a consumer for every message, the broker will store the messages until the defaultMessageExpiryInterval is reached. This might result in a high ressource consumption of the broker. If set to eventOnly, only success responses which have a event name set, are published twice. There, we expect, that an event has at least one consumer subscription and the broker does not unnecessarily stores messages for a long time. Default ts eventOnly
defaultMessageExpiryIntervalnumberthe message expiry interval in seconds Default ts 30 days in seconds
emptyTopicPartStringstringThe string which should be used in topics for parts, which are undefined Default ts __none__
maxMessagesnumbermaximum messages to run in parallel per subscription 10 means, each subscription can handle 10 calls at the same time Default ts 10
topicPrefixstringthe prefix for topic to prevent name collisions Default ts purista

Defined in

natsbridge/src/getDefaultNatsBridgeConfig.ts:4


getQueueGroupName

getQueueGroupName(prefix, address): string

Parameters

NameType
prefixstring
addressEBMessageAddress

Returns

string

Defined in

natsbridge/src/getQueueGroupName.impl.ts:3


getSubscriptionTopic

getSubscriptionTopic(this, subscription): string

Parameters

NameType
thisNatsBridge
subscriptionSubscription

Returns

string

Defined in

natsbridge/src/topic/getSubscriptionTopic.impl.ts:8


getTopicName

getTopicName(this, message): string

Calculates the NATS topic name for a message which should be sent. Something like: purista/message_type/instance_id/sender_name/sender_version/sender_target/eventname/receiver_name/receiver_version/receiver_target

Parameters

NameTypeDescription
thisNatsBridge-
messageEBMessagethe message to send

Returns

string

the NATS topic

Defined in

natsbridge/src/topic/getTopicName.impl.ts:18