Skip to main content

Inbound Event

Inbound events are deserialized representations of event messages that are consumed from the event bus. In the context of event-driven architectures, these events represent actions or changes that have occurred in the system, which other services or components need to react to.

Inbound events play a crucial role in the CQRS and event sourcing architecture by ensuring that the system can respond to changes that originate outside of the service’s own command or write operations. They help maintain consistency across microservices by allowing services to synchronize their states based on events from other services.

Implementation

Below is an implementation of an inbound event, found in the following file: Task/src/shared/application/inboundEvents/TaskAddedEventMessage.ts.

import { InboundEvent } from "@codebricks/codebricks-framework";

export interface TaskAddedEventMessageProperties {
streamName: string;
no: number;
id: string;
aggregateId: string;
aggregateVersion: number;
payload: TaskAddedEventMessagePayload;
occurredAt: Date;
}

export interface TaskAddedEventMessagePayload {
title: string;
description: string;
assigneeId: string;
}

export class TaskAddedEventMessage extends InboundEvent<TaskAddedEventMessagePayload> {
constructor(properties: TaskAddedEventMessageProperties) {
super({...properties, name: 'TaskAdded'});
}
}

Code Explanation

  • InboundEvent Class: The TaskAddedEventMessage class extends the InboundEvent class provided by the Codebricks framework. This base class likely provides common functionality for handling inbound events, such as validation, deserialization, and base properties.

  • TaskAddedEventMessageProperties Interface: This interface defines the structure of the data that makes up the TaskAddedEventMessage. It includes properties such as streamName, no, id, aggregateId, and aggregateVersion, which are essential for identifying and processing the event.

    • streamName: The name of the event stream this event belongs to.
    • no: The sequential number of the event within its stream.
    • id: A unique identifier for the event.
    • aggregateId: The ID of the aggregate (e.g., a specific task) that this event pertains to.
    • aggregateVersion: The version of the aggregate at the time this event was created.
    • payload: The actual data or changes introduced by the event, such as the task's title, description, and assigneeId.
    • occurredAt: The timestamp when the event occurred.
  • TaskAddedEventMessagePayload Interface: This interface defines the structure of the payload property, which contains the specific details of the task that was added.

  • Constructor: The constructor of TaskAddedEventMessage initializes the event by passing all necessary properties to the base InboundEvent class. The event name is hardcoded as 'TaskAdded', indicating the specific type of event.