0.2.5-internal.test.5 โ€ข Published 9 months ago

@klastra/ksync v0.2.5-internal.test.5

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

kSync v0.2 ๐Ÿš€

Real-time sync engine for modern applications.
Built for high-performance web and AI apps that need full control over events, state management, and real-time collaboration โ€” with developer-friendly APIs and production-ready scalability.

TypeScript Performance Memory Tests


โœจ What's New in v0.2

๐ŸŽฏ Developer Experience First

  • Smart Defaults - Works out of the box, configure only what you need
  • Factory Functions - createChat(), createTodos(), createGame(), createAI()
  • Comprehensive Configuration - 50+ options with full TypeScript docs
  • Backward Compatibility - All existing APIs still work

โšก Performance & Scale

  • 300k+ ops/sec - Handles massive concurrent workloads
  • Memory Efficient - Only 18MB for 1000 clients + 10k events
  • Network Resilient - Graceful handling of 10-200ms latency
  • Enterprise Ready - Tested with 500+ concurrent clients

๐Ÿ”ง Advanced Features

  • Presence System - Real-time user presence with metadata
  • Streaming Support - Live AI responses and real-time data streams
  • Offline-First - Queue events when offline, sync when reconnected
  • Room Management - Multi-room support with automatic routing
  • Performance Monitoring - Built-in metrics and debugging

๐Ÿš€ Quick Start

Installation

npm install @klastra/ksync
# or
bun add @klastra/ksync

Basic Usage

import { createKSync } from '@klastra/ksync';

// Works instantly with smart defaults
const ksync = createKSync();

// Listen for events
ksync.on('message', (data, event) => {
  console.log(`${event.userId}: ${data.text}`);
});

// Send events (instant local, auto-synced if server configured)
await ksync.send('message', {
  text: 'Hello world!',
  timestamp: Date.now()
});

// Get current state
const state = ksync.getState();

Factory Functions (Optimized Presets)

import { createChat, createTodos, createGame, createAI } from '@klastra/ksync';

// Chat app with presence and optimized batching
const chat = createChat('my-room', {
  serverUrl: 'ws://localhost:8080'
});

// Todo app with offline persistence
const todos = createTodos({
  offline: { persistence: true, queueSize: 5000 }
});

// Game with low-latency updates
const game = createGame('game-123', {
  performance: { batchSize: 50, batchDelay: 5 }
});

// AI with streaming responses
const ai = createAI('conversation-1', {
  features: { streaming: true }
});

Real-time Chat Example

const chat = createChat('general', {
  serverUrl: 'ws://localhost:8080',
  features: { presence: true }
});

// Set user presence
await chat.setPresence({
  status: 'online',
  metadata: { name: 'Alice', avatar: 'avatar-url' }
});

// Send messages
chat.on('message', (data) => {
  console.log(`${data.author}: ${data.text}`);
});

await chat.send('message', {
  text: 'Hello everyone!',
  author: 'Alice',
  timestamp: Date.now()
});

// Check who's online
const presence = chat.getPresence();
console.log(`${presence.length} users online`);

AI Streaming Example

const ai = createAI('assistant', {
  features: { streaming: true }
});

// Listen for streaming chunks
ai.on('stream-chunk', (data) => {
  process.stdout.write(data.data); // Live AI response
});

ai.on('stream-end', (data) => {
  console.log('\nResponse complete!');
});

// Start AI response stream
await ai.startStream('response-1');
await ai.streamChunk('response-1', { data: 'Hello! ' });
await ai.streamChunk('response-1', { data: 'How can I help you?' });
await ai.endStream('response-1');

๐ŸŽ›๏ธ Comprehensive Configuration

kSync v0.2 provides extensive configuration options with intelligent defaults:

const ksync = createKSync({
  // === CONNECTION ===
  serverUrl: 'ws://localhost:8080',    // Auto-connects if provided
  room: 'my-room',                     // Default: 'default'
  userId: 'user-123',                  // Auto-generated if not provided
  
  // === AUTHENTICATION ===
  auth: {
    token: 'jwt-token',                // Static token
    provider: async () => getToken(),  // Dynamic token provider
    type: 'bearer'                     // 'bearer', 'jwt', 'custom'
  },
  
  // === PERFORMANCE ===
  performance: {
    batchSize: 100,                    // Events per batch
    batchDelay: 10,                    // Batch delay in ms
    materializationCaching: true,      // Cache state computations
    compressionThreshold: 1024         // Compress payloads > 1KB
  },
  
  // === OFFLINE SUPPORT ===
  offline: {
    enabled: true,                     // Queue events when offline
    queueSize: 1000,                   // Max queued events
    persistence: true,                 // Persist queue to storage
    syncOnReconnect: true              // Auto-sync when back online
  },
  
  // === STATE MANAGEMENT ===
  state: {
    materializer: (events) => {        // Transform events to state
      return events.reduce((state, event) => {
        // Your state logic here
        return newState;
      }, {});
    },
    enableCaching: true,               // Cache materialized state
    autoMaterialize: false             // Auto-run on new events
  },
  
  // === FEATURES ===
  features: {
    presence: true,                    // Enable presence system
    streaming: true,                   // Enable streaming support
    encryption: false                  // Client-side encryption
  },
  
  // === DEBUGGING ===
  debug: {
    events: true,                      // Log events
    sync: true,                        // Log sync operations
    performance: false,                // Log performance metrics
    storage: false                     // Log storage operations
  }
});

๐Ÿ“Š Performance Benchmarks

kSync v0.2 delivers enterprise-grade performance:

Scale Test Results

๐Ÿš€ Scale Benchmarks

โšก Testing 500 Concurrent Clients...
โœ… 500 clients, 2500 events: 344,019 ops/sec in 7ms

๐ŸŒ Testing Network Conditions...
โœ… Handled 10-200ms latency gracefully (202ms total)

๐Ÿ’ฌ Testing High-Load Chat...
โœ… 100 users, 400 events: 485,830 ops/sec across 5 rooms

๐Ÿง  Testing Memory Efficiency...
โœ… 1000 clients, 10k events: 593,068 ops/sec (18MB heap)

๐Ÿ“Š Summary:
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
๐ŸŽฏ 500 Concurrent Clients: 344,019 ops/sec
๐ŸŒ Network Resilience: 10-200ms handled
๐Ÿ’ฌ Chat Performance: 485,830 ops/sec
๐Ÿง  Memory Efficiency: 593,068 ops/sec (18MB)
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

Detailed Performance Metrics

TestPerformanceDetails
Basic Event Throughput85,770 ops/sec10k events processed
Event Batching18,716 ops/secOptimized batching
State Materialization3.7M ops/sec<1.35ms latency
Memory Efficiency0B per eventEfficient trimming
Concurrent Operations6,558 ops/sec670 concurrent ops
Storage Performance395M ops/secIndexedDB operations
Presence System318,108 ops/sec1000 users tracked
Streaming1.07M ops/secReal-time chunks

Run Benchmarks Yourself

# Comprehensive benchmark suite
npm run benchmark

# Quick scale test
npx tsx scripts/final-benchmark.ts

๐Ÿ—๏ธ Architecture

Event-Driven Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Application   โ”‚โ”€โ”€โ”€โ”€โ”‚     kSync       โ”‚โ”€โ”€โ”€โ”€โ”‚   WebSocket     โ”‚
โ”‚   (Your Code)   โ”‚    โ”‚   (Local-First) โ”‚    โ”‚    Server       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚                       โ”‚                       โ”‚
         โ”‚ send()               โ”‚ store                 โ”‚ sync
         โ”‚ on()                 โ”‚ materialize           โ”‚ broadcast
         โ”‚                      โ”‚ batch                 โ”‚
         โ–ผ                      โ–ผ                       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    Event Processing Flow                        โ”‚
โ”‚                                                                 โ”‚
โ”‚  1. Event Created โ†’ 2. Local Storage โ†’ 3. Materialization โ†’    โ”‚
โ”‚  4. Batch Processing โ†’ 5. Network Sync โ†’ 6. Conflict Resolution โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Key Concepts

  • Local-First: Events stored locally immediately, synced asynchronously
  • Event Sourcing: Append-only event log with state materialization
  • Optimistic Updates: UI updates instantly, conflicts resolved later
  • Smart Batching: Automatic event batching for performance
  • Presence System: Real-time user presence with metadata
  • Room Isolation: Multi-room support with automatic routing

๐Ÿ”Œ API Reference

Core Methods

// Event Management
await ksync.send(type, data, options?)     // Send event
ksync.on(type, listener)                   // Listen to events
ksync.once(type, listener)                 // Listen once
ksync.off(type, listener)                  // Remove listener

// State Management
ksync.getState(forceMaterialize?)          // Get materialized state
ksync.getEvents()                          // Get raw events
ksync.clear(options?)                      // Clear data

// Connection Management
await ksync.connect(options?)              // Connect to server
await ksync.disconnect()                   // Disconnect
await ksync.sync(options?)                 // Manual sync

// Room Management
await ksync.joinRoom(room, options?)       // Join different room

// Presence System
await ksync.setPresence(info)              // Set user presence
ksync.getPresence(filter?)                 // Get presence info

// Streaming Support
await ksync.startStream(id, options?)      // Start stream
await ksync.streamChunk(id, chunk)         // Send chunk
await ksync.endStream(id, data?)           // End stream
ksync.getActiveStreams()                   // Get active streams

// Status & Monitoring
ksync.getStatus()                          // Get comprehensive status

Factory Functions

createKSync(config?)       // Basic instance with full control
createChat(room, config?)  // Optimized for chat applications
createTodos(config?)       // Optimized for todo/task apps
createGame(id, config?)    // Optimized for multiplayer games
createAI(id?, config?)     // Optimized for AI applications

Backward Compatibility

All v0.1 APIs still work:

// These still work exactly as before
ksync.defineSchema(name, schema)
ksync.defineMaterializer(name, fn)
await ksync.initialize(config?, syncClient?)
ksync.onPresence(listener)
await ksync.updatePresence(data)
await ksync.close()

๐Ÿงช Testing

kSync v0.2 includes comprehensive test coverage:

# Run all tests
npm test

# Run with type checking
npm run typecheck

# Run specific test file
npx tsx src/__tests__/ksync.basic.test.ts

Test Results

  • 29/29 tests passing โœ…
  • 100% TypeScript compatibility โœ…
  • Strict mode compliant โœ…
  • All edge cases covered โœ…

๐Ÿ“ Examples

Run Examples

# Basic usage
npx tsx examples/basic.ts

# Real-time sync (requires server)
npx tsx examples/sync.ts

# Advanced features
npx tsx examples/advanced-features.ts

# AI streaming
npx tsx examples/streaming.ts

# Start WebSocket server
npx tsx server/websocket-server.ts

React Integration

import { useKSync, useKSyncEvent } from '@klastra/ksync/react';

function ChatApp() {
  const [ksync] = useState(() => createChat('my-room'));
  const [messages, setMessages] = useState([]);
  
  useEffect(() => {
    ksync.on('message', (data) => {
      setMessages(prev => [...prev, data]);
    });
  }, [ksync]);

  const sendMessage = async (text: string) => {
    await ksync.send('message', {
      text,
      author: 'Current User',
      timestamp: Date.now()
    });
  };

  return (
    <div>
      <div>{messages.map(msg => <div key={msg.timestamp}>{msg.text}</div>)}</div>
      <input onKeyPress={e => e.key === 'Enter' && sendMessage(e.target.value)} />
    </div>
  );
}

๐Ÿš€ Migration from v0.1

kSync v0.2 is fully backward compatible, but here's how to use the new features:

Old Way (still works)

const ksync = new KSync({ serverUrl: 'ws://localhost:8080' });
await ksync.initialize();

New Way (recommended)

const ksync = createKSync({ serverUrl: 'ws://localhost:8080' });
// No initialization needed - connects automatically!

Using Factory Functions

// Instead of manual configuration
const ksync = createKSync({
  room: 'chat-room',
  features: { presence: true },
  performance: { batchSize: 50 }
});

// Use optimized preset
const chat = createChat('chat-room');

๐Ÿค Contributing

git clone https://github.com/0ni-x4/ksync
cd ksync
npm install
npm test
npm run benchmark

๐Ÿ“„ License

MIT License - see LICENSE file for details.


๐Ÿ”— Links

Built with โค๏ธ for developers who need real-time sync without the complexity.