@ironiumstudios/rpc-node
v3.0.1
Published
Type-safe communication between message ports from Node.js worker_threads module
Downloads
180
Readme
@ironiumstudios/rpc-node
Type-safe communication between message ports from Node.js worker_threads module.
NOTE:
this project is licensed under the company wexond, i am only forking this project to modernize it and i wish for no trouble from the wexond devs or redbrick
Installation
$ npm install --save @ironiumstudios/rpc-node @ironiumstudios/rpc-core
Quick start
Here's an example of communication from the main thread to a worker_thread
:
- Create a file that is imported in both the
worker_thread
and the main thread, for exampleping-pong.ts
:
import { WorkerChannel } from '@ironiumstudios/rpc-node';
export interface PingPongService {
ping(): string;
}
export const pingPongChannel = new WorkerChannel<PingPongService>('ping-pong');
- Code for your
worker_thread
:
import { RpcWorkerHandler } from '@ironiumstudios/rpc-node';
import { PingPongService, pingPongChannel } from './ping-pong';
class PingPongHandler implements RpcWorkerHandler<PingPongService> {
// Equivalent of |parentPort.on('message')|
ping(): string {
return 'pong';
}
}
// |parentPort| is the default value for the |messagePort| parameter in |getReceiver|.
pingPongChannel.getReceiver().handler = new PingPongHandler();
- Code for the main thread:
import { resolve } from 'path';
import { Worker } from 'worker_threads';
import { pingPongChannel } from './ping-pong';
const worker = new Worker('path/to/worker.js');
const pingPongService = pingPongChannel.getInvoker(worker);
(async () => {
console.log(await pingPongService.ping()); // Prints `pong`.
})();
More examples
Documentation
WIP