BlueRPC is a fast RPC protocol that uses WebSockets and MessagePack. It solves real-world problems and is very easy to use.
In BlueRPC, streams are first-class data types. You can send streams just like any other value, and BlueRPC automatically handles things like cancellation and backpressure.
Since streams are just regular values, you can easily mix them with structured data, unlike HTTP (where you'd need to use headers or "multipart/form-data" for structured data) or gRPC (where there's no way to directly represent a byte stream) or JSON-RPC (which doesn't support streaming at all).
Here are its features:
- Byte streams, good for efficiently sending/receiving large payloads such as files.
- Object streams, good for representing pub-sub subscriptions or observables.
- Universal web-compatibility, because of WebSockets.
- Low bandwidth, because of WebSockets, MessagePack, and automatic compression.
- Simple RPC-style interface.
- Built-in support for cancelling RPC calls and streams.
- "Notifications", inspired by JSON-RPC, allow you to push lightweight events to the server.
- Encodes strings, binary (Buffer or Uint8Array), objects, arrays, floats, integers (BigInt), booleans, null, Error, and streams (stream.Readable or ReadableStream).
BlueRPC can be implemented in any general-purpose programming language. This repository contains a specification and a reference implementation for JavaScript (compatible with Node.js and browsers).
npm install blue-rpc-protocol
Requires Node.js v16.x.x or later, or any modern browser.
const http = require('http');
const BlueRPC = require('blue-rpc-protocol');
const methods = {
echo: (param) => {
return param;
}
};
await BlueRPC.listen({
methods,
server: http.createServer(),
logger: console.log
});
const BlueRPC = require('blue-rpc-protocol');
const client = BlueRPC.createClient('ws://localhost');
const result = await client.invoke('echo', 'foo');
console.log('result:', result); // => "result: foo"