0.10.0 • Published 11 months ago

stream-json-rpc v0.10.0

Weekly downloads
3
License
MIT
Repository
github
Last release
11 months ago

stream-json-rpc

Easy bi-directionnal RPC for node/electron/browser, using the transport that you need.

Usage

As the transport layer must implement the stream.Duplex interface, some helpers are already available for electron ipcMain/ipcRenderer and node-ipc.

Example

Process 1
import rpcchannel from 'stream-json-rpc';
import { getServer, NodeIpcServerDuplex, firstConnectionHandler } from 'stream-node-ipc';

// This process acts as a node-ipc server
// But it actually being a "server" is not really relevant,
// as any process can call any other process if they can directly communicate.

const ipcServer = getServer('my-namespace'); // Instance of NodeIPC.Server

// Whenever a new client is connected, the callback will be called with
// the Duplex stream already created
firstConnectionHandler(ipcServer, (duplex: NodeIpcServerDuplex) => {
  const channel = rpcchannel(duplex, {
    // defaultRequestTimeout?: number, // defaults: 2000 (ms)
    // forwardErrors?: boolean, // defaults: false (if true, errors during requests are fully forwarded to requester)
  });
  // Get a named connection
  // On the other side, the same call must be done, with the same id, to finish the handshake
  const peer = channel.peer('connection-id');
  
  // Register handlers
  peer.setRequestHandler('inc', ({ value }: any) => {
    return value + 1;
  });
});
Process 2
import rpcchannel from 'stream-json-rpc';
import { getClient, NodeIpcClientDuplex } from 'stream-node-ipc';

// This process acts as node-ipc client

// Instance of NodeIPC.Client, connected to the server
const ipcClient = getClient('my-namespace');
const channel = rpcchannel(new NodeIpcClientDuplex(ipcClient));
// Get a named connection
// On the other side, the same call must be done, with the same id, to finish the handshake
const peer = channel.peer('connection-id');

// Call remote method on process 1
peer
  .request('inc', {
    value: 1,
  })
  .then((result) => {
    console.log('Result should be 2:', result);
  });

// You could also create any handler here with `addRequestHandler`
// or `addNotificationHandler`, and process1 would be able to call them.
0.10.0

11 months ago

0.9.0

12 months ago

0.9.1

11 months ago

0.7.9

5 years ago

0.7.8

5 years ago

0.7.7

5 years ago

0.7.6

5 years ago

0.7.5

5 years ago

0.7.4

5 years ago

0.7.3

5 years ago

0.7.1

5 years ago

0.7.0

5 years ago

0.6.1

5 years ago

0.6.0

5 years ago

0.5.8

5 years ago

0.5.7

5 years ago

0.5.6

5 years ago

0.5.5

5 years ago

0.5.4

5 years ago

0.5.3

5 years ago

0.5.2

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.5

5 years ago

0.4.4

5 years ago

0.4.3

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago