|
1
|
import { CardList } from "docs-ui" |
|
2
|
|
|
3
|
export const metadata = { |
|
4
|
title: `Event Module`, |
|
5
|
} |
|
6
|
|
|
7
|
# {metadata.title} |
|
8
|
|
|
9
|
In this document, you'll learn what an Event Module is and how to use it in your Medusa application. |
|
10
|
|
|
11
|
## What is an Event Module? |
|
12
|
|
|
13
|
An Event Module implements the underlying publish/subscribe system that handles queueing events, emitting them, and executing their subscribers. |
|
14
|
|
|
15
|
This makes the event architecture customizable, as you can either choose one of Medusa’s event modules or create your own. |
|
16
|
|
|
17
|
<Note> |
|
18
|
|
|
19
|
Learn more about Medusa's event systems in the [Events and Subscribers documentation](!docs!/learn/fundamentals/events-and-subscribers). |
|
20
|
|
|
21
|
</Note> |
|
22
|
|
|
23
|
### Default Event Module |
|
24
|
|
|
25
|
By default, Medusa uses the [Local Event Module](./local/page.mdx). 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](./redis/page.mdx), for production. You can also [Create an Event Module](./create/page.mdx). |
|
26
|
|
|
27
|
--- |
|
28
|
|
|
29
|
## How to Use the Event Module? |
|
30
|
|
|
31
|
You can use the registered Event Module as part of the [workflows](!docs!/learn/fundamentals/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. |
|
32
|
|
|
33
|
Medusa provides the helper step [emitEventStep](/references/helper-steps/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. |
|
34
|
|
|
35
|
For example: |
|
36
|
|
|
37
|
```ts |
|
38
|
import { Modules } from "@medusajs/framework/utils" |
|
39
|
import { |
|
40
|
createStep, |
|
41
|
createWorkflow, |
|
42
|
} from "@medusajs/framework/workflows-sdk" |
|
43
|
|
|
44
|
const step1 = createStep( |
|
45
|
"step-1", |
|
46
|
async ({}, { container }) => { |
|
47
|
const eventModuleService = container.resolve( |
|
48
|
Modules.EVENT_BUS |
|
49
|
) |
|
50
|
|
|
51
|
await eventModuleService.emit({ |
|
52
|
name: "custom.event", |
|
53
|
data: { |
|
54
|
id: "123", |
|
55
|
// other data payload |
|
56
|
}, |
|
57
|
}) |
|
58
|
} |
|
59
|
) |
|
60
|
|
|
61
|
export const workflow = createWorkflow( |
|
62
|
"workflow-1", |
|
63
|
() => { |
|
64
|
step1() |
|
65
|
} |
|
66
|
) |
|
67
|
``` |
|
68
|
|
|
69
|
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](!docs!/learn/fundamentals/medusa-container). |
|
70
|
|
|
71
|
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" }`. |
|
72
|
|
|
73
|
--- |
|
74
|
|
|
75
|
## List of Event Modules |
|
76
|
|
|
77
|
Medusa provides the following Event Modules. You can use one of them, or [Create an Event Module](./create/page.mdx). |
|
78
|
|
|
79
|
<CardList |
|
80
|
items={[ |
|
81
|
{ |
|
82
|
title: "Local", |
|
83
|
href: "/infrastructure-modules/event/local", |
|
84
|
badge: { |
|
85
|
variant: "neutral", |
|
86
|
children: "For Development" |
|
87
|
} |
|
88
|
}, |
|
89
|
{ |
|
90
|
title: "Redis", |
|
91
|
href: "/infrastructure-modules/event/redis", |
|
92
|
badge: { |
|
93
|
variant: "green", |
|
94
|
children: "For Production" |
|
95
|
} |
|
96
|
} |
|
97
|
]} |
|
98
|
/> |