# busrt

> JavaScript/Typescript client for BUS/RT

Latest version **0.2.7** (published 2023-11-15) · Apache-2.0 license · 0 weekly downloads

## Install

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

## Health

**Score 40/100 (D)** — status: abandoned.

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

Warnings: low downloads; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.2.7 |
| Published | 2023-11-15 |
| First published | 2022-09-30 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 102.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 140 |
| Author | Bohemia Automation / Altertech |
| Maintainers | disserman, slowns |
| Keywords | busrt, ipc, pubsub, broker |

## Links

- npm: https://www.npmjs.com/package/busrt
- Repository: https://github.com/alttch/busrt
- Homepage: https://github.com/alttch/busrt#readme
- Issues: https://github.com/alttch/busrt/issues
- npm.io page: https://npm.io/package/busrt

## Dependencies (3)

- [async-mutex](https://npm.io/package/async-mutex.md) ^0.3.2
- [sleep-promise](https://npm.io/package/sleep-promise.md) ^9.1.0
- [promise-socket](https://npm.io/package/promise-socket.md) ^7.0.0

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 0.2.7 (latest) — 2023-11-15
- 0.2.6 — 2023-11-15
- 0.2.5 — 2023-11-15
- 0.2.4 — 2023-11-15
- 0.2.3 — 2023-11-15
- 0.2.2 — 2023-11-12
- 0.2.1 — 2023-11-12
- 0.2.0 — 2023-11-12
- 0.1.2 — 2023-01-01
- 0.1.1 — 2022-09-30
- 0.1.0 — 2022-09-30

## README

# JavaScript/TypeScript client for BUS/RT

JavaScript/TypeScript client for [BUS/RT](https://github.com/alttch/busrt)

## Client example

```typescript
const { Bus, Frame } = require("busrt");

const onDisconnect = async() => {
  console.log("BUS/RT disconnected");
}

const onFrame = async (frame: Frame) => {
  console.log(frame, frame.getPayload().toString());
}

const main = async () => {
  const bus = new Bus("js");
  bus.onDisconnect = onDisconnect;
  bus.onFrame = onFrame;
  bus.timeout = 5; // seconds, default is 5
  bus.pingInterval = 1; // seconds, default is 1, must be lower than timeout
  await bus.connect("/tmp/busrt.sock"); // local IPC, faster
  // await bus.connect(("localhost", 9924)); // TCP, slower
  console.log(bus.isConnected());

  // subscribe to topics
  const req1 = await bus.subscribe(["some/topic1", "topic2"]);
  await req1.waitCompleted();

  // unsubscribe from topics
  const req2 = await bus.unsubscribe(["some/topic1", "topic2"]);
  await req2.waitCompleted();

  // send one-to-one message
  const req3 = await bus.send("target", Buffer.from("hello"));
  await req3.waitCompleted();

  // disconnect
  await bus.disconnect();
}

main()
```

## RPC example

```javascript
// payloads in this example are packed/unpacked with msgpack
const msgpack = require("msgpackr");
const sleep = require("sleep-promise");
const { Rpc, Bus, RpcEvent, BusError, BusErrorCode, Frame } = require("busrt");

// RPC notification handler
const onNotification = async (ev: RpcEvent) => {
  console.log(ev.frame.primary_sender, ev.getPayload());
}

// RPC call handler
const onCall = async (ev: RpcEvent) => {
  const method = ev.method.toString();
  const payload = ev.getPayload();
  const params = payload.length > 0 ? msgpack.unpack(payload) : null;
  if (method == "test") {
    return msgpack.pack({ ok: true });
  } else if (method == "err") {
    throw new BusError(-777, "test error");
  } else {
    throw new BusError(BusErrorCode.MethodNotFound, Buffer.from(`no such method: ${method}`));
  }
}

// frame handler (broadcasts and topics)
const onFrame = async (frame: Frame) => {
  console.log(frame.primary_sender, msgpack.unpack(frame.getPayload()));
}

const main = async () => {
  // create and connect a new client
  let bus = new Bus("js");
  await bus.connect("/tmp/busrt.sock");
  // init RPC layer
  let rpc = new Rpc(bus);
  // init RPC handlers if incoming event handling is required
  rpc.onNotification = onNotification;
  rpc.onCall = onCall;
  rpc.onFrame = onFrame;
  // send test notification
  await rpc.notify("target", Buffer.from("hello"));
  const payload = { test: 123 };
  // call rpc test method, no response required
  await rpc.call0("target", "test", msgpack.pack(payload));
  // call rpc test method and wait for the response
  try {
    const req = await rpc.call("target", "test", msgpack.pack(payload));
    const result = await req.waitCompleted();
    console.log(msgpack.unpack(result.getPayload().toString()));
  } catch (err) {
    console.log(err.code, err.message.toString());
  }
  // handle local RPC methods while connected
  while (rpc.is_connected()) {
    await sleep(1000);
  }
  await bus.disconnect();
}

main();
```

## API reference

<https://pub.bma.ai/dev/docs/busrt/js/>

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