Registry / messaging / svelte-websocket-store

svelte-websocket-store

JSON →
library1.2.15jsnpmunverified

A Svelte store that synchronizes with a WebSocket backend. Current stable version is 1.2.15, with no specific release cadence (community-maintained). Key differentiators: built on Svelte writable stores, automatic JSON serialization/deserialization, JWT authentication support, and seamless integration with Svelte 5. Peer dependency on Svelte ^5.45.8. Suitable for real-time applications where server state must be mirrored in Svelte components via reactive stores.

npm install svelte-websocket-store
INSTALL
IMPORT
SIG · SVELTE-WEBSOCKET-S
S
svelte-websocket-store
messagingjavascriptv1.2.15
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.

default
import websocketStore from 'svelte-websocket-store'
const websocketStore = require('svelte-websocket-store')
Package is ESM-only; CommonJS require will fail.
writable
import { writable } from 'svelte/store'
Svelte's writable store is used internally; you may also need it for local state.
get
import { get } from 'svelte/store'
Use get() to read store value outside Svelte components.

Shows how to import and use websocketStore, send data via assignment, and read reactive store value.

import websocketStore from 'svelte-websocket-store'; import { onMount, onDestroy } from 'svelte'; const initialValue = { message: 'connected' }; export const myStore = websocketStore('wss://echo.websocket.org', initialValue, ['option1']); onMount(() => { // Send data via the store assignment myStore.set({ content: 'hello', other_values: 'data' }); }); onDestroy(() => { // Optional: close WebSocket if store provides a cleanup method // Check documentation - not provided in basic API }); // In Svelte template: // <p>{$myStore.content}</p>
Debug
Known issues
breakingSvelte 5 required; store reactivity based on Svelte 5 runes may differ from Svelte 4. Ensure your project uses Svelte ^5.45.8.
fix
Upgrade to Svelte 5 and use $store syntax in .svelte files.
affects: >=1.0.0
breakingWebSocket connection is managed internally; store assignment triggers JSON send. Do not manually manage WebSocket unless you close the store.
fix
Use store's set method or assignment to send data; the library handles the WebSocket lifecycle.
affects: >=1.0.0
gotchaInitial value is overwritten by first WebSocket message if server sends state immediately.
fix
Initialize with a placeholder or handle server message in a derived store.
affects: >=1.0.0
gotchaThird parameter 'options' is an array of strings, not an object. Passing an object may cause unexpected behavior.
fix
Use an array of strings for options (e.g., ['jwt', 'reconnect']).
affects: >=1.0.0
Errors
Common errors & fixes
Failed to resolve import "svelte-websocket-store" from "src/lib/stores.js". Does the file exist?
Package not installed or ESM-only import.
fix
npm install svelte-websocket-store and ensure project uses ESM (type: module or .mjs).
Must use import to load ES Module: /node_modules/svelte-websocket-store/index.js
Using require() on an ESM-only package.
fix
Use import statement instead of require().
Cannot read properties of undefined (reading 'subscribe')
Initial value is undefined and store expects an object or primitive.
fix
Provide an initial value, e.g., websocketStore(url, {}, []).
Upgrade
Version history
1.2.15latest on npm
Audit
Dependencies
svelterequiredPeer dependency required for store functionality (writable store)
Agent activity
25 hits · last 30 days
node
22
OpenAI (training)
1
Resources
svelte-websocket-store — npm install svelte-websocket-store · libregistry