# How to stop Node-RED in my Electron application? (RED.stop() command doesn't stop anything)

**URL:** <https://discourse.nodered.org/t/how-to-stop-node-red-in-my-electron-application-red-stop-command-doesnt-stop-anything/62246>\
**Category:** Core Development\
**Created:** [5 May 2022 17:29 UTC](https://discourse.nodered.org/t/how-to-stop-node-red-in-my-electron-application-red-stop-command-doesnt-stop-anything/62246 "2022-05-05T17:29:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SebCanet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sebcanet/32/38069_2.png) [@SebCanet](https://discourse.nodered.org/u/SebCanet)\
**Post date:** [5 May 2022 17:29 UTC](https://discourse.nodered.org/t/how-to-stop-node-red-in-my-electron-application-red-stop-command-doesnt-stop-anything/62246/1 "2022-05-05T17:29:04Z")

</div>

Hello everyone,  
I am developping software to help program microcontroller boards and I want to integrate Node-RED into my Electron application to retrieve data sent by my boards.  
I used the example code ([https://nodered.org/docs/user-guide/runtime/embedding](https://nodered.org/docs/user-guide/runtime/embedding)) and it works fine, but I can't find how to stop the Node-Red runtime (the RED.stop() command doesn't stop anything) or even the express server. I have the impression that it's the same solution everywhere: close the application window. But this is not a solution in my opinion...  
I would like the user to be able to launch a popup (node-red launches in the background) and close it (and thus close node-red and the express server). This seems impossible...  
I tried to list the connections to close them one by one but it doesn't work, I tried to replace Express with Fastify but the problem is the same. In short....HELLLLPPP !!!!  
Thank you very much in advance.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [6 May 2022 04:57 UTC](https://discourse.nodered.org/t/how-to-stop-node-red-in-my-electron-application-red-stop-command-doesnt-stop-anything/62246/2 "2022-05-06T04:57:46Z")

</div>

Are you using MQTT?

Then it might be related to this issue:

> <https://github.com/node-red/node-red/issues/3593>
>
> \### Current Behavior
> 
> A Node red instance with a single flow consisting of a sin…gle mqtt-in node and associated broker node prevents the NR instance from exiting cleanly.  
> 
> 
> 
> 
> 
> 
> 
> \### Expected Behavior
> 
> Calling RED. stop() should cleanly terminate all flows and allow node to exit,
> 
> The issue seems to be that the mqtt code (10-mqtt.js) doesn't call client.close() during its disconnect handler.
> 
> https://github.com/node-red/node-red/blob/329008bf6d4325e2ed47437c4824f91202b7ca3f/packages/node\_modules/%40node-red/nodes/core/network/10-mqtt.js#L831
> 
> Changing this to the following fixes the issue
> \`\`\`
> if(node.closing) { node.client.end(); return \_callback(); }
> \`\`\` 
> 
> 
> \### Steps To Reproduce
> 
> The following code reproduced the problem. 
> 
> \`\`\`
> const http = require('http');
> const express = require("express");
> const RED = require("node-red");
> const fs = require('fs');
> const os = require('os');
> const path = require('path');
> const whyIsNodeRunning = require('why-is-node-running')
> 
> const mqttFlow = {
> label: 'mqtt',
> disabled: false,
> info: '',
> nodes: \[
> {
> type: 'mqtt in',
> name: 'mqtt',
> id: 'b021cb01-cbd5-11ec-8a3d-47d2cdb2f658',
> topic: 'myTopic',
> broker: 'b021f210-cbd5-11ec-8a3d-47d2cdb2f658'
> },
> {
> type: 'mqtt-broker',
> name: '',
> id: 'b021f210-cbd5-11ec-8a3d-47d2cdb2f658',
> keepalive: 60,
> port: '1883',
> broker: 'localhost'
> },
> \]
> }
> 
> process.once('SIGINT', function () {
> console.log('SIGINT received...');
> close();
> });
> 
> process.once('SIGTERM', function () {
> console.log('SIGTERM received...');
> close();
> });
> 
> var server;
> const userDir = fs.mkdtempSync(path.join(os.tmpdir(),'test-'));
> 
> 
> async function start () {
> let app = express();
> server = http.createServer(app);
>     
> var settings = {
> httpAdminRoot:"/red",
> httpNodeRoot: "/api",
> userDir: userDir,
> };
> 
> RED.init(server, settings);
> 
> app.use(settings.httpAdminRoot,RED.httpAdmin);
> app.use(settings.httpNodeRoot,RED.httpNode);
> 
> // Start Node Red
> await RED.start()
> 
> RED.runtime.events.once('flows:started', () =\> {
> RED.runtime.flows.addFlow({flow: mqttFlow});
> })
> 
> server.listen(2000);
> console.log(\`Node Red server running on port 2000\`)
> }
> 
> 
> async function close() {
> await RED.stop();
> await server.close();
> fs.rmSync(userDir, {force: true, recursive: true})
> setTimeout(() =\> {whyIsNodeRunning(console)}, 1000)
> }
> 
> start();
> \`\`\`
> 
> Running this from the command line and stopping it with Ctrl-C leads to the node process hanging and "why-IsNodeRunning" shows that the mqtt reconnect timer is still active.
> 
> \`\`\`
> # Timeout
> node:internal/async\_hooks:203                           
> node:internal/async\_hooks:474                           
> node:internal/timers:162                                
> node:internal/timers:196                                
> /home/phil/CT/NRBug/node\_modules/mqtt/lib/client.js:1123 - that.reconnectTimer = setInterval(function () {
> /home/phil/CT/NRBug/node\_modules/mqtt/lib/client.js:390 - this.\_setupReconnect()
> /home/phil/CT/NRBug/node\_modules/mqtt/lib/client.js:477 - that.emit('close')
> \`\`\`
> \[nrBug.tar.gz\](https://github.com/node-red/node-red/files/8631673/nrBug.tar.gz)
> 
> 
> 
> \### Example flow
> 
> \`\`\`
> \[{"id":"075db3f8c5ea6d56","type":"tab","label":"mqtt","disabled":false,"info":""},{"id":"b021cb01-cbd5-11ec-8a3d-47d2cdb2f658","type":"mqtt in","z":"075db3f8c5ea6d56","name":"mqtt","topic":"myTopic","broker":"b021f210-cbd5-11ec-8a3d-47d2cdb2f658","inputs":0,"x":0,"y":0,"wires":\[\[\]\]},{"id":"b021f210-cbd5-11ec-8a3d-47d2cdb2f658","type":"mqtt-broker","z":"075db3f8c5ea6d56","name":"","broker":"localhost","port":"1883","keepalive":60}\]
> \`\`\`
> 
> 
> \### Environment
> 
> \- Node-RED version: 2.2.2
> \- Node.js version: v16.4.0
> \- npm version: 7.18.1
> \- Platform/OS: Ubuntu 20.04.1
> \- Browser:Version 98.0.4758.102 (Official Build) (64-bit)

---

<div class="post-metadata">

**Author:** ![SebCanet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sebcanet/32/38069_2.png) [@SebCanet](https://discourse.nodered.org/u/SebCanet)\
**Post date:** [8 May 2022 11:15 UTC](https://discourse.nodered.org/t/how-to-stop-node-red-in-my-electron-application-red-stop-command-doesnt-stop-anything/62246/3 "2022-05-08T11:15:26Z")

</div>

Thanks a lot for answering, but it occurs even doing nothing. I lanuch it in Electron window, a 2nd from my mainwindow, and when I close this Electron window server is still running and still can use it with a browser and connect to it.  
I would like to be able to stop it comepletely without closing my main application.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [7 July 2022 11:16 UTC](https://discourse.nodered.org/t/how-to-stop-node-red-in-my-electron-application-red-stop-command-doesnt-stop-anything/62246/4 "2022-07-07T11:16:24Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
