Communicating between extensions and other applications

Hey everyone, I wanted to see if anyone has any experience getting a plasmo extension to communicate with another process running on the same machine.

I know I could build a simple web server and communicate with the external application via HTTP, but considering they're both hosted on the same machine, I figured there might be a faster way to communicate between them.

ChatGPT said I could use the following code sample, but I'm currently yet to try it, and I don't know if there's some API I could use instead.

use std::io::{self, BufRead, Write};

fn main() {
    let stdin = io::stdin();
    let mut reader = stdin.lock();
    let stdout = io::stdout();
    let mut writer = stdout.lock();

    loop {
        let mut message = String::new();
        reader.read_line(&mut message).expect("Failed to read line");
        let response = process_message(&message);
        writer.write_all(response.as_bytes()).expect("Failed to write response");
        writer.flush().expect("Failed to flush");
    }
}

fn process_message(message: &str) -> String {
    // Implement your logic to process messages here
    // You can use serde for JSON serialization/deserialization
    // Example: Convert message to JSON and return a response
    let response = format!("{{\"result\": \"Processed: {}\"}}", message.trim());
    response
}


// For Chrome
const port = chrome.runtime.connectNative('com.example.native');

// For Firefox
// const port = browser.runtime.connectNative('com.example.native');

port.postMessage({ message: 'Hello from extension!' });

port.onMessage.addListener((response) => {
    console.log('Received response:', response);
});

port.onDisconnect.addListener(() => {
    console.log('Port disconnected');
});
Was this page helpful?