Search code examples
javascriptnode.jsspawnraspberry-pi-zero

Node JS spawn of child_process


I'm spawning a command to play an audio stream with mpg123, which works fine. I expose a method to kill the process as well which also works well, but I can't figure out how to get output from the said command.

No events are fired on the child processes stdout, going by the docs it should work - what could be the issue here?

One solution I've found is to set stdio property of spawn to inherit, but then spawn returns null, which is not ideal as I can't create a handle to kill it later.

I'm running this on a Pi Zero with Node 10.24.

export const streamFactory = (cb: (data: string) => void) => {
  try {
    const st = spawn("mpg123", [configManager.getStreamUrl()]);

    st.stdout.on("data", (data) => {
      console.log(data);
      cb(data);
    });

    return {
      close: () => {
        st.kill("SIGINT");
      },
    };
  } catch (e) {
    console.log(e);
  }
};

Solution

  • For some reason, mpg123 outputs its data on stderr, not stdout. Making the switch works like a charm, go figure.