Creating a subscriber in medusa

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Another cool feature in medusa is that we can create custom subscribers. Subscribers are actions that are triggered by certain events.
Check out this list with all events available.
In our case, we want to subscribe to the product.created event so we can eventually request external information for each product created.
The subscriber fires based on Redis, so to make this work, you'll have to set up Redis for medusa.
First, install Redis on your machine. You can follow the documentation on the Redis website.
Once installed, open up your medusa server project and find the medusa-config.js file.
You will see the following line, which defines the Redis URL. You can set it to another value via the .env file. (However, in most cases, this default will work).
const REDIS_URL = process.env.REDIS_URL || 'redis://localhost:6379';
Then a bit lower, you need to enable Redis in the config.
module.exports = {
projectConfig: {
// Turn the line below on
redis_url: REDIS_URL,
database_database: './medusa-db.sql',
database_type: 'sqlite',
store_cors: STORE_CORS,
admin_cors: ADMIN_CORS,
},
plugins,
};
And that's it. You now have Redis enabled.
Now that we have Redis installed let's create our custom subscribers.
In your medusa server project, find the src/subscribers folder and, in there, create a new file called: productNotifier.js.
I'm using this naming as I want to be a notifier for new products.
The setup for each subscriber looks like this.
class ProductNotifierSubscriber {
constructor({ eventBusService }) {
eventBusService.subscribe('product.created', this.handleProduct);
}
handleProduct = async (data) => {
console.log('New product: ' + data.id);
};
}
export default ProductNotifierSubscriber;
In the example above, we subscribe to the product.created event, which is triggered each time a product is created.
We can tell medusa to execute our own handleProduct function.
Inside this function, we log the product for now.
Go ahead and spool up your server. Now visit the admin portal and add a new product. You should see the server log showing this log now.

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter