Overview
EverShop’s event system provides a powerful pub/sub architecture for handling asynchronous operations. Events are emitted to a PostgreSQL database queue and processed by dedicated subscriber functions running in a separate process.Event Emitter
emit()
Emit an event to the event queue for asynchronous processing.packages/evershop/src/lib/event/emitter.ts:26
Signature
Parameters
The name of the event to emit
The event payload data. For typed events, the structure is validated against
EventDataRegistryReturns
Promise that resolves when the event is inserted into the database queue.Implementation Details
Theemit() function:
- Inserts events into the
eventtable in PostgreSQL - Events are processed asynchronously by the event manager process
- Uses the query builder to ensure safe database operations
packages/evershop/src/lib/event/emitter.ts:26
Event Subscribers
Creating a Subscriber
Subscribers are functions that respond to specific events. They are automatically loaded from module directories.File Structure
Subscriber Function
packages/evershop/src/modules/catalog/subscribers/category_created/buildUrlRewrite.ts:10
Subscriber Execution
Subscribers are executed by the event manager process:- Events are loaded from the database queue every 10 seconds
- Matching subscribers are called for each event
- Events are marked as processed and removed from the queue
- Failed subscribers are logged but don’t block other subscribers
packages/evershop/src/lib/event/event-manager.js:99
callSubscribers()
Internal function to execute subscriber functions for an event.packages/evershop/src/lib/event/callSubscibers.js:3
Signature
Implementation
Event Manager
The event manager is a separate process that:- Loads events from the database every 10 seconds
- Maintains a queue of up to 10 events in memory
- Executes matching subscribers for each event
- Syncs completed events back to the database every 2 seconds
- Removes processed events from the database
packages/evershop/src/lib/event/event-manager.js:1
Configuration
Built-in Events
Common Events
order_placed- Triggered when an order is placedcategory_created- Triggered when a category is createdcategory_updated- Triggered when a category is updatedcategory_deleted- Triggered when a category is deletedproduct_created- Triggered when a product is createdproduct_updated- Triggered when a product is updated
Best Practices
Handle Errors Gracefully
Use try-catch blocks in subscribers to prevent one failure from affecting others
Example: Order Confirmation Email
packages/evershop/src/modules/oms/subscribers/order_placed/sendOrderConfirmationEmail.ts:624