Registry /
messaging / svelte-websocket-stores
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.fixImplement 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.fixEnsure 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.fixSerialize 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.fixUse 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).
fixEnsure 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.
fixUse 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.
fixCorrect import: const count = wrapper.webSocketStore<number>('key'); Module not found: Can't resolve 'svelte-websocket-stores'
Package not installed or incorrect import path.
fixRun npm install svelte-websocket-stores and use import { WebSocketWrapper } from 'svelte-websocket-stores'. Audit
Dependencies
svelterequiredPeer dependency: required to use Svelte stores ($) syntax.