Skip to main content

messagepack-rpc

jsr denoland deno doc Test codecov

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 "https://deno.land/x/unknownutil@v3.2.0/mod.ts";
import { Session } from "https://deno.land/x/messagepack_rpc@v2.1.1/mod.ts";

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 "https://deno.land/x/messagepack_rpc@v2.1.1/mod.ts";

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.

License

The code is released under the MIT license, which is included in the LICENSE file. By contributing to this repository, contributors agree to follow the license for any modifications made.