Skip to main content

NATS event bridge

General

The NATS message brokeropen in new window is a fast and scalable message broker.

PURISTA provides the @purista/natsbridge

Pros

  • allows scaling
  • fault tollerant
  • can be used with NATS State store (@purista/nats-state-store)
  • can be used with NATS Config store (@purista/nats-config-store)

Cons

  • needs managing of an NATS broker
  • no persistance of messages available
  • hard to handle dead letters

Config

The NATS event bridge uses the unified configuration schema as all event bridges.

Example

import { NatsBridge } from '@purista/natsbridge'

const eventBridge = new NatsBridge()
await eventBridge.start()

Topic names

The NATS protocol relays on topics for message publishing/subscribe.

PURISTA is using the following schema for topics:

const topicPrefix = config.topicPrefix
const empty = config.emptyTopicPartString

const path join(
  this.config.topicPrefix,
  convertToSnakeCase(message.messageType),
  convertToSnakeCase(message.principalId || empty),
  convertToSnakeCase(message.tenantId || empty),
  convertToSnakeCase(message.sender.instanceId || empty),
  convertToSnakeCase(message.sender.serviceName),
  convertToSnakeCase(message.sender.serviceVersion),
  convertToSnakeCase(message.sender.serviceTarget),
  convertToSnakeCase(message.eventName || empty),
  convertToSnakeCase((message as Command).receiver?.instanceId || empty),
  convertToSnakeCase((message as Command).receiver?.serviceName || empty),
  convertToSnakeCase((message as Command).receiver?.serviceVersion || empty),
  convertToSnakeCase((message as Command).receiver?.serviceTarget || empty),
)

This allows to have subscriptions for very specific messages.
The NATS event bridge does not use the available request-response pattern of NATS to be able to use the unified topic schema.
Otherwise, there would be the need to duplicate command response to be available for subscriptions.

Hints

Ensure settings across instances

Remember to ensure, prefixes, and so on are the same on every event bridge instance.
Otherwise, you might get some unexpected behaviors.

OpenTelemetry

PURISTA is using the NATS header feature to add the OpenTelemetry information to each message, as recommended:
https://w3c.github.io/trace-context-mqtt/open in new window.

You can follow updated on Twitter @purista_jsopen in new window or join the Discord serveropen in new window to get in touch with PURISTA maintainers and other developers.

Last update:
Contributors: Sebastian Wessel