# coproq

> message-passing coprocesses wrapped in OO syntax

Latest version **0.1.2** (published 2022-06-04) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install coproq
pnpm add coproq
yarn add coproq
bun add coproq
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2022-06-04 |
| First published | 2022-06-04 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 11.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Andras |
| Maintainers | andrasq |
| Keywords | rpc, process, concurrent, parallel, worker |

## Links

- npm: https://www.npmjs.com/package/coproq
- Repository: https://github.com/andras/node-coprocess
- Homepage: https://github.com/andras/node-coprocess#readme
- Issues: https://github.com/andras/node-coprocess/issues
- npm.io page: https://npm.io/package/coproq

## Dependencies (1)

- [qibl](https://npm.io/package/qibl.md) 1.20.1

## Alternatives

- [cron](https://npm.io/package/cron.md) — 4.9M weekly downloads
- [@vercel/queue](https://npm.io/package/@vercel/queue.md) — 731.6K weekly downloads
- [create-sonicjs](https://npm.io/package/create-sonicjs.md) — 1.6K weekly downloads
- [@exellix/jobs-api](https://npm.io/package/@exellix/jobs-api.md) — 941 weekly downloads
- [@forwardimpact/libskill](https://npm.io/package/@forwardimpact/libskill.md) — 575 weekly downloads

## Recent versions

- 0.1.2 (latest) — 2022-06-04
- 0.0.0 — 2022-06-04

## README

Coprocess
=========
[![Build Status](https://api.travis-ci.com/andrasq/node-coprocess.svg?branch=master)](https://travis-ci.com/github/andrasq/node-coprocess?branch=master)
[![Coverage Status](https://codecov.io/github/andrasq/node-coprocess/coverage.svg?branch=master)](https://codecov.io/github/andrasq/node-coprocess?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/andrasq/node-coprocess/badge.svg?branch=master)](https://coveralls.io/github/andrasq/node-coprocess?branch=master)


Inter-process RPC wrapped in OO syntax.

    var coprocess = require('coprocess');

    // create a worker process
    var coproc = coprocess.fork(function() {
        // worker must load its depencies as if in a separate file
        var Coprocess = require("coprocess").Coprocess;
        new Coprocess().listen({
            echo: function(x, cb) { cb(null, x) },
        });
    });

    // call the worker, wait for its response
    coproc.call('echo', 1234, function(err, reponse) {
        // => err: null, response: 1234
    });

Api
----------------

### coprocess.fork( scriptName | functionBody [,cb] )

Create a new parent / worker object pair.  Returns the parent object used to talk to the
worker.  The worker process launched running the named `scriptName` or the function
`functionBody`.  The callback `cb` is invoked as soon as the process is running, not
after it's initialized.  The worker process is killed when the parent process exits.

Script file names are relative to the current working directory, not the source file.

Functions are converted to source and saved to temporary files in the current directory
`./node-coprocess-XXXXXX.js` and get removed automatically when the parent process calls its
`process.on('exit')` listeners.  Worker functions are a convenience, they share no context
with the parent function.  They must load and initialize all their dependencies as if they
were in their own file.

`fork` throws if unable to create the worker.  The optional callback is invoked once the
worker process is running.

    var coproces = require('coprocess');
    var coproc = coprocess.fork("./scripts/test.js");

### coproc.close( [cb(err)] )

Terminate the worker process, either by disconnecting from it or killing it.

### coproc.call( method [, arg [...]], callback(err, result) )

Invoke the named `method` with the given argument(s), and wait for the results.
The callback will be invoked with the returned result once the call completes.

### coproc.emit( event [, value [...]] )

Emit a named event and optional value(s) to the registered event listener.  Events are sent
back-to-back in order, no response is returned or expected.  Note that events and calls are
sent and dispatched in order, so receipt of a batch of events can be confirmed with a single
call after the batch.

### coproc.listen( event, handler(value [, ...]) )

Listen for named events emitted by the remote.  The handler function is called
with the received arguments whenever the named `event` is received.

    coproc.listen('stats', function(stats) {
        // received another batch of stats, no response expected
    })

### coproc.listen( methods )

Register handlers for the calls in the `methods` name-value hash.  Each handler function is
provided a callback as its last argument, and must call it to signal completion and return
an error or a result back to the caller.  If completion signaling is not needed, it is
faster to `emit` events.

    coproc.listen({
        ping: function(cb) { cb(null) },
        echo: function(value, cb) { cb(null, value) },
    })


Message Formats
----------------

Call

    { id, name, argc, argv }

Response

    { id, result }

Event

    { name, argc, argv }

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