blue-rpc-protocol
v1.0.0
Published
A fast RPC protocol that uses WebSockets and MessagePack
Downloads
2
Readme
blue-rpc-protocol
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).
Here are its features:
- Byte streams (first class data type), useful for efficiently piping large payloads such as files.
- Object streams (first class data type), useful for representing pub-sub subscriptions and observables.
- Universal web-compatibility, because of WebSockets.
- Low bandwidth, because of MessagePack, WebSockets, and automatic compression.
- Simple RPC-style interface.
- Built-in support for cancelling RPC calls and streams.
- "Notifications", inspired by JSON-RPC, for pushing 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).
Installation
npm install blue-rpc-protocol
Requires Node.js v16.x.x or later, or any modern browser.
Usage
Server example
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
});
Client example
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"