0

Electron can execute low-level commands through the following NodeJS APIs:

These APIs can only be executed on the Main process. Which means they won't work on the Preload and Renderer processes.

Given that you have the tier of processes: MainPreloadRenderer

How can the Renderer process capture the stdout and stderr messages happening on the Main process, line per line as they happen?

Basically, how to update the Renderer process with the outputs/progress happening from the Main process while having in mind you have to expose some APIs via the Preload?

Abel Callejo
  • 13,779
  • 10
  • 69
  • 84
  • What about [`ipcRenderer.sendSync`](https://www.electronjs.org/docs/api/ipc-renderer#ipcrenderersendsyncchannel-args) and [`ipcMain,on`](https://www.electronjs.org/docs/api/ipc-main#ipcmainonchannel-listener) ? Both can be used to execute a command on `main` and send the output synchronously to `renderer` – cachique Jun 22 '21 at 15:26
  • @cachique Well, [`ipcMain.on()`](https://www.electronjs.org/docs/api/ipc-main#ipcmainonchannel-listener) only listens and doesn't send while [`ipcRenderer.sendSync()`](https://www.electronjs.org/docs/api/ipc-renderer#ipcrenderersendsyncchannel-args) only sends and doesn't listen. It seem to be the opposite of this topic. This topic is about sending a message from the **Main** to **Renderer**. Not the other way around. – Abel Callejo Jun 23 '21 at 01:25
  • 2
    don't you just want to grab the renderer's window and do `window.webContents.send("message", data);` and intercept it in the renderer via `ipcRenderer.on`? – pushkin Jun 23 '21 at 03:38
  • I was just about to comment that @pushkin – cachique Jun 23 '21 at 03:39
  • As you said in your post, execution can only happen in `main`. The thing I think it matters is who starts the action. If it is the `renderer` then you use `ipcRenderer`. If it is `main` then you use `webContents`. I can think of writing a line at a time from a text file or update the total records proccesed of a database so long as examples of your question. Both of them can be started either on `renderer` or in `main`. I did both things on apps as `renderer`'s actions. – cachique Jun 23 '21 at 03:49
  • 1
    Does this answer your question? [How can we send messages main process to renderer process in Electron](https://stackoverflow.com/questions/52124675/how-can-we-send-messages-main-process-to-renderer-process-in-electron) – pushkin Jun 23 '21 at 14:46
  • Partly, this topic would easily look like a duplicate. However, the link you are suggesting only deals with **Main** and **Renderer**, what I am after here is how to make it work with using **Preload** too. This is a `v13.*` and onwards. Do you think that makes a difference? – Abel Callejo Jun 23 '21 at 23:25
  • not really. You can use ipcRenderer the same in preload. – pushkin Jul 01 '21 at 14:31

0 Answers0