Wor Chan
Wor Chan

Reputation: 169

Electron.js - Send child process' spawn stdout data to renderer in realtime

I have a simple node child process that invokes a script and that script takes time to output some information (kinda like how ping works).

let command = spawn(
      execPath,
      [...args],
      { cwd: null, detached: false }
    );

Then I do a standard console.log for the stdout:

command.stdout.on("data", (stdout) => {
      console.log("Realtime Output: ", stdout.toString());
    });

The issue is, I want to send this realtime output back to renderer process and show it on the frontend. I tried adding an ipcRenderer.send() inside the command.stdout.on() but it doesn't work, and the frontend shows undefined in console.log.

Is there any way to achieve this?

Upvotes: 0

Views: 1206

Answers (1)

Huy Duy
Huy Duy

Reputation: 852

You have to send the stdout from mainWindow through webContents

 mainWindow.webContents.send('output', stdout.toString())

Upvotes: 1

Related Questions