Postgres Listen/Notify -Listeners increase per every flow deploy

I'm using pg.Client.Query(LISTEN [Channel]) in a function node.

My flow starts once with inject node.

I had noticed that per every deploy of the flow the number of listeners increases, meaning per every notified postgress event I'm getting event X number of deployments.

I'm a newbie with the node-red and can't find documentation on how the flow deployment process works.

Is there an option to subscribe to a deployment event in the flow and make cleaning, like closing the connection against pg.

Other suggestions?


my test flow

[{"id":"9ae31d61.392cb","type":"function","z":"de517d38.f7c6c","name":"CreateUpdateCustomerEvent","func":"var pg = global.get('pgModule');\n\nconst client = new pg.Client({\n    user: 'rednode',\n    password: 'p6a03a3528dac87de9fe865902dfe1f66bfff3ff551a0d7cd1ef4af3db09b91b5',\n    host: '',\n    port: '5432',\n    database: 'dvh1c9c6mcjrb',\n    ssl: true\n})\n\ntry {\n    client.connect(function(err) {\n        if (err) {\n            node.error(err)\n        }\n        else \n        {\n            // Listen for all pg_notify channel messages\n            client.on('notification', function(msg) {\n               \n                node.send(msg);\n          \n            });\n            \n            client.on('error', (err) => {\n                console.error('something bad has happened!', err.stack)\n            });\n            \n            client.on('end', (err) => {\n                console.error('session ended', err.stack);\n                client.on('notification', function(msg) {\n               \n                    node.send(msg);\n          \n                });\n                client.query('LISTEN customer_changed');\n            });\n            \n            \n  \n        // Designate which channels we are listening on. Add additional channels with multiple lines.\n        client.query('LISTEN customer_changed');\n  \n      \n                        \n           /* client.query(\"select * from newdev.customer__c where name = 'workdude3'\", \n            function (err,res) {\n              \n              //console.log(res);\n              client.end();// closing the connection;\n              \n              if(err){\n                   console.log(err);\n                   node.error(err);\n               }\n               else {\n              \n                msg.payload = [res.rows[0]];\n                console.log('***** payload' + msg.payload)\n              }\n            });\n        */\n        }\n    \n    }\n);\n}\ncatch(err) \n{\n    console.log(err);\n    node.error(err);\n}\n \nreturn null;","outputs":1,"noerr":0,"x":410,"y":320,"wires":[["b1ba81ef.822b6"]]}]

If you are creating resources that need cleaning up in a function node, then you need to add a handler for the close event as described in this section of the docs: