john_ryan
john_ryan

Reputation: 1787

Chrome Native Messaging with electron app

I'm trying to set up communication between a chrome extension and an electron app on OSX.

I have the JSON file in ~/Library/Application\ Support/Google/Chrome/NativeMessagingHosts/com.company.app.json

    {
        "name": "com.company.app",
        "description": "MyApp",
        "path": "/Users/johnryan/Desktop/Code/electron-app/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar",
        "type": "stdio",
        "allowed_origins": ["chrome-extension://xxxxxxxxxxxx"]
    }

On the chrome extension i have a simple native messaging call:

    chrome.runtime.sendNativeMessage('com.company.app',
      { text: "Hello" },
      function(response) {
        console.log("Received " + response);
      });

then in the main.development.js i have:

    var readline = require('readline');
    var rl = readline.createInterface({
      input: process.stdin,
      output: process.stdout,
      terminal: false
    });
    
    rl.on('line', function(line){
        console.log("RECEIVED:" + line);
    })

However, when i execute the sendNativeMessage I don't see anything in the logs. Is there something i'm missing here?

Upvotes: 2

Views: 3043

Answers (1)

Deepak Jha
Deepak Jha

Reputation: 1609

Recently stumbled upon this issue, and found out that in windows platform access to stdin and stdio from main thread is not supported. Temporarily I have made it work by starting the http server inside my electron application and listening from http port in windows platform.

However the same should work on MAC OS just build your electron application using electron-packager and give path of your exe(exe of your electron app) in the path variable, as given below.

    {
        "name": "com.company.app",
        "description": "MyApp",
        "path": "/Users/johnryan/Desktop/Code/electron-app/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.exe",
        "type": "stdio",
        "allowed_origins": ["chrome-extension://xxxxxxxxxxxx"]
    }

In order for it to work in windows see this post it might have a solution,

GitHub -Issue of stdin and stdio in electron application

Upvotes: 4

Related Questions