4.6.2. Emit Workflow and Service Events

In this chapter, you'll learn about event types and how to emit an event in a service or workflow.

Event Types#

In your customization, you can emit an event, then listen to it in a subscriber and perform an asynchronus action, such as send a notification or data to a third-party system.

There are two types of events in Medusa:

  1. Worflow event: an event that's emitted in a workflow after a commerce feature is performed. For example, Medusa emits the order.placed event after a cart is completed.
  2. Service event: an event that's emitted to track, trace, or debug processes under the hood. For example, you can emit an event with an audit trail.

Which Event Type to Use?#

Workflow events are the most common event type in development, as most custom features and customizations are built around workflows.

Some examples of workflow events:

  1. When a user creates a blog post and you're emitting an event to send a newsletter email.
  2. When you finish syncing products to a third-party system and you want to notify the admin user of new products added.
  3. When a customer purchases a digital product and you want to generate and send it to them.

You should only go for a service event if you're emitting an event for processes under the hood that don't directly affect front-facing features.

Some examples of service events:

  1. When you're tracing data manipulation and changes, and you want to track every time some custom data is changed.
  2. When you're syncing data with a search engine.

Emit Event in a Workflow#

To emit a workflow event, use the emitEventStep helper step provided in the @medusajs/medusa/core-flows package.

For example:

Code
1import { 2  createWorkflow,3} from "@medusajs/framework/workflows-sdk"4import {5  emitEventStep,6} from "@medusajs/medusa/core-flows"7
8const helloWorldWorkflow = createWorkflow(9  "hello-world",10  () => {11    // ...12
13    emitEventStep({14      eventName: "custom.created",15      data: {16        id: "123",17        // other data payload18      },19    })20  }21)

The emitEventStep accepts an object having the following properties:

  • eventName: The event's name.
  • data: The data payload as an object. You can pass any properties in the object, and subscribers listening to the event will receive this data in the event's payload.

In this example, you emit the event custom.created and pass in the data payload an ID property.

Test it Out#

If you execute the workflow, the event is emitted and you can see it in your application's logs.

Any subscribers listening to the event are executed.


Emit Event in a Service#

To emit a service event:

  1. Resolve event_bus from the module's container in your service's constructor:
  1. Use the event bus service's emit method in the service's methods to emit an event:
src/modules/hello/service.ts
1class HelloModuleService {2  // ...3  performAction() {4    // TODO perform action5
6    this.eventBusService_.emit({7      name: "custom.event",8      data: {9        id: "123",10        // other data payload11      },12    })13  }14}

The method accepts an object having the following properties:

  • name: The event's name.
  • data: The data payload as an object. You can pass any properties in the object, and subscribers listening to the event will receive this data in the event's payload.
  1. By default, the Event Module's service isn't injected into your module's container. To add it to the container, pass it in the module's registration object in medusa-config.ts in the dependencies property:
medusa-config.ts
1import { Modules } from "@medusajs/framework/utils"2
3module.exports = defineConfig({4  // ...5  modules: [6    {7      resolve: "./src/modules/hello",8      dependencies: [9        Modules.EVENT_BUS,10      ],11    },12  ],13})

The dependencies property accepts an array of module registration keys. The specified modules' main services are injected into the module's container.

That's how you can resolve it in your module's main service's constructor.

Test it Out#

If you execute the performAction method of your service, the event is emitted and you can see it in your application's logs.

Any subscribers listening to the event are also executed.

Was this chapter helpful?
Edit this page