Message Queue
The CommandKit Queue package provides a service-agnostic message queue API for inter-service communication. It allows you to send and receive messages between different parts of your application or across multiple services using a simple, unified interface.
Features
- Service Agnostic: Works with any message queue implementation
- Simple API: Easy-to-use
send
andreceive
functions - Type Safety: Full TypeScript support with strong typing
- Driver System: Pluggable drivers for different queue backends
- Discord.js Integration: Built-in support for Discord.js brokers
- Redis Support: Ready-to-use Redis PubSub driver
Quick Start
import { setDriver } from '@commandkit/queue';
import { RedisPubSubDriver } from '@commandkit/queue/discordjs';
import { PubSubRedisBroker } from '@discordjs/brokers';
import Redis from 'ioredis';
// Set up the driver
const broker = new PubSubRedisBroker(new Redis());
const driver = new RedisPubSubDriver(broker);
setDriver(driver);
// Send a message
import { send } from '@commandkit/queue';
await send('user-updates', { userId: '123', action: 'login' });
// Receive messages
import { receive } from '@commandkit/queue';
await receive('user-updates', (message) => {
console.log(`User ${message.userId} performed ${message.action}`);
});
Installation
npm install @commandkit/queue
For Discord.js integration with Redis:
npm install @commandkit/queue @discordjs/brokers ioredis
Core Concepts
Topics
Topics are named channels where messages are sent and received. Think of them as categories or channels for different types of messages.
Messages
Messages can be any JSON-serializable data. They're sent to topics and received by subscribers.
Drivers
Drivers are implementations that handle the actual message queue backend. CommandKit provides a unified API that works with any driver.
Use Cases
- Inter-Service Communication: Send messages between different parts of your application
- Event Broadcasting: Broadcast events to multiple subscribers
- Task Distribution: Distribute work across multiple workers
- Real-time Updates: Send real-time updates to connected clients
- Microservices: Enable communication between microservices
Next Steps
- Basic Operations - Learn about sending and receiving messages
- Discord.js Integration - Set up Redis PubSub with Discord.js
- Advanced Features - Custom drivers, error handling, and more