I'm writing an Electron application which needs to send quite some data every ~25ms from the renderer to a separate Node process started in the main process with the native fork
module.
The data looks like this: [{ x: int, y: int }, ...]
, with about 1000 points in it (and a bit more information than shown here for brevity).
I started using ipc.send
in the renderer process but it has quite a serious performance penalty: 4.25ms for every ipc.send
.
So I looked into starting a WebSocket with the ws
npm package in the forked Node process, and sending the data over the WebSocket with JSON. This was a lot better. Even improved it further by using avsc
instead of parsing to JSON (from ~4ms to ~1ms).
So the WebSocket solution is working great, but there's a problem: it needs to find a free port and go over the local network. In macOS this also triggers a dialog:
Do you want the application "x.app" to accept incoming network connections?
This dialog in combination with the trickiness of using the local network is something I'd like to avoid if possible.
My question is: does anyone know of a better solution to send data to other processes in Electron that doesn't go over the local network?
The biggest benefit of this module over the built-in IPC is that it enables you to send a message and get the response back in the same call. This would usually require multiple IPC subscriptions. You can use this module directly in both the main and renderer process. Requires Electron 10 or later.
Moreover, Electron has a full-featured API for native system operations. However, Electron applications consume above average resources on user’s computers, which means many developers are looking for lightweight Electron alternatives. In this article, I will be discussing two alternatives to Electron: Tauri and Neutralino.js.
The IPC module is a powerful and necessary tool when navigating Electron’s multi-process architecture. The ability to communicate between processes opens the door for many use cases such as persisting state across multiple windows and managing window states. More information on IPC functionality can be found here:
The sendMessage () function will send a payload via Electron’s IPC to the mainWindow renderer process using the ‘my-ipc-example’ channel. After that, we’ll create a basic file menu with a single action. Replace lines 91 and 92 in main.dev.ts with the following snippet:
So the WebSocket solution is working great, but there's a problem: it needs to find a free port and go over the local network. In macOS this also triggers a dialog: ...
Are you using a loopback address? Loopback addresses should never require a network confirmation. In my local ws electron projects, I use 127.0.0.1:port instead of localhost, and this bypasses internet security dialogs entirely. You can use other 127.x.x.x addresses, as long as you add the appropriate loopback interface on Linux/MacOS. On Windows, 127.x.x.x are already added by default.
My question is: does anyone know of a better solution to send data to other processes in Electron that doesn't go over the local network?
Electron can either use IPC, RPC or network-based comm techs. You've already experienced IPC (RPC is very similar). The fastest, in my experience are the network-based techs. My experience was very similar to yours in that websockets blew the competition away. The network-based comm techs should never trigger LAN/WAN/ISP security measures unless they use LAN/WAN/ISP addresses.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With