Skip to content

Type-safe inter-process communication for Electron and Node.js

Notifications You must be signed in to change notification settings

gunawan092w/rpc-electron

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

84 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@wexond/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 @wexond/rpc-electron @wexond/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 '@wexond/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 '@wexond/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

Documentation

WIP

About

Type-safe inter-process communication for Electron and Node.js

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 100.0%