Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Mahmood83367 opened this issue Aug 20, 2024 · 3 comments

Comments

@Mahmood83367
Copy link

Mahmood83367 commented Aug 20, 2024

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
Copy link
Collaborator

ttypic commented Aug 20, 2024

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
Copy link
Author

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
Copy link
Collaborator

ttypic commented Aug 20, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants