Maximilian Braun
Maximilian Braun

Reputation: 73

How to access the electron process from another npm process?

I am made a script that opens a tcp server and listens for incoming requests and then creates a windows notification. Here is the code:

const notifier = require('node-notifier');
const path = require('path');
const net = require('net');
const port = 7070;
const host = '';

const server = net.createServer();
server.listen(port, host, () => {
    console.log('TCP Server is running on port ' + port + '.');
});

let sockets = [];
server.on('connection', function(sock) {
    console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort);
    sockets.push(sock);

    sock.on('data', function(data) {
        var tryCatch = true;
        try {
            JSON.parse(data);
        } catch (err) {
            tryCatch = err;
        }
        if (tryCatch == true) {
            var JSONdata = JSON.parse(data);
            if (JSONdata["action"] == "notification") {
                notifier.notify({
                        title: 'Recived Message',
                        message: JSONdata["message"],
                        icon: path.join(__dirname, 'icon.png'),
                        actions: ["OK", "Abbrechen"]
                    },
                    (err, data) => {
                        console.log('Waited');
                        console.log(JSON.stringify({ err, data }));
                        sock.write(JSON.stringify({ err, data }));
                        sock.write('\r');
                        sock.destroy();
                    }
                );
            } else if (JSONdata["action"] == "closeServer") {
                sock.destroy();
                server.close();
            }
        } else {
            sock.write(tryCatch.message);
            sock.destroy();
        }
    });

    // Add a 'close' event handler to this instance of socket
    sock.on('close', function(data) {
        let index = sockets.findIndex(function(o) {
            return o.remoteAddress === sock.remoteAddress && o.remotePort === sock.remotePort;
        })
        if (index !== -1) sockets.splice(index, 1);
        console.log('CLOSED: ' + sock.remoteAddress + ' ' + sock.remotePort);
        // server.close();
    });
});

That script works without a problem. Now i want to connect it with my electron app. I want to access the electron app from this npm process to for example open a page. But i don't know how to access the electron process from outside and by outside i mean from another npm process. Hope someone could help me or point me in the right direction. I am thankful for every answer or info.

Upvotes: 0

Views: 48

Answers (1)

Maximilian Braun
Maximilian Braun

Reputation: 73

Simple to say. I didnt find a solution on how to access another proccess but instead you can just vorbidd or intercept and interrupt if the user tries to open another window. Then you can instead spawn your own window and use the ipcMain and ipcRender modules to send smth to the requried window.

Upvotes: 1

Related Questions