Custom Application Protocol my-app://


#1

Edit: So after searching all day, I’ve discovered that since the service I’m trying to integrate with (PubSubHubbub) is based on web hooks, I need to have an HTTP server able to handle requests coming from the web (not behind a firewall, and or localhost). So using protocols wouldn’t work either. However, I’d still like to integrate protocols into my application somehow. So far, the problem here has nothing to do with any of the JSONP stuff—that was just an end-goal—but instead right now I’m just trying to get the protocol to work. So I would still appreciate some help solving that issue :smile:

Hello! I’m working on an Electron app that needs to hit an external server which uses JSONP to return results. This would be trivial on the web, however since this is a desktop application obviously things get a little hairier. Specifically, I’m trying to integrate with this

After a bit of digging I’ve come across protocols in Electron and from what I can tell this is the solution I’m looking for (please correct me if I’m wrong). This way I can let the external server know that the callback url is my-app://some/path, and then I can handle it on the application side.

This is my code right now:

let app = require('app');

app.on('ready', function () {
  let protocol = require('protocol');

  protocol.registerStringProtocol('my-app', function (request, callback) {
    let url = request.url.substr(7);

    console.log('url: %s', url);

    callback('It works!');
  }, function (err) {
    if (!err) {
      console.log('Registered protocol succesfully');
    }
  });
});

After starting the Electron app, I see in the console: Registered protocol successfully, as one would expect. I then attempt to send a GET request to my-app://test using Safari but I’m getting a “There is no application set to open the URL my-app://test.” alert

Using this bit of code I found online to list all registered protocols, I don’t see my-app in the list

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -dump|egrep "(bindings.*\:$)"|sort

What am I doing wrong? I can hardly find any information online about using this module, and the posts I have found (either on this message board or elsewhere) have not been useful, and they use the old API (although it can be easily converted).

Or, as a friend of mine mentioned, I could be going about this completely wrong and what I may need to do is actually run an HTTP server within my app to handle requests on some sort of obscure port? Desktop apps are very new to me so for all I know he’s right

Thanks!


#2

The protocol module only works for requests made within your Electron application. This stackoverflow question has some suggestions for registering OS level protocols.