4

I am working on native messaging host. I am able to launch my custom application by using api

var port = chrome.runtime.connectNative('com.my_company.my_application');

I can post message to my custom app by using api

port.postMessage({ text: "Hello, my_application" });

I know they are using input/out stream to send and receive messages. how should my native application(c or c++ exe) will get notify about message received which function/ event should i handle to receive message.

Nik
  • 682
  • 1
  • 8
  • 27

2 Answers2

5

UPDATE:

Regarding how to listen for the messages on the native app, they are sent to the stdio (for the time being this is the only available communication channel between Chrome extensions and native apps). Take a look at this sample app featuring a native messaging host implemented in python.


You listen for messages registering a listener on port's onMessage event.

Use sendNativeMessage() only if you want a one-time communication (not a persistent port). In that case, do not use chrome.runtime.connectNative(...). Instead, do something like this:

var msg = {...};
chrome.runtime.sendNativeMessage("<your_host_id>", msg, function(response) {
    if (chrome.runtime.lastError) {
        console.log("ERROR: " + chrome.runtime.lastError.message);
    } else {
        console.log("Messaging host: ", response);
    }
});

The docs' section about Native Messaging is pretty detailed and a great source of information.

Junho Yeo
  • 93
  • 1
  • 7
gkalpak
  • 47,844
  • 8
  • 105
  • 118
  • thanks for reply. I want know how my native application(c or c++ exe) will receive the message send by chrome.runtime.sendNativeMessage() api.Which event should i handle in my native app to get this message. where should i get the message in my native application(c or c++ exe). – Nik Nov 27 '13 at 09:55
  • @Nik: I most probably misunderstood the question. Please, see my updated answer. – gkalpak Nov 27 '13 at 10:04
  • @Marc Thanks this working .. Last thing i want know when should i close the .exe. Should i send custom message when tab getting closed or is there any other thing ? – Nik Nov 27 '13 at 13:11
  • You can check if "getchar()" returns EOF. If Yes, leave the Loop an Exit. – Marc Nov 27 '13 at 13:38
5

I am posting c++ code which will communicate i.e receives and sends the messages to chrome extension. Hope this will help to other developer

int _tmain(int argc, _TCHAR* argv[])
{
    cout.setf( std::ios_base::unitbuf ); //instead of "<< eof" and "flushall"
    _setmode(_fileno(stdin),_O_BINARY);


    unsigned int c, i, t=0;
    string inp;  
    bool bCommunicationEnds = false;

    bool rtnVal = true;
    do {

        inp="";
        t=0;
        //Reading message length 
        cin.read(reinterpret_cast<char*>(&t) ,4);

        // Loop getchar to pull in the message until we reach the total
        //  length provided.
        for (i=0; i < t; i++) {
            c = getchar();
            if(c == EOF)
            {
                bCommunicationEnds = true;
                i = t;
            }
            else
            {
                inp += c;
            }
        }

         if(!bCommunicationEnds)
        {
            //Writing Message length
            cout.write(reinterpret_cast<char*>(&inp),4); 
            //Write original message.
            cout<<inp;
        }
    }while(!bCommunicationEnds);
    return 0;
}
Nik
  • 682
  • 1
  • 8
  • 27
  • 2
    Neither code nor comment above is correct - The first 4 bytes of a message are a 32-bit unsigned integer in native byte order. For example see code at http://src.chromium.org/svn/trunk/src/chrome/browser/extensions/api/messaging/native_message_process_host.cc – Spike0xff Apr 16 '14 at 16:00