Registry / messaging / svelte-websocket-stores

svelte-websocket-stores

JSON →
library1.2.4jsnpmunverified

Synchronizes primitive-typed Svelte stores across a WebSocket connection. Version 1.2.4 requires Svelte 3, 4, or 5. Designed for touch panels and other scenarios where a Svelte client mirrors server-side state. Unlike larger state management libraries, it focuses on real-time sync of booleans, numbers, strings, and JSON-serializable values using a simple message format. Ships TypeScript types. Maintained by xt449.

npm install svelte-websocket-stores
INSTALL
IMPORT
SIG · SVELTE-WEBSOCKET-S
S
svelte-websocket-stores
messagingjavascriptv1.2.4
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

WebSocketWrapper
import { WebSocketWrapper } from 'svelte-websocket-stores'
const WebSocketWrapper = require('svelte-websocket-stores')
ESM-only package; CommonJS require will fail.
WebSocketWrapper.instance
const wrapper = new WebSocketWrapper(url, scope); wrapper.start();
WebSocketWrapper.instance.start()
The instance is NOT a singleton; you must create a new instance.
webSocketStore
const store = wrapper.webSocketStore<boolean>('id');
import { webSocketStore } from 'svelte-websocket-stores'
webSocketStore is an instance method on WebSocketWrapper, not a named export.

Shows initialization of WebSocketWrapper, creation of a typed store, and reactive logging.

import { WebSocketWrapper } from 'svelte-websocket-stores'; const wrapper = new WebSocketWrapper('ws://localhost:8080', 'client1'); wrapper.start(); // Create a writable store synced with server const count = wrapper.webSocketStore<number>('counter.count'); $: console.log('Count updated:', $count); // Update locally (sends to server) function increment() { $count = ($count || 0) + 1; }
Debug
Known issues
breakingThe store value is only synced when the WebSocket connection is open. Lost connection causes stale values.
fix
Implement reconnection logic or use a wrapper that queues updates.
affects: >=1.0.0
deprecatedNo deprecations known in current version.
gotchaThe scope field in messages must match the client's scope exactly (e.g., 'tp1') or be 'global'. Messages with mismatched scope are silently discarded.
fix
Ensure server sends messages with correct scope or use 'global' for broadcast.
affects: >=1.0.0
gotchaOnly primitive types (boolean, number, string) and JSON-serializable objects are supported. Functions, Symbols, and undefined will fail.
fix
Serialize complex types to JSON strings before storing.
affects: >=1.0.0
gotchaThe library assumes the server follows the expected message protocol. Sending malformed JSON or missing fields will cause runtime errors.
fix
Use the documented Message type on the server side.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'webSocketStore')
Trying to call webSocketStore on an undefined wrapper (not instantiated or not exported correctly).
fix
Ensure WebSocketWrapper is instantiated and exported: const wrapper = new WebSocketWrapper(...); export { wrapper };
Uncaught ReferenceError: $ is not defined
Using $ prefix outside a Svelte component's <script> or without Svelte compiler.
fix
Use wrapper.webSocketStore in a .svelte file's <script> block, or use store.subscribe() in plain JS.
TypeError: store is not a function
Trying to call webSocketStore as a function instead of a method on an instance.
fix
Correct import: const count = wrapper.webSocketStore<number>('key');
Module not found: Can't resolve 'svelte-websocket-stores'
Package not installed or incorrect import path.
fix
Run npm install svelte-websocket-stores and use import { WebSocketWrapper } from 'svelte-websocket-stores'.
Upgrade
Version history
1.2.4latest on npm
Audit
Dependencies
svelterequiredPeer dependency: required to use Svelte stores ($) syntax.
Agent activity
17 hits · last 30 days
node
14
Amazon
1
OpenAI (training)
1
Resources
svelte-websocket-stores — npm install svelte-websocket-stores · libregistry