# @purista/mqttbridge API

Package for using an MQTT broker as event bridge.

---
Canonical: /handbook/api/modules/_purista_mqttbridge/
Format: Markdown for agents
---

Package for using an MQTT broker as event bridge.

Package: `@purista/mqttbridge`

## Signature

```typescript
@purista/mqttbridge
```

## Examples

```typescript
import { MqttBridge } from '@purista/mqttbridge'

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

## Members

### Classes

- `class TopicRouter` — Local MQTT topic router keyed by subscription identifiers and topic filters.

### Type Aliases

- `type GetCommandResponseSubscriptionTopicFn = (this: IMqttBridge) => string` — Function signature for building this instance's MQTT command response topic filter.
- `type GetCommandTopicFn = (this: IMqttBridge, address: EBMessageAddress) => string` — Function signature for building an MQTT command subscription topic filter.
- `type GetSharedTopicNameFn = (this: IMqttBridge, topic: string) => string` — Function signature for wrapping an MQTT topic as a shared subscription.
- `type GetSubscriptionTopicFn = (this: IMqttBridge, subscription: Subscription) => string` — Function signature for building an MQTT subscription topic filter.
- `type GetTopicNameFn = (this: IMqttBridge, message: EBMessage) => string` — Function signature for building an MQTT publish topic from a PURISTA message.
- `type IMqttBridge = { client: MqttClient | undefined; pendingInvocations: PendingInvocationRegistry<unknown> } & EventBridgeBaseClass<MqttBridgeConfig>` — Internal receiver shape used by exported MQTT helper functions.
- `type IncomingMessageFunction = (this: IMqttBridge, payload: EBMessage, packet: IPublishPacket) => Promise<void>` — MQTT message handler bound to an IMqttBridge instance.
- `type MqttBridgeConfig = Prettify<{ allowRetries: boolean; defaultMessageExpiryInterval: number; defaultSessionExpiryInterval: number; emptyTopicPartString: string; qosCommand: QoS; qoSSubscription: QoS; ... } & IClientOptions>` — Configuration for MqttBridge.

### Variables

- `const getCommandResponseSubscriptionTopic: GetCommandResponseSubscriptionTopicFn` — Builds the MQTT topic filter used to receive command responses for the
current bridge instance.
- `const getCommandSubscriptionTopic: GetCommandTopicFn` — Builds the MQTT topic filter used by command handlers for one service
address.
- `const getSharedTopicName: GetSharedTopicNameFn` — Wraps a topic with the configured MQTT shared-subscription prefix and group.
- `const getSubscriptionTopic: GetSubscriptionTopicFn` — Builds the MQTT topic filter for a PURISTA subscription definition.
- `const getTopicName: GetTopicNameFn` — Calculates the MQTT topic name for a message which should be sent.
- `const handleCommandResponse: IncomingMessageFunction` — Handles MQTT command responses for pending invocations.
- `const puristaVersion: "3.0.1"` — The PURISTA package version embedded in the published runtime build.

### Functions

- `getCommandHandler(address: EBMessageAddress, cb: (message: { contentEncoding: string; contentType: string; correlationId: string; eventName: string; id: string; messageType: Command; ... }) => Promise<{ contentEncoding: "utf-8"; contentType: "application/json"; correlationId: string; eventName: string; id: string; isHandledError: boolean; ... } | { contentEncoding: string; contentType: string; correlationId: string; eventName: string; id: string; messageType: CommandSuccessResponse; ... }>, _metadata: CommandDefinitionMetadataBase, _eventBridgeConfig: DefinitionEventBridgeConfig): IncomingMessageFunction`
- `getDefaultMqttBridgeConfig(): { allowRetries: boolean; defaultMessageExpiryInterval: number; defaultSessionExpiryInterval: number; emptyTopicPartString: string; qosCommand: QoS; qoSSubscription: QoS; ... }`
- `getSubscriptionHandler(_subscription: Subscription, cb: (message: EBMessage) => Promise<Omit<{ contentEncoding: unknown; contentType: unknown; correlationId: unknown; eventName: unknown; id: unknown; messageType: unknown; ... }, unknown | unknown> | undefined>): IncomingMessageFunction`
- `isMatchingTopic(input: string, pattern: string): boolean`
- `msToSec(ms: number): number`

### Event bridge

- `class MqttBridge` — EventBridge implementation for MQTT 5 brokers.
