arlac77 / svelte-websocket-store Goto Github PK
View Code? Open in Web Editor NEWsvelte store with a websocket backend
License: BSD 2-Clause "Simplified" License
svelte store with a websocket backend
License: BSD 2-Clause "Simplified" License
When running my app with yarn run start
, the websocketStore
connects to localhost:38535
regardless of what I have in the url
parameter:
import websocketStore from 'svelte-websocket-store';
let myStore = websocketStore("ws://localhost:8080/ws", initialValue, []);
I am using the websocket store with a Go backend, not Node, so this is not helpful in my situation, though I can see how it would be if I were using Node. It does connect to my specified URL as well, but any commands seem to be sent to only the Node server Yarn is running.
Is there a way to disable this behaviour?
Instead of keeping a single connection, the store keeps creating new connections whenever its value is updated.
This line might be the cause:
svelte-websocket-store/src/index.mjs
Line 79 in 4f93b0e
Here's a screenshot:
Reproduction:
https://codesandbox.io/s/svelte-websocket-store-bug-reproduction-jn0qg
I am wondering what this was developed for. Does it help with refreshing data instead of polling for example? Thanks!
(!) Missing global variable name
Use output.globals to specify browser global variable names corresponding to external modules
svelte-websocket-store (guessing 'websocketStore') ver 1.1.3
I try to use this library, but I get error in browser:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'readyState')
at Object.set (index.mjs:80:18)
My code:
const initialValue = { };
if (browser) {
const myStore = websocketStore("ws://localhost:3000/foo", initialValue);
myStore.set({
content: "to be saved",
other_values: "all"
});
}
I use latest Svelte's:
❯ npm list
[email protected] /home/jacek/storage/developSSD/epsvelte
├── @sveltejs/[email protected]
├── [email protected]
└── [email protected]
In my Svelte app running test with Jest fails because the index file is of this library is of type 'mjs'.
The following error is thrown:
> jest src
FAIL src/App.test.js
● Test suite failed to run
Cannot find module 'svelte-websocket-store' from 'src/App.svelte'
It would be nice if you could add a section to the Readme explaining how to setup Jest properly to work with this library.
yes, gzip, please add support for gzip websocket messages
[rollup-plugin-svelte] The following packages did not export their `package.json` file so we could not check the "svelte" field. If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.
- svelte-websocket-store
LiveReload enabled
created public/build/bundle.js in 321ms
[2021-02-13 06:25:39] waiting for changes...
[rollup-plugin-svelte] The following packages did not export their `package.json` file so we could not check the "svelte" field. If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.
- svelte-websocket-store
LiveReload enabled
(!) Plugin node-resolve: Could not resolve import "svelte-websocket-store/src/index.mjs" in /Users/test/github/svelte-huobi-client-app/src/App.svelte using exports defined in /Users/test/github/svelte-huobi-client-app/node_modules/svelte-websocket-store/package.json.
(!) Unresolved dependencies
https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency
svelte-websocket-store/src/index.mjs (imported by src/App.svelte)
(!) Missing global variable name
Use output.globals to specify browser global variable names corresponding to external modules
svelte-websocket-store/src/index.mjs (guessing 'websocketStore')
created public/build/bundle.js in 343ms
I get the following error when trying to import this module.
internal/modules/cjs/loader.js:984
throw new ERR_REQUIRE_ESM(filename);
^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\...\node_modules\svelte-websocket-store\src\index.mjs
at Module.load (internal/modules/cjs/loader.js:984:11)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Module.require (internal/modules/cjs/loader.js:1026:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (C:\Users\...\__sapper__\dev\server\server.js:10:38)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) {
code: 'ERR_REQUIRE_ESM'
}
I'm not sure what's going on here? I'm using this line in my Sapper route code:
import websocketStore from 'svelte-websocket-store'
```
If added to a sveltekit page/component that SSR then it fails.
Suggest to make it check if not browser and handle exit gracefully
There's really no need for forced JSON? Let it be up to the usecsse. Maybe a derived version forcing json?
file:///{{url}}/node_modules/svelte-websocket-store/src/index.mjs:54
socket = new WebSocket(url, socketOptions);
^
ReferenceError: WebSocket is not defined
at open (file:///{{url}}/node_modules/svelte-websocket-store/src/index.mjs:54:5)
at Object.subscribe (file:///{{url}}/node_modules/svelte-websocket-store/src/index.mjs:84:7)
at Proxy.subscribe (/{{url}}/node_modules/svelte/internal/index.js:59:25)
at eval (/src/routes/dashboard.svelte:36:48)
at Object.$$render (/{{url}}/node_modules/svelte/internal/index.js:1758:22)
at Object.default (/.svelte-kit/generated/root.svelte:58:129)
at eval (/src/routes/__layout.svelte:85:75)
at Object.$$render (/{{url}}/node_modules/svelte/internal/index.js:1758:22)
at eval (/.svelte-kit/generated/root.svelte:50:126)
at $$render (/{{url}}/node_modules/svelte/internal/index.js:1758:22)
Node.js v18.2.0
Any way to show a status if websocket is open or closed to allow for an alert in ui?
Most of the time, discussions prevent users from opening unnecessary tickets or bugs.
It might be useful to have a function on the store that reopens the socket to a new URL, while preserving subscriptions.
For example, I'd like to use this with @polkadot/api@beta, instead of the current setup of my store as below:
import { readable } from 'svelte/store';
const { ApiPromise, WsProvider } = require('@polkadot/api');
const wsProvider = new WsProvider('wss://kusama-rpc.polkadot.io/');
let api = ApiPromise.create({ provider: wsProvider });
export const getTotalIssuance = readable(null, async function start(set) {
set(await (await api).query.balances.totalIssuance());
return function stop() {};
});
export const getGenesisHash = readable(null, async function start(set) {
set((await api).genesisHash.toHex());
return function stop() {};
});
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.