jdiamond/chrome-native-messaging

How do I send a message to chrome extension

slifin opened this issue · 4 comments

Sorry if this seems naive,

I've read: https://github.com/jdiamond/chrome-native-messaging/blob/master/host/my_host.js
and implemented it in code and the application runs ok, but doesn't seem to output anything when I run it just stays open

How do I get my node app to output a single message to chrome extension that meets the criteria of "JSON, UTF-8 encoded and is preceded with 32-bit message length in native byte order" using this package?

The host staying open is normal, but you're not really supposed to launch it yourself. You can launch it yourself for testing purposes (see the Testing section in the README).

Normally, Chrome launches the host when you send a message from your app. Only then does the host have a chance to reply. Chrome will eventually decide when to shut the host down, but I don't know the exact rules. So far, I've been sending a message from my app which launches the host and my host has been able to send back multiple messages before getting shut down without any trouble. The demo host and app do that, too, when you send a "readdir" message.

Let me know if this helps or if you have any other questions.

yeah I'm opening the app for testing purposes I'd like to make a tool that can call the chrome extension every time the application is called for live reloading in sublime text

it would go sublime text build -> opens node application -> messages -> chrome extension -> chrome extension reloads the active tab

Since my last message I've been able to get a message back to the chrome extension using this code:

#!/usr/bin/env node

var nativeMessage = require('./chrome-native-messaging/index');
var output = new nativeMessage.Output();
output.write('{"text":"hello world"}');
process.stdin
.pipe(new nativeMessage.Input())
.pipe(new nativeMessage.Transform(function(msg, push, done) {
    var reply = getReplyFor(msg);
    push(reply);               
    done();                    
}))
.pipe(output)
.pipe(process.stdout);

I realise this is outside of scope, but do you know if there is a way within the windows operating system to pipe a message to the stdin of an application that is already running? (i.e. this one)

Hey man, I think I get what you're doing. The example app I made was based on the app I made for a client and all of my interactions with the native messaging host were request/response--the server was never pushing messages on its own, but I was just messing around with it and I think it works.

I just buffed the example app to give you more control over connecting and disconnecting and also included an example of subscribing to events emitted by the "server".

You were totally on the right track with just writing to the output stream. Check out the new my_host.js.

As for piping stdin to another process, I'm not sure how that could work. I would try just piping stdin to a socket and the socket's output back to stdout like this:

process.stdin.pipe(net.connect(port)).pipe(process.stdout)

On the actual server, you'd pipe the socket for each client into the same input/transform/output chain like in my_host.js:

net.createServer(function(client) {
    // create the streams...
    client.pipe(input).pipe(transform).pipe(output).pipe(client);
}).listen(port)

Hope this helps.

Thank you I'll give this ago asap :)