We care deeply about developer experience across all our products. Our primitives and tooling allow developers to effortlessly build digital commerce applications at record speed.
Today, we are excited to announce a massive update to our Subscriber API that significantly improves the developer experience of a core concept in Medusa’s customization toolbox.
Subscribers
Services in Medusa emit events whenever actions like creating and updating records occur. Subscribers in Medusa provide a way for developers to run asynchronous logic in response to these events. This could include syncing a product to an external service when it is updated in Medusa or applying a special discount when a set of specific products is added to a cart.
With our updated API, you can create subscribers using a pattern similar to how you build API Routes.
123456789101112131415// src/subscribers/order-placed.tsexport default async function ({ data, container }) {const { id } = dataconst slackService = container.resolve("slackService")const orderService = container.resolve("orderService")const order = await orderService.retrieve(id)await slackService.sendNotis({ data: order })}export const config: SubscriberConfig = {event: OrderService.Events.PLACED,}
Example of a Subscriber
The example above illustrates a few key points of the updated Subscriber API:
- Exports from files in
control the behavior of your event handlers. The default export is a function with the logic to run, and theCopy to clipboard/subscribers
export specifies the event that logic should be run in response toCopy to clipboardconfig
- The handler accepts an argument of type
. This can be de-structured to access the event data and the Medusa container.Copy to clipboardSubscriberArgs
In the example above, we register a handler that is run every time the product updated event is triggered.
The new approach to registering subscribers also comes with the benefit of making it easier than ever to set up a handler for multiple events. The
field in the config accepts either a single event or a list of events. Continuing with the above example, it would be possible to update the handler to update the external system when a product is created, updated, and deleted.Copy to clipboardevent
1234567891011121314// src/subscribers/product-sync.tsimport { syncProductsWorkflow } from "../workflows"export default async function ({ data, eventName, container }) {await syncProductsWorkflow.run({ input: data })}export const config: SubscriberConfig = {event: [ProductService.Events.CREATED,ProductService.Events.UPDATED,ProductService.Events.DELETED]}
By updating the
field and adding a few additional lines of code, the handler can now process all relevant events. This keeps the external system synchronized with Medusa.Copy to clipboardevent
What is next?
We are excited to hear your thoughts about our new Subscriber API. If you have any feedback or experience issues, don’t hesitate to post on our GitHub Issues board or GitHub Discussions. To check other of our recent releases, check out our Medusa Recap page.
Share this post