ably-js icon indicating copy to clipboard operation
ably-js copied to clipboard

When using ably-js in Electron istead of socketio and run the project i have this problem

Open Mahmood83367 opened this issue 1 year ago • 3 comments

ably-node.js:767 11:51:50.942 Ably: WebSocketTransport.connect(): Unexpected exception creating websocket: err = Error: ws does not work in the browser. Browser clients must use the native WebSocket object at new module.exports (http://localhost:8080/main.js:132830:9) at WebSocketTransport.createWebSocket (http://localhost:8080/main.js:31727:12) at http://localhost:8080/main.js:31760:59 at http://localhost:8080/main.js:24972:33 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) Screenshot 2024-08-20 at 11 56 34 AM

┆Issue is synchronized with this Jira Task by Unito

Mahmood83367 avatar Aug 20 '24 08:08 Mahmood83367

Hey @Mahmood83367,

When working with an Electron app, the renderer process (which runs in the context of a web page) requires using npm packages designed for client-side (browser) environments. Our package has different entry points depending on whether it is being used in a browser or Node.js environment.

Your build setup doesn’t automatically select the "browser" field and tries to use Node.js version instead, that's why you are getting this error.

To avoid this you can use build tools and bundlers like Webpack, that automatically prefer the "browser" field when bundling for the renderer process.

Another option is to directly import browser version in your app: import 'ably/build/ably.js'.

ttypic avatar Aug 20 '24 09:08 ttypic

when i import this in my app import 'ably/build/ably.js' i got this error

Module not found: Package path ./build/ably.js is not exported from package /Users/al-raqeem/Documents/GitHub/Clinic360-frontend/node_modules/ably (see exports field in /Users/al-raqeem/Documents/GitHub/Clinic360-frontend/node_modules/ably/package.json) src/contexts/SocketContext.js

Mahmood83367 avatar Aug 20 '24 09:08 Mahmood83367

Your build setup respects the exports fields and prevents importing undeclared modules. So it shouldn't try to use node version of the library in the renderer process. Could you provide a minimal reproducible example or steps for us to reproduce the issue?

ttypic avatar Aug 20 '24 13:08 ttypic