Banjerr
Banjerr

Reputation: 121

How can I send information from a web server to an Electron app?

I am in the process of building an electron which will eseentially provide "notifications" when a certain form is filled out in a certain service that is connected via oAuth2.

I am having trouble, however, finding any information about the process that (i think) will need to happen.

I am thinking...

a user submits form A from this web service

that web service CAN (not sure if this is the way to go) do a HTTP post to my server

then I was thinking that my server could send that info over to the electron app. I've looked into 'https://www.npmjs.com/package/electron-workers', but i'm still unclear if this could help solve my problem.

Any help or even a nudge in the right direction would be greatly appreciated!

Upvotes: 1

Views: 2962

Answers (2)

martpie
martpie

Reputation: 6080

You have multiple way to do that, but this can only simply be done from the renderer process from Electron (basically your front-end),

Use a web socket for real-time application

Web socket is a technology that enable real-time communication between server and client. The most famous is socket.io. You can have a look at Web Sockets API too.

Set an interval that do an Ajax request to your server

var checkNotifications = function() {

    var r = new XMLHttpRequest();

    r.open("POST", "url/of/your/notifications", true);

    r.onreadystatechange = function () {
        if (r.readyState != 4 || r.status != 200) return;
            alert("Success: " + r.responseText);
        };

    r.send();
}


setInterval(checkNotifications, 5000); // every 5 seconds

Upvotes: 2

charles ross
charles ross

Reputation: 572

Electron is running a local node http server, but it's not a public address. Max Ogden has mentioned - I think in the "State of Electron" youtube talk - installing a "headless" electron app on a public webserver to do some service related stuff, but basically you have to have a reachable address. Do you want a desktop webserver to have a public URL?

Upvotes: 0

Related Questions