# @meforce/rpc-node

> Type-safe communication between message ports from Node.js worker_threads module

Latest version **1.0.5** (published 2022-08-08) · 0 weekly downloads

## Install

```sh
npm install @meforce/rpc-node
pnpm add @meforce/rpc-node
yarn add @meforce/rpc-node
bun add @meforce/rpc-node
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2022-08-08 |
| First published | 2022-08-08 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 11.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | meforcetechnology |

## Links

- npm: https://www.npmjs.com/package/@meforce/rpc-node
- Repository: https://github.com/meforce/rpc
- Homepage: https://github.com/meforce/rpc#readme
- Issues: https://github.com/meforce/rpc/issues
- npm.io page: https://npm.io/package/@meforce/rpc-node

## Recent versions

- 1.0.5 (latest) — 2022-08-08

## README

# @meforce/rpc-node

Type-safe communication between message ports from Node.js worker_threads module.

[![NPM](https://img.shields.io/npm/v/@meforce/rpc-node.svg?style=flat-square)](https://www.npmjs.com/package/@meforce/rpc-node)
[![NPM](https://img.shields.io/npm/dm/@meforce/rpc-node?style=flat-square)](https://www.npmjs.com/package/@meforce/rpc-node)

## Installation

```bash
$ npm install --save @meforce/rpc-node @meforce/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`:

```ts
import { WorkerChannel } from '@meforce/rpc-node';

export interface PingPongService {
  ping(): string;
}

export const pingPongChannel = new WorkerChannel<PingPongService>('ping-pong');
```

- Code for your `worker_thread`:

```ts
import { RpcWorkerHandler } from '@meforce/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:

```ts
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

- [Main to Worker](examples/main-to-worker)

## Documentation

WIP

---
_Source: https://npm.io/package/@meforce/rpc-node · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
