Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 17, 2022 10:29 am GMT

How to Handle Events in a Node.js Ecommerce Platform

Medusais an open source ecommerce platform with plenty of features that boost the ecommerce experience for merchants while providing a great developer experience.

As a headless commerce platform, Medusas server architecture is composed of a few components that have different purposes and usages.

This article in particular focuses on Subscribers, whats their purpose, and how they can be used to handle events in Medusa.

Overview

In Medusa, there are events that are emitted when a certain action occurs. For example, if a customer places an order, theorder.placedevent is emitted with the order data.

These events are designed to allow other parts of the platform or third-party integrations to listen to them and take action accordingly.

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 theorder.placedevent 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 reside in your project'ssrc/subscribersdirectory. Files here should export classes, which will be treated as subscribers by Medusa. By convention, the class name should end withSubscriberand the file name should be the camel-case version of the class name withoutSubscriber. For example, theWelcomeSubscriberclass is in the filesrc/subscribers/welcome.js.

Whenever an event is emitted, the subscribers registered handler method is executed. The handler method receives as a parameter an object that holds data related to the event. For example, if an order is placed theorder.placedevent will be emitted and all the handlers will receive the order id in the parameter object.

Prerequisites

Medusa's event system works by pushing data to a Queue that each handler then gets notified of. The queuing system is based on Redis and you will therefore need to make sure thatRedis is installed and configured for your Medusa project.

Then, you need to set your Redis URL in your Medusa server. By default, the Redis URL isredis://localhost:6379. If you use a different one, set the following environment variable in.env:

REDIS_URL=<YOUR_REDIS_URL>

After that, inmedusa-config.js, youll need to comment out the following line:

module.exports = {  projectConfig: {    redis_url: REDIS_URL, //this line is commented out    ...  }}

After that, you are able to listen to events on your server.

How to Create a Custom Subscriber

After creating the file undersrc/subscribers, in the constructor of your subscriber, you should listen to events usingeventBusService.subscribe, whereeventBusServiceis a service injected into your subscribers constructor.

The eventBusService.subscribe method takes an event name as its first parameter, and a callback function as its second parameter. This callback will be executed when the event is fired.

For example, here is theOrderNotifierSubscriberclass which is created insrc/subscribers/orderNotifier.js:

class OrderNotifierSubscriber {  constructor({ eventBusService }) {    eventBusService.subscribe("order.placed", this.handleOrder);  }  handleOrder = async (data) => {    console.log("New Order: " + data.id)  };}export default OrderNotifierSubscriber;

This subscriber will register the methodhandleOrderas one of the handlers of theorder.placedevent. The methodhandleOrderwill be executed every time an order is placed, and it will receive the order ID in thedataparameter. You can then use the orders details to perform any kind of task you need.

Thedataobject will not contain other order data. Only the ID of the order. You can retrieve the order information using theorderService.

Using Services in Subscribers

You can access any service through the dependencies injected to your subscribers constructor.

For example:

constructor({ productService, eventBusService }) {    this.productService = productService;    eventBusService.subscribe("order.placed", this.handleOrder);}

You can then usethis.productServiceanywhere in your subscribers methods.

Conclusion

Subscribers are just one part of Medusas architecture. Here are some documentation that can guide you into doing more with Medusa:

  1. How to Add an Endpoint for Storefront and Admin
  2. What are Services and how to create a custom service
  3. How to create a payment provider

Should you have any issues or questions related to Medusa, then feel free to reach out to the Medusa team viaDiscord.


Original Link: https://dev.to/medusajs/how-to-handle-events-in-a-nodejs-ecommerce-platform-1nnh

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To