-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
fix: electron main and renderer process detection #1879
base: main
Are you sure you want to change the base?
Conversation
I can check later today. Thanks for mentioning me! Maybe somebody could implement a test with playwright? https://www.electronjs.org/docs/latest/tutorial/automated-testing#using-playwright |
Errors
|
@axi92 It's all right now |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix lint issues: npm run lint-fix
I checked the current branch and it breaks for me. What was your vision of an simple electron app? |
@axi92 An automated test of course with a dedicated GH workflow like for browser tests |
@robertsLando I created #1883 as a poc that electron tests are possible. |
In fact, this test should not be placed here for judgment. Because the rendering process is also a browser environment, since it is a browser environment, it should go the browser logic. Otherwise, the ws module may be mistakenly loaded in the browser environment of the rendering process, and the ws module cannot run in the browser environment. If you do need to use the mqtt protocol in an electron application, it is recommended to put the connection establishment logic in the main process, and then use the interprocess communication mechanism to interact between the row rendering process and the main process. The rendering process must have a window and a document, but not necessarily a Node.js environment, depending on the configuration at instantiation time. With the main rendering process's responsibilities clearly divided, the browser environment's detection mechanism can maintain the previous standard judgment logic. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lint is failing, I think you have to exclude electron-tests directory from tsconfig
I'm not an electron user but I think it makes sense. Also mqtt-explorer (an electron application that uses mqtt) seems to do it this way |
If you use electron in its best practice mode that is the case, yes I agree with you. On the other hand if you do ignore that on purpose because you choose to, I don't know if it is a good option to deny the user that option. The workaround we used before there was electron support was to patch mqtt after |
@axi92 I'm not blocking this, I just would like to be sure it works for all scenarios :) |
The Electron main process owns all Node environments and can establish connections according to the logic of non-browser environments. The rendering process belongs to the browser environment and should follow the logic of the browser environment to establish the connection. Just because the rendering process has more process, Node, and other accessible environments than the normal browser environment, it should not be attributed to the Node environment. In this cross-end application, it is up to the user to decide which way to establish the connection. According to the needs, the connection can be established in the main process or the rendering process, rather than the application library to force detection to determine the protocol. According to the above convention, the is-browser.ts file is purely used to detect the browser environment, regardless of whether there are other factors, such as the main process or the rendering process. |
@GuoSirius I understand, so is there a way to allow both you and @axi92 being able to use mqtt with electron as you want? I don't want to break anyone I just would like to allow everybody using this lib as they prefer, maybe a way to override the is_browser check could work? with an env var maybe. Giving that your solution seems the most correct one and I agree with you we could use yours to detect the electron rendering process and consider it browser and in order to also allow @axi92 using it like he want add an env var to allow users forcing the value of is_browser. What do you think? cc @axi92 |
Override is a good option! Either with options or an ENV var as long as it is possible to override the check I am fine with it 👍 |
Ok so @GuoSirius would you like to add the override with the env var to this PR please? |
Could this be closed now that we have |
Any updates for this issue? forceNativeWebSocket seems can't solve this. I can not connect to broker with tcp or mqtt protocol in a node integrated renderer process. |
When I specify to use the TPC or MQTT protocol for connection, it automatically adjusts to use the WS protocol instead. It doesn't seem reasonable. In my opinion, if the caller specifies a protocol, the library should simply use the specified protocol to establish connection. If the current environment doesn't support it, just throw an exception for troubleshooting. Environment detection may be useful for some users, but this mechanism cannot achieve 100% accuracy. Therefore, I believe that an option is needed to tell the library directly bypass environment detection and work solely in the way specified by caller. |
@ericlogic forceNativeWebsockets is used to force MQTTjs to use browser's Websocket instead of the ws module, your problem seems related due to MQTTjs detecting your env as a browser when it is not, would you like to submit a PR to fix this? |
@robertsLando Yes, you are right. I did some research and it's a little complicated. First, isStandardBrowserEnv method isn't work as expected. It is executed when is-browser.ts is imported, at that time, "process" object is an empty object. But it works OK at runtime. After that, when I walked around the isBrower check, The library throwed an Error.
The error is throwed in the mqtt.esm.js but I can't find any related code in source code. It seem that there is a lot of work to do. |
Fixes #1877