https://github.com/getstation/stream-json-rpc
Easy bi-directionnal RPC for node/electron/browser, using the transport that you need.
https://github.com/getstation/stream-json-rpc
Last synced: about 1 year ago
JSON representation
Easy bi-directionnal RPC for node/electron/browser, using the transport that you need.
- Host: GitHub
- URL: https://github.com/getstation/stream-json-rpc
- Owner: getstation
- License: mit
- Created: 2018-11-12T13:51:27.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-10-05T18:06:31.000Z (over 2 years ago)
- Last Synced: 2025-05-02T16:55:25.136Z (about 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 755 KB
- Stars: 2
- Watchers: 3
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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](packages/stream-electron-ipc) and
[node-ipc](packages/stream-node-ipc).
### Example
##### Process 1
```typescript
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
```typescript
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.
```