Publish Events

Using the Business Event Support extension, additional code will be generated, which allows to easily publish an event.

Event factory

For creating an event, the event factory can be used in commands, services and agents, which have events associated. The return value of the factory methods are objects, which e.g. provide methods to set the payload and publish the event.

The code below shows and example of how to create a new event object by using the factory:

// Create an event using event factory
const event1 = this.factory.event.nsacrnm.EventIdentifier();
 
// Set event payload
event1.payload = payload;
 
// publish event
await event1.publish();

Publish events

For each event, the payload, a custom message key and custom headers can be set. At publishing, the event is published to the Kafka topic, which was specified at the event while modelling.

Note:

When setting custom message headers for schema type events, the header key 'apicurio.value.globalId' is reserved and will be overwritten by the sdk, as it is used for integration purposes.

The code below shows how to define a payload for the event (using a schema from the schema registry), set a custom message key and custom headers:

import { schemaRegistry } from 'solution-framework';

  /**
   * Event publishing in Command or Service
   */
  public async execute(): Promise<void> {

    // create a new event instance
    const event = this.factory.event.domainNs.OrderAccepted();

    // Create payload object using the type defined in schemaRegistry
    const payload: schemaRegistry.SchemaName = {
      property1: 'val'
    }
    event.payload = payload;

    // set message key (optional)
    event.messageKey = 'customMessageKey';
    
    // set message headers (optional)
    event.messageHeaders = {
      'headerKey': 'headerValue',
    }

    // Publish the event
    await event.publish();
  }
Warning:

Using time property in an entity as event payload is not compatible with Java events using properties of type time due to different implementation between Node.js and Java stacks. Consider migrating to schema payloads for events.

Publish event using entity as payload (deprecated)

An event using an entity as payload is published by using the structure of the following code, however this is deprecated and it is recommended to use schemas from the schema registry instead.

In this case, it is allowed to set custom message headers for the event, but no custom message key is allowed.

  /**
   * Event publishing in Command or Service
   */
  public async execute(): Promise<void> {

    // create a new event instance
    const event = this.factory.event.domainNs.OrderAccepted();

    // Create payload entity for event and set values
    event.payload = this.factory.entity.domainNs.OrderAccepted_Payload();
    payload.property1 = value1;
    
    // set message headers (optional)
    event.messageHeaders = {
      'headerKey': 'headerValue',
    }

    // Publish the event
    await event.publish();
  }