websockets / ws

Simple to use, blazing fast and thoroughly tested WebSocket client and server for Node.js
MIT License
21.33k stars 2.3k forks source link

ws doesn't work with sveltekit's adapter-cloudflare #2210

Closed maemigh closed 3 months ago

maemigh commented 3 months ago

Is there an existing issue for this?

Description

When using adapter-cloudflare with sveltekit, this error comes up: Error: ws does not work in the browser. Browser clients must use the native WebSocket object

It works fine with the same project when running with Vite.

ws version

8.16.0

Node.js Version

v21.5.0

System

System: OS: macOS 14.3.1 CPU: (8) arm64 Apple M1 Memory: 75.34 MB / 16.00 GB Shell: 5.9 - /bin/zsh

Expected result

import to load

Actual result

No response

Attachments

No response

lpinca commented 3 months ago

The error is self explanatory. In the browser you have to use the WebSocket client that ships with the browser, not ws.

maemigh commented 3 months ago

This code does not run in the browser. The error occurs when using ws inside a +server.ts route.I think it is probably because the adapter-cloudflare is built with

                const result = await esbuild.build({
                    platform: 'browser',
                    conditions: ['worker', 'browser'],
                    sourcemap: 'linked',
                    target: 'es2022',
                    entryPoints: [`${tmp}/_worker.js`],
                    outfile: `${dest}/_worker.js`,
                    allowOverwrite: true,
                    format: 'esm',
                    bundle: true,
                    loader: {
                        '.wasm': 'copy'
                    },
                    external,
                    alias: Object.fromEntries(compatible_node_modules.map((id) => [id, `node:${id}`])),
                    logLevel: 'silent'
                });

where platform is browser. Guess I'll open an issue there.

lpinca commented 3 months ago

Yes, I guess the issue arises from that platform: 'browser' option.

lpinca commented 3 months ago

I'm closing this as answered.