Overview
This tutorial demonstrates how to build a message queue system using Motia’s pub/sub pattern. You’ll create an order processing system that accepts orders via HTTP, processes them asynchronously, and sends notifications. What you’ll learn:- Pub/sub messaging patterns
- Queue-triggered Steps
- Chaining Steps with enqueue
- Event-driven architecture
- Error handling in async workflows
- Processing pipelines
Prerequisites
Before starting, make sure you have:
- Node.js version 19 or higher
- Completed the Hello World tutorial
- Understanding of asynchronous processing
Use Case: Order Processing System
We’ll build a system that:- Accepts orders via HTTP API
- Validates and processes orders in the background
- Updates inventory
- Sends confirmation notifications
- Handles errors gracefully
Project Setup
Building the System
Step 1: Define Data Types
Createsteps/orders/types.ts:
steps/orders/types.ts
Step 2: Create Order API
Createsteps/orders/create-order.step.ts:
steps/orders/create-order.step.ts
- Immediate response: API returns immediately while processing happens in background
- State storage: Order is persisted before enqueueing
- Enqueue: Publishes event to
process-ordertopic
Step 3: Process Orders
Createsteps/orders/process-order.step.ts:
steps/orders/process-order.step.ts
- Chained events: Enqueues multiple follow-up tasks
- Error handling: Catches failures and triggers error workflow
- State updates: Tracks order progress
- Fan-out pattern: Single event triggers multiple downstream events
Step 4: Update Inventory
Createsteps/orders/update-inventory.step.ts:
steps/orders/update-inventory.step.ts
Step 5: Send Notifications
Createsteps/orders/send-notification.step.ts:
steps/orders/send-notification.step.ts
Step 6: Get Order Status
Createsteps/orders/get-order.step.ts:
steps/orders/get-order.step.ts
Running the Application
Flow Diagram
Advanced Patterns
Parallel Processing
Modifyprocess-order.step.ts to run inventory and notification in parallel:
Retry Logic
Add retry configuration to queue triggers:Dead Letter Queue
Handle permanently failed events:Next Steps
Scheduled Tasks
Add periodic jobs to your system
Queue Reference
Learn advanced queue features
Background Jobs Guide
Best practices for background processing
Workflows Guide
Advanced workflow patterns