eis
electron-ipc-socket
npm i electron-ipc-socket
eis

electron-ipc-socket

Two-way communication on top of Electron IPC system

by Tim Voronov

3.0.0 (see all)License:MITTypeScript:Not Found
npm i electron-ipc-socket
Readme

electron-ipc-socket

Event based communication

Response-request abstraction on top of Electron IPC system.

npm version Actions Status

    npm install --save electron-ipc-socket

Motivation

Events are good, but sometimes you want something more than just 'emit and forget'. Current package provides an abstraction on top of Electron IPC system that allows you to make 'request-response' communication.

Usage

Events

// main-process.js

import { ipcMain, BrowserWindow } from 'electron';
import { Socket, Transport, Event, InboundRequest } from 'electron-ipc-socket';
import fs from 'fs';

const win = new BrowserWindow();

const socket = new Socket(new Transport(ipcMain, win));

socket.open('main-win');

socket.onEvent('ready', (evt: Event) => {
    console.log('Renderer process is ready');
});
// renderer-process.js

import { ipcRenderer } from 'electron';
import { Socket } from 'electron-ipc-socket';

const socket = new Socket(ipcRenderer);

socket.open('main-win');

socket.send('ready');

Request-response

// main-process.js

import { ipcMain, BrowserWindow } from 'electron';
import { Socket, Transport, Event, InboundRequest } from 'electron-ipc-socket';
import fs from 'fs';

const win = new BrowserWindow();

const socket = new Socket(new Transport(ipcMain, win));

socket.open('main-win');

socket.onRequest('ping', (req: InboundRequest) => {
    return 'pong';
});
// renderer-process.js

import { ipcRenderer } from 'electron';
import { Socket } from 'electron-ipc-socket';

const socket = new Socket(ipcRenderer);

socket.open('main-win');

socket
    .request('ping')
    .then(content => console.log(content))
    .catch(err => console.error(err));
Async request handler
// main-process.js

import { ipcMain, BrowserWindow } from 'electron';
import { Socket, Transport, Event, InboundRequest } from 'electron-ipc-socket';
import fs from 'fs';
import util from 'util';

const read = util.promisify(fs.read);
const win = new BrowserWindow();

const socket = new Socket(new Transport(ipcMain, win));

socket.open('main-win');

socket.onRequest('file', async (req: InboundRequest) => {
    return read(req.data);
});
// renderer-process.js

import { ipcRenderer } from 'electron';
import { Socket } from 'electron-ipc-socket';

const socket = new Socket(ipcRenderer);

socket.open('main-win');

socket
    .request('file', 'package.json')
    .then(content => console.log(content))
    .catch(err => console.error(err));

Downloads/wk

30

GitHub Stars

14

LAST COMMIT

2yrs ago

MAINTAINERS

1

CONTRIBUTORS

3

OPEN ISSUES

0

OPEN PRs

4
VersionTagPublished
3.0.0
latest
2yrs ago
No alternatives found
No tutorials found
Add a tutorial

Rate & Review

100
No reviews found
Be the first to rate