Build scalable distributed systems with built-in Service Discovery and Event Bus. Supports Redis for production and in-memory for development.
Asynchronous communication between services with Redis or in-memory support.
Automatic service registration and discovery with health checks.
Built-in round-robin and weighted load balancing across instances.
Type-safe remote procedure calls with circuit breaker support.
The Event Bus allows your services to communicate asynchronously without tight coupling. Use eventBus().publish() to emit events.
1import { eventBus } from 'canxjs';23// Publish an event4await eventBus().publish('order.created', {5orderId: '123',6amount: 99.00,7customer: 'john@example.com'8});910// Publish with metadata11await eventBus().publish('user.registered', {12userId: 'usr_456',13email: 'jane@example.com'14}, {15priority: 'high',16retries: 317});
Use the @Subscribe decorator or subscribe programmatically to handle events from other services.
1import { Subscribe } from 'canxjs';23class EmailService {4@Subscribe('order.created')5async onOrderCreated(payload: any) {6await sendOrderConfirmation(payload.orderId);7}89@Subscribe('user.registered')10async onUserRegistered(payload: any) {11await sendWelcomeEmail(payload.email);12}13}1415// Or subscribe programmatically16eventBus().subscribe('payment.failed', async (payload) => {17await notifyAdmin(payload);18});
Automatic service discovery and load balancing. Services register themselves upon startup and can discover other services by name.
1import { createServiceRegistry } from 'canxjs';23const registry = createServiceRegistry({4driver: 'redis', // or 'consul', 'etcd'5ttl: 30000 // Health check interval6});78// Register this instance9await registry.register('payment-service', '10.0.0.5', 3000);1011// Discover other services12const userService = await registry.discover('user-service');13// Result: { host: '10.0.0.2', port: 4000, healthy: true }1415// Load-balanced discovery (round-robin)16const instance = await registry.discoverOne('api-gateway');
Type-safe remote procedure calls with automatic service discovery and circuit breaker support.
1import { createRpcClient } from 'canxjs';23// Create typed RPC client4const userClient = createRpcClient<UserService>({5service: 'user-service',6timeout: 50007});89// Call remote methods10const user = await userClient.getUser('usr_123');11const users = await userClient.listUsers({ page: 1, limit: 10 });1213// With circuit breaker14const orderClient = createRpcClient<OrderService>({15service: 'order-service',16circuitBreaker: {17threshold: 5,18timeout: 3000019}20});
| Driver | Use Case | Config |
|---|---|---|
| memory | Development, testing | None required |
| redis | Production | REDIS_URL |
| consul | Enterprise | CONSUL_URL |