Skip to main content
Event subscribers allow your extensions to react to system events like product creation, order placement, customer registration, and more. They’re perfect for implementing custom business logic, sending notifications, logging, and integrating with external services.

Overview

Event subscribers in EverShop:
  • React to system events without modifying core code
  • Execute asynchronously to avoid blocking main operations
  • Can be chained - multiple subscribers can listen to the same event
  • Are easy to test and maintain
  • Support both sync and async operations

How Events Work

When an action occurs in EverShop (e.g., a product is created), the system emits an event. Subscribers listening to that event are automatically executed.

Directory Structure

Event subscribers are organized by event name:
The directory name must match the event name exactly.

Creating Your First Subscriber

Real-World Example

Here’s the actual subscriber from the sample extension:
extensions/sample/src/subscribers/product_created/consoleLog.js
This simple subscriber logs product data to the console whenever a new product is created.

Common Events

Here are common events you can subscribe to:

Product Events

Order Events

Customer Events

Advanced Examples

Send Email on Order

src/subscribers/order_placed/sendConfirmationEmail.js

Update Inventory

src/subscribers/order_placed/updateInventory.js

Log to Database

src/subscribers/product_updated/logChange.ts

Webhook Integration

src/subscribers/order_placed/notifyWarehouse.js

Customer Segmentation

src/subscribers/customer_registered/addToSegment.js

Multiple Subscribers per Event

You can have multiple subscribers for the same event:
All subscribers will execute when the event is triggered.
Subscribers execute asynchronously and in no guaranteed order. Don’t rely on execution sequence.

Async/Await Support

Subscribers fully support async operations:

Error Handling

Always implement proper error handling:
Best Practice: Log errors but don’t throw them. Throwing errors can prevent other subscribers from executing.

Data Access Patterns

Destructure Event Data

Validate Data

Testing Subscribers

Manual Testing

Trigger events manually in development:

Unit Testing

Best Practices

Keep Subscribers Focused: Each subscriber should do one thing well. Create multiple subscribers instead of one complex one.
  • Error Handling - Always wrap logic in try-catch blocks
  • Logging - Log both successes and failures for debugging
  • Async Operations - Use async/await for cleaner code
  • Validation - Validate event data before processing
  • Idempotency - Design subscribers to handle duplicate events
  • Performance - Avoid blocking operations; use queues for heavy tasks
  • Testing - Write unit tests for business logic

Common Use Cases

Email Notifications

External Integrations

Internal Operations

Troubleshooting

Subscriber Not Executing

  1. Verify directory name matches event name exactly
  2. Check subscriber exports a default function
  3. Run npm run build to rebuild
  4. Check extension is enabled in config/default.json
  5. Verify the event is actually being emitted

Errors in Subscriber

  1. Add try-catch blocks to identify errors
  2. Check console logs for error messages
  3. Verify event data structure matches expectations
  4. Test subscriber in isolation

Performance Issues

  1. Move heavy operations to background jobs
  2. Implement queuing for external API calls
  3. Use caching where appropriate
  4. Profile subscriber execution time

Next Steps

Cron Jobs

Schedule background tasks

API Endpoints

Create custom API endpoints