EventBus
API reference for EventBus
Class: EventBus
Section titled “Class: EventBus”EventBus client for managing subscriptions to an event store
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new EventBus(
http
,eventStoreId
):EventBus
Create a new EventBus client for a specific event store
Parameters
Section titled “Parameters”The HTTP client used for API requests
eventStoreId
Section titled “eventStoreId”string
The ID of the event store to manage subscriptions for
Returns
Section titled “Returns”EventBus
Methods
Section titled “Methods”getSubscription()
Section titled “getSubscription()”getSubscription(
subscriptionId
):Promise
<Subscription
>
Get details about a specific subscription
Parameters
Section titled “Parameters”subscriptionId
Section titled “subscriptionId”string
ID of the subscription to retrieve
Returns
Section titled “Returns”Promise
<Subscription
>
Subscription details
Example
Section titled “Example”const subscription = await eventBus.getSubscription('sub_123456');console.log(subscription.status); // 'ACTIVE'
listSubscriptions()
Section titled “listSubscriptions()”listSubscriptions(
options
):Promise
<ListSubscriptionsResponse
>
List all subscriptions for this event store
Parameters
Section titled “Parameters”options
Section titled “options”Optional filtering and pagination parameters
Returns
Section titled “Returns”Promise
<ListSubscriptionsResponse
>
List of subscriptions and total count
Example
Section titled “Example”// List all webhook subscriptionsconst { subscriptions, totalCount } = await eventBus.listSubscriptions({ subscriberType: SubscriberType.Webhook, limit: 20, offset: 0});
subscribe()
Section titled “subscribe()”subscribe(
options
):Promise
<Subscription
>
Subscribe to events from this event store
Parameters
Section titled “Parameters”options
Section titled “options”Configuration for the subscription
Returns
Section titled “Returns”Promise
<Subscription
>
The created subscription information
Throws
Section titled “Throws”When subscription configuration is invalid
Throws
Section titled “Throws”When the event store doesn’t exist
Throws
Section titled “Throws”When request validation fails
Throws
Section titled “Throws”When authentication fails
Example
Section titled “Example”import { isInvalidSubscriptionConfigError, isEventStoreNotFoundError} from '@delta-base/server';
try { const subscription = await eventBus.subscribe({ eventFilter: 'user.*', subscriber: { type: SubscriberType.Webhook, config: { url: 'https://example.com/webhook', headers: { 'X-API-Key': 'secret' }, retryPolicy: { maxAttempts: 3, backoffMinutes: 5 } } } });
// Check if this is an existing subscription if (subscription.isExistingSubscription) { console.log('Found existing subscription:', subscription.message); } else { console.log('Created new subscription:', subscription.message); }} catch (error) { if (isInvalidSubscriptionConfigError(error)) { console.log(`Invalid configuration: ${error.configError}`); // Handle invalid subscription config } else if (isEventStoreNotFoundError(error)) { console.log(`Event store '${error.eventStoreId}' not found`); // Handle missing event store } else { throw error; // Re-throw unknown errors }}
subscribeWebhook()
Section titled “subscribeWebhook()”subscribeWebhook(
eventFilter
,url
,options
):Promise
<Subscription
>
Create a webhook subscription
Parameters
Section titled “Parameters”eventFilter
Section titled “eventFilter”EventFilterPattern
Pattern determining which events to receive
string
The URL that will receive HTTP POST requests with events
options
Section titled “options”Omit
<WebhookConfig
, "url"
> & object
= {}
Additional configuration options
Returns
Section titled “Returns”Promise
<Subscription
>
The created subscription information
Example
Section titled “Example”// Subscribe to all user eventsconst subscription = await eventBus.subscribeWebhook( 'user.*', 'https://example.com/webhook', { headers: { 'X-API-Key': 'secret' }, retryPolicy: { maxAttempts: 3, backoffMinutes: 5 } });
// Check if this is an existing subscription with the same configurationif (subscription.isExistingSubscription) { console.log('Reusing existing subscription:', subscription.message);} else { console.log('Created new subscription:', subscription.message);}
unsubscribe()
Section titled “unsubscribe()”unsubscribe(
subscriptionId
):Promise
<{message
:string
;success
:boolean
; }>
Unsubscribe from events (delete a subscription)
Parameters
Section titled “Parameters”subscriptionId
Section titled “subscriptionId”string
ID of the subscription to delete
Returns
Section titled “Returns”Promise
<{ message
: string
; success
: boolean
; }>
Success message
Example
Section titled “Example”const result = await eventBus.unsubscribe('sub_123456');console.log(result.success); // true