Subscribers
In this document, you'll learn what Subscribers are in Medusa.
What are Subscribers
Subscribers register handlers for an events and allows you to perform an action when that event occurs. For example, if you want to send your customer an email when they place an order, then you can listen to the order.placed
event and send the email when the event is emitted.
Natively in Medusa there are subscribers to handle different events. However, you can also create your own custom subscribers.
Custom subscribers are TypeScript or JavaScript files in your project's src/subscribers
directory. Subscriber files must default export a handler function and export a configuration object.
Whenever an event is emitted, the subscriber’s handler function is executed. The handler function receives as a parameter an object that includes the data payload, among other parameters. For example, if an order is placed, the order.placed
event is emitted and all the handlers receive the order ID in the data
object.
Example Use Cases
Subscribers are useful in many use cases, including:
- Send a confirmation email to the customer when they place an order by subscribing to the
order.placed
event. - Automatically assign new customers to a customer group by subscribing to the
customer.created
. - Handle custom events that you emit
Custom Development
Developers can create custom subscribers in the Medusa backend, a plugin, or in a module.
Learn how to create a subscriber in Medusa.