ably / ably-js

Javascript, Node, Typescript, React, React Native client library SDK for Ably realtime messaging service
https://ably.com/download
Apache License 2.0
316 stars 55 forks source link

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

Open Mahmood83367 opened 2 months ago

Mahmood83367 commented 2 months ago

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

ttypic commented 2 months ago

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'.

Mahmood83367 commented 2 months ago

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

ttypic commented 2 months ago

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?