Coder Social home page Coder Social logo

cosium / web-native-messaging-host Goto Github PK

View Code? Open in Web Editor NEW
7.0 5.0 0.0 130 KB

A java library allowing to turn any JVM application into a Web Native Messaging Host

License: MIT License

Java 81.73% TypeScript 13.20% HTML 0.30% JavaScript 4.51% Shell 0.26%
native-messages native-messaging native-messaging-host

web-native-messaging-host's Introduction

Build Status Maven Central

Web Native Messaging Host

A java library allowing to turn any JVM application into a Web Native Messaging Host .

Gotchas

To date, web native messaging protocol only supports stdio communication:

  • messages are sent to the native application's stdin
  • messages are sent to the browser extension via native application's stdout

Native application (e.g. your JVM application) instances started by the browser must never use System.out for anything else than native messaging. Very often, you will have to make sure your logger never writes to System.out.

As soon as a Channel is open, the current library will swap System.in and System.out for instances throwing exception on any interaction attempt coming from components foreign to the current library. On Channel shutdown, the library will swap back the standard System.in and System.out instances. The current library cannot cover the full application lifecycle, therefore it is only a best effort.

Using System.out (e.g. for logging) before opening a Channel will probably trigger a fatal error on the browser side leading to the end of the communication channel and the native application shutdown.

Quick start

  1. Add the following dependency:
    <dependency>
      <groupId>com.cosium.web_native_messaging_host</groupId>
      <artifactId>web-native-messaging-host</artifactId>
      <version>${web-native-messaging-host.version}</version>
    </dependency>
  2. Open the communication channel:
    public class App implements MessageHandler {
    
      private final ObjectMapper objectMapper = new ObjectMapper();
    
      public static void main(String[] args) {
        Host host = Host.builder(new App()).build();
        // Open the communication channel
        try (CloseableChannel channel = host.openChannel()) {
          // Block until channel shutdown
          channel.waitForShutdown();
        }
      }
    
      @Override
      public void onMessage(Channel channel, ContainerNode<?> rawMessage) {
        Message message = objectMapper.convertValue(rawMessage, Message.class);
        // Print the received message
        System.out.printf("Received message '%s'%n", message.body);
        // Send a message to the other end
        channel.sendMessage(objectMapper.valueToTree(new Message("I got your message")));
      }
    
      private record Message(String body) {
      } 
    }

Build

e2e-tests can only run on Unix systems.

./mvnw clean verify

Release

./release.sh

web-native-messaging-host's People

Contributors

dependabot[bot] avatar reda-alaoui avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.