@argos-js/events
v0.1.2
Published
Event-driven architecture suite with pluggable brokers (Kafka, RabbitMQ, Redis, Memory) and Transactional Outbox pattern for Argos-JS
Readme
@argos-js/events
High-performance, runtime-agnostic event-driven messaging suite for Argos-JS, featuring first-class support for native CDC engine ArgosDB Pulse (port 8788, binary Bincode 1.x Little-Endian framing) and interchangeable broker drivers.
Features
- Runtime-Agnostic Binary Serialization: 100% pure TypeScript Bincode 1.x Little-Endian encoder/decoder built on standard Web APIs (
Uint8Array,DataView,TextEncoder,TextDecoder), with zero native C/C++ dependencies. Fully portable across Node.js 18+, Bun, and Deno. - Pluggable Event Drivers:
pulse: Native CDC/WAL stream consumer for ArgosDB Pulse (argosdb-pulse, port 8788).memory: High-performance, zero-dependency in-memory pub/sub broker for unit testing and CI.rabbitmq: Enterprise AMQP topic exchanges with competing consumer queues.redis: Redis Streams consumer groups and Pub/Sub.kafka: Apache Kafka cluster consumer groups and partitioned topics.
- ArgosDB Pulse Protocol:
- Sub-millisecond binary TCP framing (
[4-byte LE payload length][bincode payload]). - Native client OpCodes:
Subscribe(0x00),JoinGroup(0x01),Ack(0x02),Nack(0x03),SeekHlc(0x04). - High-resilience
PulseStreamParserwith cumulative buffer for seamless TCP packet chunking and backpressure handling. - Automatic reconnection with exponential backoff and transparent consumer group re-registration.
- Sub-millisecond binary TCP framing (
- Consumer Groups & CDC Records: Competing consumers with automatic or manual
ctx.ack()andctx.nack(), plusevent.jsonPayload<T>()helper. - Transactional Outbox Pattern: Atomic domain event emission within database transactions, guaranteed at-least-once delivery with background polling relay.
Installation
pnpm add @argos-js/eventsConfiguration & Drivers
Configure the active driver via code or environment variables:
# Select active driver: pulse | memory | rabbitmq | redis | kafka
EVENTS_DRIVER=pulse
# ArgosDB Pulse CDC connection
ARGOS_PULSE_HOST=127.0.0.1
ARGOS_PULSE_PORT=8788Initializing the Events Manager
import { createEventsManager } from '@argos-js/events';
export const events = createEventsManager({
driver: (process.env.EVENTS_DRIVER as any) || 'pulse',
pulse: {
host: process.env.ARGOS_PULSE_HOST || '127.0.0.1',
port: Number(process.env.ARGOS_PULSE_PORT) || 8788,
autoAck: false, // Manual acknowledgement via ctx.ack() / ctx.nack()
},
outbox: {
enabled: true,
tableName: 'events_outbox',
pollIntervalMs: 1000,
},
});Usage: Consumer Groups (CDC Mutations)
ArgosDB Pulse automatically publishes mutation events from write-ahead logs (INSERT, UPDATE, DELETE) on channels such as cdc.auth_users.*.
import { events, MutationOp } from '@argos-js/events';
await events.connect();
// Join consumer group 'email_workers'
await events.subscribeGroup({
group: 'email_workers',
workerId: `worker-${process.pid}`,
pattern: 'cdc.auth_users.*',
handler: async (record, ctx) => {
// Check mutation operation
if (record.event.op === MutationOp.Insert) {
// Decode JSON from afterPayload
const user = record.event.jsonPayload<{ id: string; name: string; email: string }>();
if (user?.email) {
console.log(`[EmailWorker] ✉️ Sending welcome email to: ${user.name} <${user.email}>`);
await sendWelcomeEmail(user);
}
}
// Acknowledge processing
await ctx.ack();
},
});Binary Protocol Specification (ArgosDB Pulse)
Frame Layout
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Payload Length (4 bytes, Little-Endian) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Bincode 1.x Serialized Payload (Little-Endian) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+Supported OpCodes (Client -> Server)
| Tag (u32 LE) | Variant | Fields |
| :--- | :--- | :--- |
| 0x00 | Subscribe | pattern: String |
| 0x01 | JoinGroup | group_name: String, consumer_id: String, pattern: String |
| 0x02 | Ack | group_name: String, message_id: u128 |
| 0x03 | Nack | group_name: String, message_id: u128 |
| 0x04 | SeekHlc | hlc: u128 |
Server Frames (Server -> Client)
| Tag (u32 LE) | Variant | Structure |
| :--- | :--- | :--- |
| 0 | Subscribed | pattern: String |
| 1 | JoinedGroup | group_name: String, consumer_id: String |
| 2 | Record | topic: String, event: MutationEvent |
| 3 | GroupRecord | group_name: String, consumer_id: String, message_id: u128, topic: String, event: MutationEvent |
| 4 | AckResult | ok: bool, message_id: u128 |
| 5 | NackResult | ok: bool, message_id: u128 |
| 6 | Error | message: String |
Transactional Outbox Pattern
Emit events safely within ACID database transactions:
await db.transaction(async (trx) => {
const user = await trx.from('users').insert({ name: 'Alice' }).returning('*');
// Automatically writes to events_outbox in the same transaction
await trx.events.emit('user.created', { id: user.id, name: user.name });
});License
MIT © Argos-JS Team
