npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

busrt

v0.2.7

Published

JavaScript/Typescript client for BUS/RT

Downloads

106

Readme

JavaScript/TypeScript client for BUS/RT

JavaScript/TypeScript client for BUS/RT

Client example

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

// 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/