Skip to content

PURISTA API / Modules / @purista/dapr-sdk / DaprEventBridge

Class: DaprEventBridge

@purista/dapr-sdk.DaprEventBridge

The DaprEventBridge connects to the Dapr sidecar container. It provides endpoints for invoking commands, triggering subscriptions and emitting event messages. The sidecar container invokes commands and subscriptions of the service connected to the event bridge. A DaprClient (http fetch) is used for communication from the service/event bridge to the sidecar container.

Names for services, commands, subscriptions and events are converted to kebab-case. If the event bridge is configured to expose REST endpoints defined in command builder, the endpoints are generated as defined in the command builder.

The event bridge is using Hono under the hood. You need to provide a serve function. Depending on your runtime (Node, Bun, Deno) an adapter might be needed.

See

Hono website

Example

typescript
import { DaprConfigStore, DaprEventBridge, DaprSecretStore, DaprStateStore } from '@purista/dapr-sdk'

const eventBridge = new DaprEventBridge({
   serve,
 })

// start the services first ...

await eventBridge.start()

Hierarchy

Implements

Table of contents

Constructors

Properties

Methods

Constructors

constructor

new DaprEventBridge(config): DaprEventBridge

Parameters

NameTypeDescription
configObject-
config.apiPrefix?stringthe prefix to be used if the command is configured as REST api endpoint according to the OpenAPI defintion needs to enableRestApiExpose set to true Default ts /api
config.clientConfig?DaprClientConfig-
config.commandPayloadAsCloudEvent?booleancommand invocations are wrapped in CloudEvent Link https://github.com/cloudevents/spec/tree/v1.0 Default ts false
config.defaultCommandTimeout?numberOverwrite the hardcoded default timeout of command invocations
config.enableHttpCompression?booleanenable HTTP compression in web server Default ts true
config.enableRestApiExpose?booleanexpose commands as regular REST endpoints when they are configured as endpoints Default ts true
config.instanceId?stringThe instance id of the event bridge. If not set, a id will generated each time a instance is created. Use this if there is a need to always have the same instance id.
config.logLevel?LogLevelNameIf no logger instance is given, use this log level
config.logger?LoggerA logger instance
config.name?stringname of the bridge Default ts HttpEventBridge
config.pathPrefix?stringthe prefix to be used for exposing commands as endpoints expecting a event bus message Default ts purista
config.serve(options: { fetch: (request: Request) => unknown ; hostname?: string ; port?: number }) => Server<typeof IncomingMessage, typeof ServerResponse> | Http2Server | Http2SecureServerThe serve function is depending on the runtime. - Bun: Bun.serve - Node.js: serve function from additional package @hono/hono-node-server - Deno: serve function from package https://deno.land/std/http/server.ts See https://hono.dev
config.serverHost?stringHost of the server. Default ts 127.0.0.1
config.serverPort?numberPort of the server. Default ts 8080
config.spanProcessor?SpanProcessorA OpenTelemetry span processor
config.subscriptionPayloadAsCloudEvent?booleansubscription invocations are wrapped in CloudEvent Link https://github.com/cloudevents/spec/tree/v1.0 Default ts false

Returns

DaprEventBridge

Overrides

HttpEventBridge.constructor

Defined in

dapr-sdk/src/DaprEventBridge/DaprEventBridge.impl.ts:45

Properties

app

app: Hono<Env, BlankSchema, "/">

Inherited from

HttpEventBridge.app

Defined in

base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:26


client

client: HttpEventBridgeClient

Inherited from

HttpEventBridge.client

Defined in

base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:29


config

config: Complete<{ apiPrefix?: string ; clientConfig?: DaprClientConfig ; commandPayloadAsCloudEvent?: boolean ; defaultCommandTimeout?: number ; enableHttpCompression?: boolean ; enableRestApiExpose?: boolean ; instanceId?: string ; logLevel?: LogLevelName ; logger?: Logger ; name?: string ; pathPrefix?: string ; serve: (options: { fetch: (request: Request) => unknown ; hostname?: string ; port?: number }) => Server<typeof IncomingMessage, typeof ServerResponse> | Http2Server | Http2SecureServer ; serverHost?: string ; serverPort?: number ; spanProcessor?: SpanProcessor ; subscriptionPayloadAsCloudEvent?: boolean }>

Inherited from

HttpEventBridge.config

Defined in

core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:14


defaultCommandTimeout

defaultCommandTimeout: number

The default time until when a command invocation automatically returns a time out error

Implementation of

EventBridge.defaultCommandTimeout

Inherited from

HttpEventBridge.defaultCommandTimeout

Defined in

core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:17


instanceId

instanceId: string

Implementation of

EventBridge.instanceId

Inherited from

HttpEventBridge.instanceId

Defined in

core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:16


isShuttingDown

isShuttingDown: boolean

Inherited from

HttpEventBridge.isShuttingDown

Defined in

base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:27


isStarted

isStarted: boolean

Inherited from

HttpEventBridge.isStarted

Defined in

base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:28


logger

logger: Logger

Inherited from

HttpEventBridge.logger

Defined in

core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:12


name

name: string

Implementation of

EventBridge.name

Inherited from

HttpEventBridge.name

Defined in

core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:15


pubSubSubscriptions

Private pubSubSubscriptions: DaprPubSubType[] = []

Defined in

dapr-sdk/src/DaprEventBridge/DaprEventBridge.impl.ts:43


server

server: undefined | Server<typeof IncomingMessage, typeof ServerResponse> | Http2Server | Http2SecureServer

Inherited from

HttpEventBridge.server

Defined in

base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:25


traceProvider

traceProvider: NodeTracerProvider

Inherited from

HttpEventBridge.traceProvider

Defined in

core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:13

Methods

destroy

destroy(): Promise<void>

Shut down event bridge as gracefully as possible

Returns

Promise<void>

Implementation of

EventBridge.destroy

Inherited from

HttpEventBridge.destroy

Defined in

base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:43


emit

emit<K>(eventName, parameter?): void

Type parameters

NameType
Kextends EventKey<{ eventbridge-connected: never ; eventbridge-connection-error: unknown ; eventbridge-disconnected: never ; eventbridge-error: unknown ; eventbridge-reconnecting: never }>

Parameters

NameType
eventNameK
parameter?{ eventbridge-connected: never ; eventbridge-connection-error: unknown ; eventbridge-disconnected: never ; eventbridge-error: unknown ; eventbridge-reconnecting: never }[K]

Returns

void

Inherited from

HttpEventBridge.emit

Defined in

core/dist/commonjs/core/types/GenericEventEmitter.d.ts:13


emitMessage

emitMessage<T>(message): Promise<Readonly<EBMessage>>

Emit a message to the eventbridge without awaiting a result

Type parameters

NameType
Textends EBMessage

Parameters

NameTypeDescription
messageOmit<EBMessage, "id" | "timestamp" | "correlationId">the message

Returns

Promise<Readonly<EBMessage>>

Implementation of

EventBridge.emitMessage

Inherited from

HttpEventBridge.emitMessage

Defined in

base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:32


getTracer

getTracer(): Tracer

Returns open telemetry tracer of this service

Returns

Tracer

Tracer

Inherited from

HttpEventBridge.getTracer

Defined in

core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:24


invoke

invoke<T>(input, ttl?): Promise<T>

Call a command of a service and return the result of this command

Type parameters

Name
T

Parameters

NameTypeDescription
inputOmit<{ contentEncoding: string ; contentType: string ; correlationId: string ; eventName?: string ; id: string ; messageType: Command ; otp?: string ; payload: { parameter: unknown ; payload: unknown } ; principalId?: string ; receiver: EBMessageAddress ; sender: { instanceId: string ; serviceName: string ; serviceTarget: string ; serviceVersion: string } ; tenantId?: string ; timestamp: number ; traceId?: string }, "id" | "timestamp" | "correlationId" | "messageType">a partial command message
ttl?numberthe time to live (timeout) of the invocation

Returns

Promise<T>

Implementation of

EventBridge.invoke

Inherited from

HttpEventBridge.invoke

Defined in

base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:33


isHealthy

isHealthy(): Promise<boolean>

Indicates if the eventbridge is running and works correctly

Returns

Promise<boolean>

Implementation of

EventBridge.isHealthy

Inherited from

HttpEventBridge.isHealthy

Defined in

base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:39


isReady

isReady(): Promise<boolean>

Indicates if the eventbridge has been started and is connected to underlaying message broker

Returns

Promise<boolean>

Implementation of

EventBridge.isReady

Inherited from

HttpEventBridge.isReady

Defined in

base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:38


off

off<K>(eventName, fn): void

Type parameters

NameType
Kextends EventKey<{ eventbridge-connected: never ; eventbridge-connection-error: unknown ; eventbridge-disconnected: never ; eventbridge-error: unknown ; eventbridge-reconnecting: never }>

Parameters

NameType
eventNameK
fnEventReceiver<{ eventbridge-connected: never ; eventbridge-connection-error: unknown ; eventbridge-disconnected: never ; eventbridge-error: unknown ; eventbridge-reconnecting: never }[K]>

Returns

void

Inherited from

HttpEventBridge.off

Defined in

core/dist/commonjs/core/types/GenericEventEmitter.d.ts:12


on

on<K>(eventName, fn): void

Type parameters

NameType
Kextends EventKey<{ eventbridge-connected: never ; eventbridge-connection-error: unknown ; eventbridge-disconnected: never ; eventbridge-error: unknown ; eventbridge-reconnecting: never }>

Parameters

NameType
eventNameK
fnEventReceiver<{ eventbridge-connected: never ; eventbridge-connection-error: unknown ; eventbridge-disconnected: never ; eventbridge-error: unknown ; eventbridge-reconnecting: never }[K]>

Returns

void

Inherited from

HttpEventBridge.on

Defined in

core/dist/commonjs/core/types/GenericEventEmitter.d.ts:11


registerCommand

registerCommand(address, cb, metadata, eventBridgeConfig): Promise<string>

Parameters

NameType
addressEBMessageAddress
cb(message: { contentEncoding: string ; contentType: string ; correlationId: string ; eventName?: string ; id: string ; messageType: Command ; otp?: string ; payload: { parameter: unknown ; payload: unknown } ; principalId?: string ; receiver: EBMessageAddress ; sender: { instanceId: string ; serviceName: string ; serviceTarget: string ; serviceVersion: string } ; tenantId?: string ; timestamp: number ; traceId?: string }) => Promise<Readonly<Omit<{ contentEncoding: string ; contentType: string ; correlationId: string ; eventName?: string ; id: string ; messageType: CommandSuccessResponse ; otp?: string ; payload: unknown ; principalId?: string ; receiver: { instanceId: string ; serviceName: string ; serviceTarget: string ; serviceVersion: string } ; sender: { instanceId: string ; serviceName: string ; serviceTarget: string ; serviceVersion: string } ; tenantId?: string ; timestamp: number ; traceId?: string }, "instanceId">> | Readonly<Omit<{ contentEncoding: "utf-8" ; contentType: "application/json" ; correlationId: string ; eventName?: string ; id: string ; isHandledError: boolean ; messageType: CommandErrorResponse ; otp?: string ; payload: { data?: unknown ; message: string ; status: StatusCode } ; principalId?: string ; receiver: { instanceId: string ; serviceName: string ; serviceTarget: string ; serviceVersion: string } ; sender: { instanceId: string ; serviceName: string ; serviceTarget: string ; serviceVersion: string } ; tenantId?: string ; timestamp: number ; traceId?: string }, "instanceId">>>
metadataObject
metadata.expose{ contentEncodingRequest?: string ; contentEncodingResponse?: string ; contentTypeRequest?: string ; contentTypeResponse?: string ; deprecated?: boolean ; inputPayload?: SchemaObject ; outputPayload?: SchemaObject ; parameter?: SchemaObject } & { http: { method: "POST" | "GET" | "PATCH" | "PUT" | "DELETE" ; openApi?: { additionalStatusCodes?: StatusCode[] ; description: string ; isSecure: boolean ; operationId?: string ; query?: QueryParameter<{}>[] ; summary: string ; tags?: string[] } ; path: string } }
eventBridgeConfigDefinitionEventBridgeConfig

Returns

Promise<string>

Implementation of

EventBridge.registerCommand

Inherited from

HttpEventBridge.registerCommand

Defined in

base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:34


registerSubscription

registerSubscription(subscription, cb): Promise<string>

Register a new subscription

Parameters

NameTypeDescription
subscriptionSubscriptionthe subscription definition
cb(message: EBMessage) => Promise<undefined | Omit<{ contentEncoding: string ; contentType: string ; correlationId?: string ; eventName: string ; id: string ; messageType: CustomMessage ; otp?: string ; payload?: unknown ; principalId?: string ; receiver?: EBMessageAddress ; sender: { instanceId: string ; serviceName: string ; serviceTarget: string ; serviceVersion: string } ; tenantId?: string ; timestamp: number ; traceId?: string }, "id" | "timestamp">>the function to be called if a matching message arrives

Returns

Promise<string>

Implementation of

EventBridge.registerSubscription

Overrides

HttpEventBridge.registerSubscription

Defined in

dapr-sdk/src/DaprEventBridge/DaprEventBridge.impl.ts:97


removeAllListeners

removeAllListeners(): void

Returns

void

Inherited from

HttpEventBridge.removeAllListeners

Defined in

core/dist/commonjs/core/types/GenericEventEmitter.d.ts:14


start

start(): Promise<void>

Start the eventbridge and connect to the underlaying message broker

Returns

Promise<void>

Implementation of

EventBridge.start

Overrides

HttpEventBridge.start

Defined in

dapr-sdk/src/DaprEventBridge/DaprEventBridge.impl.ts:80


startActiveSpan

startActiveSpan<F>(name, opts, context, fn): Promise<F>

Start a child span for opentelemetry tracking

Type parameters

Name
F

Parameters

NameTypeDescription
namestringname of span
optsSpanOptionsspan options
contextundefined | Contextoptional context
fn(span: Span) => Promise<F>function to be executed within the span

Returns

Promise<F>

return value of fn

Inherited from

HttpEventBridge.startActiveSpan

Defined in

core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:33


unregisterCommand

unregisterCommand(address): Promise<void>

Unregister a service command

Parameters

NameTypeDescription
addressEBMessageAddressThe address (service name, version and command name) of the command to be de-registered

Returns

Promise<void>

Implementation of

EventBridge.unregisterCommand

Inherited from

HttpEventBridge.unregisterCommand

Defined in

base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:35


unregisterSubscription

unregisterSubscription(address): Promise<void>

Parameters

NameType
addressEBMessageAddress

Returns

Promise<void>

Implementation of

EventBridge.unregisterSubscription

Inherited from

HttpEventBridge.unregisterSubscription

Defined in

base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:37


wrapInSpan

wrapInSpan<F>(name, opts, fn, context?): Promise<F>

Start span for opentelemetry tracking on same level. The created span will not become the "active" span within opentelemetry!

This means during logging and similar the spanId of parent span is logged.

Use wrapInSpan for marking points in flow of one bigger function, but not to trace the program flow itself

Type parameters

Name
F

Parameters

NameTypeDescription
namestringname of span
optsSpanOptionsspan options
fn(span: Span) => Promise<F>function te be executed in the span
context?Contextspan context

Returns

Promise<F>

return value of fn

Inherited from

HttpEventBridge.wrapInSpan

Defined in

core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:49