$ show arlac77/svelte-websocket-store
svelte-websocket-store is a Svelte library that provides a WebSocket-backed store implementation. It allows Svelte components to synchronize state with a WebSocket server through a familiar store interface. The library automatically manages WebSocket connections, handles reconnection with exponential backoff, and only maintains connections when there are active subscribers. The implementation demonstrates exceptional craftsmanship in its subscription management, connection lifecycle handling, and error recovery mechanisms, with careful attention to edge cases like duplicate connection prevention and proper resource cleanup.
The LLM identified 1 files worth highlighting for reuse:
The websocketStore function creates a Svelte store that synchronizes with a WebSocket backend. It implements automatic reconnection with exponential backoff ([2000, 5000, 10000, 30000, 60000] ms), properly manages connection lifecycle based on subscriber count (opening when subscriptions exist, closing when none), handles WebSocket events correctly (onmessage, onclose, onerror), uses openPromise to prevent duplicate connection attempts, and provides a clean API for sending and receiving JSON data. This component is highly reusable as it has no internal dependencies beyond the browser WebSocket API and can be dropped into any Svelte project needing real-time state synchronization.