Install kSync v0.2 and set up your development environment
npm install @klastra/ksync
import { createKSync } from '@klastra/ksync'; // Works immediately with smart defaults const ksync = createKSync(); // Send events await ksync.send('message', { text: 'Hello world!' }); // Listen for events ksync.on('message', (data) => console.log(data.text));
tsconfig.json
{ "compilerOptions": { "target": "ES2020", "module": "ESNext", "moduleResolution": "node", "allowSyntheticDefaultImports": true, "esModuleInterop": true, "strict": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "jsx": "react-jsx" // For React integration } }
import { createKSync } from '@klastra/ksync'; // Browser-optimized configuration const ksync = createKSync({ serverUrl: 'ws://localhost:8080', // Your WebSocket server // Storage automatically uses IndexedDB });
import { createKSync } from '@klastra/ksync'; // Server-optimized configuration const ksync = createKSync({ storage: { type: 'memory' }, // Memory storage for servers sync: { enabled: false }, // Disable sync on server });
import { createKSync } from '@klastra/ksync'; const ksync = createKSync({ serverUrl: 'ws://localhost:8080', performance: { batchSize: 200, // Leverage Bun's performance batchDelay: 5 // Ultra-low latency } });
import { createChat } from '@klastra/ksync'; // Optimized for chat with presence const chat = createChat('room-name', { serverUrl: 'ws://localhost:8080' }); await chat.setPresence({ status: 'online', metadata: { name: 'Alice' } });
# Clone for development git clone https://github.com/0ni-x4/ksync.git cd ksync # Install dependencies npm install # Start the development server npm run server
import { WebSocketServer } from 'ws'; const wss = new WebSocketServer({ port: 8080 }); console.log('🚀 kSync server running on ws://localhost:8080'); wss.on('connection', (ws) => { console.log('📱 Client connected'); ws.on('message', (data) => { // Broadcast to all other clients wss.clients.forEach((client) => { if (client !== ws && client.readyState === 1) { client.send(data); } }); }); });
import { createKSync } from '@klastra/ksync'; const ksync = createKSync({ // High-performance settings performance: { batchSize: 200, // Higher throughput batchDelay: 5, // Low latency materializationCaching: true, // Cache computed state compressionThreshold: 1024 // Compress large payloads }, // Production features auth: { token: process.env.JWT_TOKEN, type: 'bearer' }, // Monitoring debug: { events: false, // Disable in production performance: true, // Monitor metrics sync: false, storage: false }, // Scaling offline: { queueSize: 10000, // Large offline queue persistence: true // Persist across restarts } });