async-call-rpc JSON RPC Client & Server

6.4.2 · active · verified Tue Apr 21

async-call-rpc is a lightweight, zero-dependency JSON RPC server and client library written in TypeScript, designed for any ECMAScript 2018+ environment. The current stable version is 6.4.2, and the package sees active development with frequent patch and minor releases addressing bug fixes and introducing new features like the `encoder` option. Key differentiators include its full TypeScript support, custom encoder capabilities for complex data types, and experimental support for async generators. It works in both Node.js and browser environments and is published on both npm and JSR, offering flexibility in module consumption. It explicitly does not support ES5 environments or JSON RPC 1.0.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up a basic in-memory JSON RPC client and server using `async-call-rpc`, define a simple API, and make remote procedure calls.

import { AsyncCall } from 'async-call-rpc';

// Define a simple in-memory channel for demonstration
class InMemoryChannel {
    private clientSend?: (data: string) => void;
    private serverSend?: (data: string) => void;

    // Simulate client sending to server
    clientChannel = {
        send: (data: string) => {
            if (this.serverSend) {
                this.serverSend(data);
            }
        },
        on: (cb: (data: string) => void) => {
            this.clientSend = cb;
        }
    };

    // Simulate server sending to client
    serverChannel = {
        send: (data: string) => {
            if (this.clientSend) {
                this.clientSend(data);
            }
        },
        on: (cb: (data: string) => void) => {
            this.serverSend = cb;
        }
    };
}

const channel = new InMemoryChannel();

// Define the server API interface
interface MyServerAPI {
    add(a: number, b: number): Promise<number>;
    greet(name: string): Promise<string>;
}

// Server implementation
const serverImpl: MyServerAPI = {
    async add(a, b) {
        console.log(`Server received: add(${a}, ${b})`);
        return a + b;
    },
    async greet(name) {
        console.log(`Server received: greet('${name}')`);
        return `Hello, ${name}!`;
    }
};

// Create the RPC server instance
const server = new AsyncCall(serverImpl, { channel: channel.serverChannel });

// Create the RPC client instance (null for `thisSideImplementation` means it's purely a client)
const client = new AsyncCall<MyServerAPI>(null, { channel: channel.clientChannel });

// Use the client to call server methods
async function runClient() {
    console.log('Client calling add(5, 3)');
    const resultAdd = await client.add(5, 3);
    console.log('Result of add(5, 3):', resultAdd); // Expected: 8

    console.log('Client calling greet("World")');
    const resultGreet = await client.greet('World');
    console.log('Result of greet("World"):', resultGreet); // Expected: "Hello, World!"

    // Clean up server (optional, for explicit shutdown)
    server.stop();
}

runClient().catch(console.error);

view raw JSON →