Subscribers Not Working

This guide helps you troubleshoot issues when subscribers in your Medusa application are not running.

Confirm Event is Emitted#

Make sure that the event you're trying to listen to is actually being emitted. You can do this by adding a console log or using a debugger in the part of your code where the event is emitted.

For example:

If the event is not being emitted, check the logic in your code to ensure that the event emission is correctly implemented.


Confirm Subscriber is Set Up Correctly#

Make sure that your subscriber is correctly set up to listen to the event you're emitting. Check the following:

  1. The subscriber is created under the src/subscribers directory.
  2. The subscriber exports an asynchronus function that handles the event. For example:
src/subscribers/your-subscriber.ts
1import { SubscriberArgs, type SubscriberConfig } from "@medusajs/framework"2
3export default async function orderPlacedHandler({4  event: { data },5  container,6}: SubscriberArgs<{ id: string }>) {7  const logger = container.resolve("logger")8
9  logger.info("Sending confirmation email...")10
11  // ...12}
  1. The subscriber exports a configuration object with the correct event name. For example:
src/subscribers/your-subscriber.ts
1export const config: SubscriberConfig = {2  event: `order.placed`, // Confirm this name is correct3}

If your subscriber is set up correctly, you'll see the following log in the terminal when the event is emitted:

Terminal
info:    Processing order.placed which has 1 subscribers

Confirm Worker Mode is Set Up Correctly#

If you emit an event, but subscribers listening to that event don't run, it may be because your Medusa application is not running in worker mode.

Note: If you're a Cloud user and this issue is happening on your deployed Medusa application, contact support for assistance.

Why this Happens#

By default, the Medusa application runs both the server, which handles all incoming requests, and the worker, which processes background tasks, in a single process.

In production, you should deploy two separate instances of your Medusa application: one for the server and one for the worker.

However, if you deploy only a server instance, or if you accidentally set the worker mode to server, the subscribers won't run because there is no worker process to handle them.

How to Fix it#

Deploy a Worker Instance

If you haven't deployed a worker instance of your Medusa application, do so by following the steps in the Worker Mode of Medusa Instance guide.

For example, you should have the following configuration in your medusa-config.ts file:

medusa-config.ts
1module.exports = defineConfig({2  projectConfig: {3    workerMode: process.env.WORKER_MODE || "shared",4    // ...5  },6  // ...7})

In your deployed server Medusa instance, set WORKER_MODE to server, and in your deployed worker Medusa instance, set WORKER_MODE to worker.

Set the Correct Worker Mode

If this issue is happening locally, ensure that you set the correct worker mode in your medusa-config.ts file.

When running the server locally, either don't set the workerMode configuration or set it to shared in medusa-config.ts:

medusa-config.ts
1module.exports = defineConfig({2  projectConfig: {3    workerMode: "shared", // or don't set it at all4    // ...5  },6  // ...7})

Additional Resources

Was this page helpful?
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break