Javascript messaging library
npm i @thalesrc/hermes
or yarn add @thalesrc/hermes
The main concept is sending messages via MessageClient
's and answering them via MessageHost
's.
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();
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();
Implemented but not documented yet
Implemented but not documented yet