@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 example ping-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> {
ping(): string {
return 'pong';
}
}
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());
})();
More examples
Documentation
WIP