@thalesrc/hermes
TypeScript icon, indicating that this package has built-in type declarations

6.1.1 • Public • Published

hermes

publish build npm npm codecov TypeScript

Javascript messaging library

Installation

npm i @thalesrc/hermes or yarn add @thalesrc/hermes

Usage

The main concept is sending messages via MessageClient's and answering them via MessageHost's.


Iframe

Send and recieve messages accross iframes and parent windows

// inside iframe
import { IframeMessageClient, Request } from '@thalesrc/hermes/iframe';

class MessageSenderService extends IframeMessageClient {
  @Request('hello')
  public sayHello(name: string): Observable<string> {
    return null;
  }
}

const service = new MessageSenderService();

service.sayHello('John').subscribe(message => {
  console.log(message);
});

// 'Hi John, here are some data for you'
// 'Thales Rocks!!'
// inside parent window
import { IframeMessageHost, Listen, UpcomingMessage } from '@thalesrc/hermes/iframe';
import { of } from 'rxjs';

class MessageListenerService extends IframeMessageHost {
  @Listen('hello')
  public listenHello({data, sender}: UpcomingMessage): Observable<string> {
    return of(
      'Hi ' + data + ', here is some data for you',
      'Thales Rocks!!'
    );
  }
}

const listener = new MessageListener();

Chrome Extensions

Send and recieve messages accross tabs, background-scripts, content-scripts etc.

// content-script or a page etc.
import { ChromeMessageClient, Request } from '@thalesrc/hermes/chrome';

class MessageSenderService extends ChromeMessageClient {
  @Request('hello')
  public sayHello(name: string): Observable<string> {
    return null;
  }
}

const service = new MessageSenderService();

service.sayHello('John').subscribe(message => {
  console.log(message);
});

// 'Hi John, here are some data for you'
// 'Thales Rocks!!'
// background-script etc.
import { ChromeMessageHost, Listen } from '@thalesrc/hermes/chrome';
import { of } from 'rxjs';

class MessageListenerService extends ChromeMessageHost {
  @Listen('hello')
  public listenHello(name: string): Observable<string> {
    return of(
      'Hi ' + name + ', here is some data for you',
      'Thales Rocks!!'
    );
  }
}

const listener = new MessageListener();

Workers

Implemented but not documented yet


Broadcast

Implemented but not documented yet

Package Sidebar

Install

npm i @thalesrc/hermes

Weekly Downloads

4

Version

6.1.1

License

MIT

Unpacked Size

39.3 kB

Total Files

75

Last publish

Collaborators

  • alisahinozcelik