Event System
Relevant source files
The following files were used as context for generating this wiki page:
The Budibase Event System is a decoupled, asynchronous messaging architecture built to handle various occurrences within the platform. It utilizes a publish-subscribe (pub/sub) model, where parts of the application can publish events without knowledge of which components will process them. This system is fundamental for features like auditing, real-time notifications, and triggering background tasks.
The core of the system relies on Amazon SQS (Simple Queue Service) for message queuing, ensuring that events are handled reliably and can be processed by different services, such as the main server or background workers. The system is composed of three main parts: Event definitions, Publishers, and Processors. Publishers create and send events, which are then queued and eventually consumed by registered Processors that execute specific logic based on the event type.
Architecture Overview
The event system follows a standard pub/sub pattern. A service or module uses a Publisher to create and send an event. The publishEvent function sends this event to an SQS queue. A background worker or another service polls this queue, retrieves the event, and passes it to the corresponding Processor for handling.
This diagram illustrates the high-level data flow of the event system.
Event Publishing
Events are published from various parts of the application to signal that a specific action has occurred. This is managed by publisher modules and the central publishEvent function.
Publishers
Publishers are collections of functions grouped by domain (e.g., user, app, role). Each function is responsible for constructing the payload for a specific event and calling publishEvent.
The main publisher index exports all available event publisher modules.
Sources: packages/backend-core/src/events/publishers/index.ts:1-10
A concrete example is the user publisher, which handles events related to user lifecycle and permissions.
// packages/backend-core/src/events/publishers/user.ts:15-24
async function created(user: User, timestamp?: number) {
const properties: UserCreatedEvent = {
userId: user._id as string,
viaScim: isScim(),
audited: {
email: user.email,
},
}
await publishEvent(Event.USER_CREATED, properties, timestamp)
}
Sources: packages/backend-core/src/events/publishers/user.ts:15-24
This sequence diagram shows the process of publishing a USER_CREATED event.
The publishEvent Function
This is the central function for sending all events to the SQS queue. It takes the event name, its properties, and an optional timestamp. It enriches the event with metadata like appId and tenantId from the current execution context before sending it.
// packages/backend-core/src/events/events.ts:25-46
export async function publishEvent<T extends EventProperties>(
name: Event,
properties: T,
timestamp?: number
) {
const ctx = context.get() || {}
const event: EventPayload<T> = {
name,
properties,
appId: ctx.appId,
tenantId: ctx.tenantId,
timestamp: timestamp || Date.now(),
}
// don't await, let it process in background
if (isCloud() && getQueue()) {
getQueue()
.send(JSON.stringify(event))
.catch(err => {
// fail silently
logs.error("Error publishing event to SQS", err)
})
}
}
Sources: packages/backend-core/src/events/events.ts:25-46
Event Processing
Once an event is published to the SQS queue, it is consumed and processed by a registered handler.
Processor Registry
The Processors class maintains a map of event names to their corresponding processor functions. Processors are registered using the register method.
| Class/Function | Description | File Path |
|---|---|---|
Processors.register() | Adds a processor function to the registry for a specific event name. | packages/backend-core/src/events/processors/Processors.ts |
Processors.process() | Finds and executes the appropriate processor for a given event payload. | packages/backend-core/src/events/processors/Processors.ts |
// packages/backend-core/src/events/processors/Processors.ts:10-12
static register(name: Event, processor: Processor) {
this.processors[name] = processor
}
Sources: packages/backend-core/src/events/processors/Processors.ts:10-12
The process method looks up the processor by the event name from the payload and executes it. If no processor is found, it logs an error.
// packages/backend-core/src/events/processors/Processors.ts:14-22
static async process(payload: EventPayload<any>) {
const processor = this.processors[payload.name]
if (processor) {
await processor(payload)
} else {
// this is a problem, but not a big one, just means an event
// was fired that this service doesn't know how to handle
logs.error(`Unable to find processor for event: ${payload.name}`)
}
}
Sources: packages/backend-core/src/events/processors/Processors.ts:14-22
Initialization
In the main server package, an init() function is responsible for registering all the necessary event processors. This function is called during server startup to ensure that the application is ready to handle incoming events from the queue.
The init function imports all processor definitions and registers them.
// packages/server/src/events/index.ts:10-15
import * as processors from "./processors"
export async function init() {
for (let processor of Object.values(processors)) {
processor.register()
}
}
Sources: packages/server/src/events/index.ts:10-15
This ensures that when the server starts, it is fully equipped to process any events that are consumed from the SQS queue.
Summary
The Budibase Event System provides a robust and scalable mechanism for handling asynchronous tasks and inter-service communication. By leveraging SQS, it decouples event producers (Publishers) from event consumers (Processors), allowing for a more resilient and maintainable architecture. This system is critical for logging actions, triggering automations, and performing other background activities that are essential to the platform's functionality.