Skip to main content

🦕 TypeScript module that allows for the implementation of MessagePack-RPC using MessagePack as the message schema.

Works with
It is unknown whether this package works with Bun
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
This package works with Deno
It is unknown whether this package works with Browsers
JSR Score
88%
Published
2 weeks ago (2.4.0)

This is a TypeScript module that allows for the implementation of MessagePack-RPC using MessagePack as the message schema.

Usage

Server

import { assert, is } from "@core/unknownutil";
import { Session } from "@lambdalisue/messagepack-rpc";

async function handleConnection(conn: Deno.Conn): Promise<void> {
  const session = new Session(conn.readable, conn.writable);

  // Define APIs
  session.dispatcher = {
    sum(x, y) {
      assert(x, is.Number);
      assert(y, is.Number);
      return x + y;
    },
  };

  // Start the session
  session.start();

  // Do whatever

  // Shutdown the session
  await session.shutdown();
}

const listener = Deno.listen({ hostname: "localhost", port: 8080 });
for await (const conn of listener) {
  handleConnection(conn).catch((err) => console.error(err));
}

Client

import { Client, Session } from "@lambdalisue/messagepack-rpc";

const conn = await Deno.connect({ hostname: "localhost", port: 8080 });
const session = new Session(conn.readable, conn.writable);
const client = new Client(session);

// Start the session
session.start();

// Do whatever
console.log(await client.call("sum", 1, 2)); // 3
console.log(await client.call("sum", 2, 3)); // 5

// Shutdown the session
await session.shutdown();

Although the original MessagePack-RPC specification does not mention bidirectional communication, this module supports it. Therefore, APIs defined on the client side can be called from the server side.

Built and signed on
GitHub Actions
View transparency log

Add Package

deno add @lambdalisue/messagepack-rpc

Import symbol

import * as mod from "@lambdalisue/messagepack-rpc";

Add Package

npx jsr add @lambdalisue/messagepack-rpc

Import symbol

import * as mod from "@lambdalisue/messagepack-rpc";

Add Package

yarn dlx jsr add @lambdalisue/messagepack-rpc

Import symbol

import * as mod from "@lambdalisue/messagepack-rpc";

Add Package

pnpm dlx jsr add @lambdalisue/messagepack-rpc

Import symbol

import * as mod from "@lambdalisue/messagepack-rpc";

Add Package

bunx jsr add @lambdalisue/messagepack-rpc

Import symbol

import * as mod from "@lambdalisue/messagepack-rpc";