Event Module

In this document, you'll learn what an Event Module is and how to use it in your Medusa application.

What is an Event Module?#

An Event Module implements the underlying publish/subscribe system that handles queueing events, emitting them, and executing their subscribers.

This makes the event architecture customizable, as you can either choose one of Medusa’s event modules or create your own.

NoteLearn more about Medusa's event systems in the Events and Subscribers documentation.

Default Event Module#

By default, Medusa uses the Local Event Module. This module uses Node’s EventEmitter to implement the publish/subscribe system. While this is suitable for development, it's recommended to use other Event Modules, such as the Redis Event Module, for production. You can also Create an Event Module.


How to Use the Event Module?#

You can use the registered Event Module as part of the workflows you build for your custom features. A workflow is a special function composed of a series of steps that guarantees data consistency and reliable roll-back mechanism.

Medusa provides the helper step emitEventStep that you can use in your workflow. You can also resolve the Event Module's service in a step of your workflow and use its methods to emit events.

For example:

Code
1import { Modules } from "@medusajs/framework/utils"2import { 3  createStep,4  createWorkflow,5} from "@medusajs/framework/workflows-sdk"6
7const step1 = createStep(8  "step-1",9  async ({}, { container }) => {10    const eventModuleService = container.resolve(11      Modules.EVENT12    )13
14    await eventModuleService.emit({15      name: "custom.event",16      data: {17        id: "123",18        // other data payload19      },20    })21  } 22)23
24export const workflow = createWorkflow(25  "workflow-1",26  () => {27    step1()28  }29)

In the example above, you create a workflow that has a step. In the step, you resolve the service of the Event Module from the Medusa container.

Then, you use the emit method of the Event Module to emit an event with the name "custom.event" and the data payload { id: "123" }.


List of Event Modules#

Medusa provides the following Event Modules. You can use one of them, or Create an Event Module.

Local
For Development
Redis
For Production
Was this page helpful?
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break