# @prsm/duplex

> [![NPM version](https://img.shields.io/npm/v/@prsm/duplex?color=a1b858&label=)](https://www.npmjs.com/package/@prsm/duplex)

Latest version **1.2.0** (published 2025-03-27) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @prsm/duplex
pnpm add @prsm/duplex
yarn add @prsm/duplex
bun add @prsm/duplex
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2025-03-27 |
| First published | 2023-01-08 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 219.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | nvms |
| Maintainers | nvmsy |

## Links

- npm: https://www.npmjs.com/package/@prsm/duplex
- npm.io page: https://npm.io/package/@prsm/duplex

## Recent versions

- 1.2.0 (latest) — 2025-03-27
- 1.1.12 — 2023-07-23
- 1.1.11 — 2023-07-23
- 1.1.10 — 2023-07-23
- 1.1.8 — 2023-05-24
- 1.1.7 — 2023-05-23
- 1.1.6 — 2023-05-23
- 1.1.5 — 2023-04-12
- 1.1.4 — 2023-04-12
- 1.1.3 — 2023-04-09
- 1.1.1 — 2023-01-08
- 1.1.0 — 2023-01-08
- 1.0.0 — 2023-01-08

## README

# duplex

[![NPM version](https://img.shields.io/npm/v/@prsm/duplex?color=a1b858&label=)](https://www.npmjs.com/package/@prsm/duplex)

An optionally-secure, full-duplex TCP command server and client built on top of `node:tls` and `node:net`. Provides reliable, Promise-based communication with automatic reconnection and command queueing.

## Features

- **Promise-based API** - All operations return Promises for easy async/await usage
- **Command queueing** - Commands are automatically queued when offline
- **Reliable connections** - Robust error handling and reconnection
- **Secure communication** - Optional TLS encryption
- **Bidirectional communication** - Full-duplex TCP communication
- **Lightweight** - No external dependencies

## Server

```typescript
import { CommandServer } from "@prsm/duplex";
import fs from "node:fs";

// Create a server instance
const server = new CommandServer({
  host: "localhost",
  port: 3351,
  secure: false, // For TLS, set to true and provide certificates
});

// Connect the server (returns a Promise)
await server.connect();

// Register command handlers
server.command(0, async (payload, connection) => {
  console.log("Received:", payload);
  return { status: "success", data: "Command processed" };
});

// For secure connections (TLS)
const secureServer = new CommandServer({
  host: "localhost",
  port: 3352,
  secure: true,
  key: fs.readFileSync("certs/server/server.key"),
  cert: fs.readFileSync("certs/server/server.crt"),
  ca: fs.readFileSync("certs/server/ca.crt"),
  requestCert: true,
});

await secureServer.connect();
```

## Client

```typescript
import { CommandClient } from "@prsm/duplex";
import fs from "node:fs";

// Create a client instance
const client = new CommandClient({
  host: "localhost",
  port: 3351,
  secure: false, // For TLS, set to true and provide certificates
});

// Connect to the server (returns a Promise)
await client.connect();

// Using Promise-based API
try {
  const response = await client.command(0, { action: "getData" }, 5000);
  console.log("Response:", response.result);
} catch (error) {
  console.error("Error:", error);
}

// Using callback API
client.command(0, { action: "getData" }, 5000, (result, error) => {
  if (error) {
    console.error("Error:", error);
    return;
  }
  console.log("Response:", result);
});

// For secure connections (TLS)
const secureClient = new CommandClient({
  host: "localhost",
  port: 3352,
  secure: true,
  key: fs.readFileSync("certs/client/client.key"),
  cert: fs.readFileSync("certs/client/client.crt"),
  ca: fs.readFileSync("certs/ca/ca.crt"),
});

await secureClient.connect();
```

## Error Handling

The library provides detailed error information with error codes:

```typescript
try {
  await client.command(0, payload, 1000);
} catch (error) {
  if (error.code === 'ETIMEOUT') {
    console.log('Command timed out');
  } else if (error.code === 'ENOTFOUND') {
    console.log('Command not found on server');
  } else {
    console.error('Other error:', error.message);
  }
}
```

## Graceful Shutdown

```typescript
// Close client connection
await client.close();

// Close server
await server.close();
```

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