# @push.rocks/smartipc 🚀 **Lightning-fast, type-safe IPC for modern Node.js applications** [![npm version](https://img.shields.io/npm/v/@push.rocks/smartipc.svg)](https://www.npmjs.com/package/@push.rocks/smartipc) [![TypeScript](https://img.shields.io/badge/TypeScript-5.x-blue.svg)](https://www.typescriptlang.org/) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](./license) SmartIPC is a production-grade Inter-Process Communication library that brings enterprise-level messaging patterns to Node.js. Built with TypeScript from the ground up, it offers zero-dependency native IPC with automatic reconnection, type-safe messaging, and built-in observability. ## Why SmartIPC? - **🎯 Zero External Dependencies** - Pure Node.js implementation using native `net` module - **🔒 Type-Safe** - Full TypeScript support with generics for compile-time safety - **🔄 Auto-Reconnect** - Built-in exponential backoff and circuit breaker patterns - **📊 Observable** - Real-time metrics and connection tracking - **⚡ High Performance** - Length-prefixed framing, backpressure handling, and optimized buffers - **🎭 Multiple Patterns** - Request/Response, Pub/Sub, and Fire-and-Forget messaging - **🛡️ Production Ready** - Message size limits, heartbeat monitoring, and graceful shutdown ## Installation ```bash pnpm add @push.rocks/smartipc # or npm install @push.rocks/smartipc ``` ## Quick Start ### Simple TCP Server & Client ```typescript import { SmartIpc } from '@push.rocks/smartipc'; // Create a server const server = SmartIpc.createServer({ id: 'my-service', host: 'localhost', port: 9876 }); // Handle incoming messages server.onMessage('hello', async (data, clientId) => { console.log(`Client ${clientId} says:`, data); return { response: 'Hello back!' }; }); await server.start(); // Create a client const client = SmartIpc.createClient({ id: 'my-service', host: 'localhost', port: 9876, clientId: 'client-1' }); await client.connect(); // Send a message and get response const response = await client.request('hello', { message: 'Hi server!' }); console.log('Server responded:', response); ``` ## Core Concepts ### Transport Types SmartIPC supports multiple transport mechanisms, automatically selecting the best one for your platform: ```typescript // TCP Socket (cross-platform, network-capable) const tcpServer = SmartIpc.createServer({ id: 'tcp-service', host: 'localhost', port: 9876 }); // Unix Domain Socket (Linux/macOS, fastest local IPC) const unixServer = SmartIpc.createServer({ id: 'unix-service', socketPath: '/tmp/my-app.sock' }); // Windows Named Pipe (Windows optimal) const pipeServer = SmartIpc.createServer({ id: 'pipe-service', pipeName: 'my-app-pipe' }); ``` ### Message Patterns #### 🔥 Fire and Forget Fast, one-way messaging when you don't need a response: ```typescript // Server server.onMessage('log', (data, clientId) => { console.log(`[${clientId}]:`, data.message); // No return value needed }); // Client await client.sendMessage('log', { message: 'User logged in', timestamp: Date.now() }); ``` #### 📞 Request/Response RPC-style communication with timeouts and type safety: ```typescript // Server - Define your handler with types interface CalculateRequest { operation: 'add' | 'multiply'; values: number[]; } interface CalculateResponse { result: number; computedAt: number; } server.onMessage('calculate', async (data) => { const result = data.operation === 'add' ? data.values.reduce((a, b) => a + b, 0) : data.values.reduce((a, b) => a * b, 1); return { result, computedAt: Date.now() }; }); // Client - Type-safe request const response = await client.request( 'calculate', { operation: 'add', values: [1, 2, 3, 4, 5] }, { timeout: 5000 } ); console.log(`Sum is ${response.result}`); ``` #### 📢 Pub/Sub Pattern Topic-based message broadcasting: ```typescript // Server automatically handles subscriptions const publisher = SmartIpc.createClient({ id: 'events-service', clientId: 'publisher' }); const subscriber1 = SmartIpc.createClient({ id: 'events-service', clientId: 'subscriber-1' }); const subscriber2 = SmartIpc.createClient({ id: 'events-service', clientId: 'subscriber-2' }); // Subscribe to topics await subscriber1.subscribe('user.login', (data) => { console.log('User logged in:', data); }); await subscriber2.subscribe('user.*', (data) => { console.log('User event:', data); }); // Publish events await publisher.publish('user.login', { userId: '123', timestamp: Date.now() }); ``` ## Advanced Features ### 🔄 Auto-Reconnection with Exponential Backoff Clients automatically reconnect on connection loss: ```typescript const client = SmartIpc.createClient({ id: 'resilient-service', clientId: 'auto-reconnect-client', reconnect: { enabled: true, initialDelay: 1000, // Start with 1 second maxDelay: 30000, // Cap at 30 seconds factor: 2, // Double each time maxAttempts: Infinity // Keep trying forever } }); // Monitor connection state client.on('connected', () => console.log('Connected! 🟢')); client.on('disconnected', () => console.log('Connection lost! 🔴')); client.on('reconnecting', (attempt) => console.log(`Reconnecting... Attempt ${attempt} 🟡`)); ``` ### 💓 Heartbeat Monitoring Keep connections alive and detect failures quickly: ```typescript const server = SmartIpc.createServer({ id: 'monitored-service', heartbeat: { enabled: true, interval: 5000, // Send heartbeat every 5 seconds timeout: 15000 // Consider dead after 15 seconds } }); // Clients automatically respond to heartbeats const client = SmartIpc.createClient({ id: 'monitored-service', clientId: 'heartbeat-client', heartbeat: true // Enable heartbeat responses }); ``` ### 📊 Real-time Metrics & Observability Track performance and connection health: ```typescript // Server metrics const serverStats = server.getStats(); console.log({ isRunning: serverStats.isRunning, connectedClients: serverStats.connectedClients, totalConnections: serverStats.totalConnections, uptime: serverStats.uptime, metrics: { messagesSent: serverStats.metrics.messagesSent, messagesReceived: serverStats.metrics.messagesReceived, bytesSent: serverStats.metrics.bytesSent, bytesReceived: serverStats.metrics.bytesReceived, errors: serverStats.metrics.errors } }); // Client metrics const clientStats = client.getStats(); console.log({ connected: clientStats.connected, reconnectAttempts: clientStats.reconnectAttempts, lastActivity: clientStats.lastActivity, metrics: clientStats.metrics }); // Track specific clients on server const clientInfo = server.getClientInfo('client-1'); console.log({ clientId: clientInfo.clientId, metadata: clientInfo.metadata, connectedAt: clientInfo.connectedAt, lastActivity: clientInfo.lastActivity, subscriptions: clientInfo.subscriptions }); ``` ### 🛡️ Security & Limits Protect against malicious or misbehaving clients: ```typescript const secureServer = SmartIpc.createServer({ id: 'secure-service', maxMessageSize: 10 * 1024 * 1024, // 10MB max message size maxConnections: 100, // Limit concurrent connections connectionTimeout: 60000, // Drop idle connections after 1 minute // Authentication (coming soon) auth: { required: true, validator: async (token) => { // Validate auth token return validateToken(token); } } }); // Rate limiting per client secureServer.use(rateLimitMiddleware({ windowMs: 60000, // 1 minute window max: 100 // 100 requests per window })); ``` ### 🎯 Broadcast to Specific Clients Send targeted messages: ```typescript // Broadcast to all connected clients server.broadcast('system-alert', { message: 'Maintenance in 5 minutes' }); // Send to specific client server.sendToClient('client-1', 'personal-message', { content: 'This is just for you' }); // Send to multiple specific clients server.sendToClients(['client-1', 'client-2'], 'group-message', { content: 'Group notification' }); // Get all connected client IDs const clients = server.getConnectedClients(); console.log('Connected clients:', clients); ``` ## Error Handling Comprehensive error handling with typed errors: ```typescript import { IpcError, ConnectionError, TimeoutError } from '@push.rocks/smartipc'; // Client error handling client.on('error', (error) => { if (error instanceof ConnectionError) { console.error('Connection failed:', error.message); } else if (error instanceof TimeoutError) { console.error('Request timed out:', error.message); } else { console.error('Unknown error:', error); } }); // Server error handling server.on('client-error', (clientId, error) => { console.error(`Client ${clientId} error:`, error); // Optionally disconnect misbehaving clients if (error.code === 'INVALID_MESSAGE') { server.disconnectClient(clientId); } }); // Request with error handling try { const response = await client.request('risky-operation', data, { timeout: 5000, retries: 3 }); } catch (error) { if (error instanceof TimeoutError) { // Handle timeout } else { // Handle other errors } } ``` ## Testing SmartIPC includes comprehensive testing utilities: ```typescript import { createTestServer, createTestClient } from '@push.rocks/smartipc/testing'; describe('My IPC integration', () => { let server, client; beforeEach(async () => { server = await createTestServer(); client = await createTestClient(server); }); afterEach(async () => { await client.disconnect(); await server.stop(); }); it('should handle messages', async () => { server.onMessage('test', (data) => ({ echo: data })); const response = await client.request('test', { value: 42 }); expect(response.echo.value).toBe(42); }); }); ``` ## Performance Benchmarks SmartIPC has been optimized for high throughput and low latency: | Transport | Messages/sec | Avg Latency | Use Case | |-----------|-------------|-------------|----------| | Unix Socket | 150,000+ | < 0.1ms | Local high-performance IPC | | TCP (localhost) | 100,000+ | < 0.2ms | Local network-capable IPC | | TCP (network) | 50,000+ | < 1ms | Distributed systems | | Named Pipe | 120,000+ | < 0.15ms | Windows local IPC | *Benchmarked on modern hardware with 1KB message payloads* ## Architecture SmartIPC uses a layered architecture for maximum flexibility: ``` ┌─────────────────────────────────────────┐ │ Application Layer │ │ (Your business logic and handlers) │ └─────────────────────────────────────────┘ ↕ ┌─────────────────────────────────────────┐ │ IPC Client / Server │ │ (High-level API, patterns, routing) │ └─────────────────────────────────────────┘ ↕ ┌─────────────────────────────────────────┐ │ IPC Channel │ │ (Connection management, reconnection, │ │ heartbeat, request/response) │ └─────────────────────────────────────────┘ ↕ ┌─────────────────────────────────────────┐ │ Transport Layer │ │ (TCP, Unix Socket, Named Pipe) │ │ (Framing, buffering, I/O) │ └─────────────────────────────────────────┘ ``` ## Comparison with Alternatives | Feature | SmartIPC | node-ipc | zeromq | | |---------|----------|----------|---------|--| | Zero Dependencies | ✅ | ❌ | ❌ | | | TypeScript Native | ✅ | ❌ | ❌ | | | Auto-Reconnect | ✅ | ⚠️ | ✅ | | | Request/Response | ✅ | ⚠️ | ✅ | | | Pub/Sub | ✅ | ❌ | ✅ | | | Built-in Metrics | ✅ | ❌ | ❌ | | | Heartbeat | ✅ | ❌ | ✅ | | | Message Size Limits | ✅ | ❌ | ✅ | | | Type Safety | ✅ | ❌ | ❌ | | ## Contributing We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details. ```bash # Clone the repository git clone https://code.foss.global/push.rocks/smartipc.git # Install dependencies pnpm install # Run tests pnpm test # Build pnpm build ``` ## Support - 📖 [Documentation](https://code.foss.global/push.rocks/smartipc) - 🐛 [Issue Tracker](https://code.foss.global/push.rocks/smartipc/issues) - 💬 [Discussions](https://code.foss.global/push.rocks/smartipc/discussions) ## License and Legal Information This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. **Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file. ### Trademarks This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH. ### Company Information Task Venture Capital GmbH Registered at District court Bremen HRB 35230 HB, Germany For any legal inquiries or if you require further information, please contact us via email at hello@task.vc. By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works. --- **Built with ❤️ by Task Venture Capital GmbH**