I.K.
I.K.

Reputation: 464

How to Close React App on Electron App Quit

I have a Electron + React app that is working on port 3000. When I call the app.quit method from Electron's start.js file, it exits from the Electron but I can still see that 0.0.0.0:3000 is in LISTENING state from netstat -ao.

I am working on Windows 10 and I tried app.exit(0) from electron file, however React app is still reachable from Browser and netstat -ao shows 0.0.0.0:3000 in LISTENING state.

  "scripts": {
    "start": "nf start -p 3000",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "electron": "electron .",
    "electron-start": "node src/start-react",
    "react-start": "SET BROWSER=none&&react-scripts start",
    "pack": "build --dir",
    "dist": "npm run build && build",
    "postinstall": "install-app-deps",
    "preinstall": "npm install -g foreman"
  },

I want to close ReactApp completely after/before calling app.quit() of Electron Here is the code I use for closing app.

ipcMain.on('closeApp', (evt, arg) => {
    app.exit(0)
});

Upvotes: 4

Views: 2550

Answers (1)

Kiran Maniya
Kiran Maniya

Reputation: 8979

You are seeing the 0.0.0.0:3000 still listening because you didn't close the connection properly. You need to close the react app server before quit the app. There is an event named before-quit in electron app triggered exactly before app quits. You can utilize the callback to close server. To look up the pid attached to port 3000, There is a find-process package, after finding the pid, kill the process. Consider the given example.

const find = require('find-process');

app.on('before-quit' , (e) => {
    find('port', 3000)
      .then(function (list) {
      if(list[0] != null){
          process.kill(list[0].pid, 'SIGHUP');
      }
    }.catch((e) => {
        console.log(e.stack || e);
    });
});

Upvotes: 2

Related Questions