arlac77 / svelte-websocket-store

svelte store with a websocket backend
BSD 2-Clause "Simplified" License
286 stars 21 forks source link

ReferenceError: WebSocket is not defined #648

Open somiryu opened 2 years ago

somiryu commented 2 years ago

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

somiryu commented 2 years ago

Using Svelte-kit

manuganji commented 2 years ago

I don't see any lines in the source that make it work for the node js environment. The index.mjs directly refers to WebSocket which would only work if you're inside the browser. So this doesn't work for Svelte Kit (at least as of today).

JDR42 commented 2 years ago

FYI, to use with SvelteKit you can employ conditionals with browser from $app/env to ensure this library is only attempted to run on the client-side.