@meforce/rpc-electron
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

@meforce/rpc-electron

Type-safe communication between Electron processes. No more remembering IPC channel names, parameters order and their types.

NPM NPM

Installation

$ npm install --save @meforce/rpc-electron @meforce/rpc-core

Quick start

Here's an example of communication from the renderer process to the main process:

  • Create a file that is imported in both main and renderer processes, for example ping-pong.ts:
import { RendererToMainChannel } from '@meforce/rpc-electron';

export interface PingPongService {
  ping(): string;
}

export const pingPongChannel = new RendererToMainChannel<PingPongService>(
  'ping-pong',
);
  • Code for the renderer process:
import { ipcRenderer } from 'electron';
import { pingPongChannel } from './ping-pong';

const pingPongService = pingPongChannel.getInvoker();

(async () => {
  // Equivalent of |ipcRenderer.invoke|
  console.log(await pingPongService.ping()); // Prints `pong`.
})();
  • Code for the main process:
import { RpcMainHandler } from '@meforce/rpc-electron';
import { PingPongService, pingPongChannel } from './ping-pong';

// Equivalent of |ipcMain.handle|
class PingPongHandler implements RpcMainHandler<PingPongService> {
  ping(): string {
    return 'pong';
  }
}

pingPongChannel.getReceiver().handler = new PingPongHandler();

More examples

Package Sidebar

Install

npm i @meforce/rpc-electron

Weekly Downloads

2

Version

1.0.5

License

none

Unpacked Size

23.2 kB

Total Files

32

Last publish

Collaborators

  • meforcetechnology