Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dizys/orpc
The type-safe Node.js and browser RPC library built on top of socket.io. Empowered with SOA governance and load balancing.
https://github.com/dizys/orpc
nodejs rpc socket-io typesafe typescript
Last synced: about 2 months ago
JSON representation
The type-safe Node.js and browser RPC library built on top of socket.io. Empowered with SOA governance and load balancing.
- Host: GitHub
- URL: https://github.com/dizys/orpc
- Owner: dizys
- License: mit
- Created: 2018-11-25T13:05:31.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T04:11:15.000Z (almost 2 years ago)
- Last Synced: 2024-10-02T22:37:11.443Z (3 months ago)
- Topics: nodejs, rpc, socket-io, typesafe, typescript
- Language: TypeScript
- Homepage:
- Size: 271 KB
- Stars: 10
- Watchers: 0
- Forks: 2
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![NPM Package](https://badge.fury.io/js/orpc.svg)](https://www.npmjs.com/package/orpc) [![Build Status](https://travis-ci.org/dizys/orpc.svg?branch=master)](https://travis-ci.org/dizys/orpc)
# orpc
The type-safe Node.js and browser RPC library built on top of socket.io. Empowered with SOA governance and load balancing.
## Installation
```sh
yarn add orpc
```## Quick Start
`shared.ts`
```ts
export interface HelloServicePrototype extends ServicePrototype {
say(name: string): string;
}export interface MyRPCSchema extends RPCSchema {
hello: HelloServicePrototype;
}
````server.ts`
```ts
class HelloService implements HelloServicePrototype {
say(name: string): string {
return `Hello, ${name}!`;
}
}let myRPC: MyRPCSchema = {
hello: new HelloService(),
};const server = new RPCServer(myRPC);
server.start(8013);
````client.ts`
```ts
let rpc = createClient('http://localhost:8013/');let result = await rpc.hello.say('dizy'); // Type-safe
console.log(result); // 'Hello, dizy!'
```## Core Concepts
> `ServicePrototype` and `RPCSchema` should be shared between client and server. `Service` should be implemented only on server.
### Service Prototype
A service prototype is an interface that defines the signature of the methods provided in the service. This is shared between both server and client side, so it will help type both our server implementation and client usage.
```ts
interface FileServicePrototype extends ServicePrototype {
list(dir: string): Promise;
upload(file: Buffer, saveToDir: string): Promise;
download(path: string): Promise;
delete(path: string): Promise;
rename(oldPath: string, newPath: string): Promise;
}
```### RPC Schema
In order to run multiple services on one single server, we build RPC schema to assemble these services into one bigger type. This is also shared between client and server.
```ts
interface MyRPCSchema extends RPCSchema {
file: FileServicePrototype;
system: SystemServicePrototype;
blog: BlogServicePrototype;
}
```### Service
Service is implemented in a form of `Class`. And it could be considered as a collection of methods ready to be remotely called. It is expected to implement the corresponding `ServerPrototype` on server.
```ts
class FileService implements FileServicePrototype{
async list(dir: string): Promise{
let files = await //...implementationreturn files;
}//...
}
```### Gateway
`Gateway` is a reverse proxy that takes clients' calls and sends them to one of the known servers, then gets results and feed them back to clients. To clients, a `Gateway` behaves exactly like a `RPCServer`, except its performance compete with several servers combined. `Gateway` is especially useful for high-cpu-usage services. Also, `Gateway` supports SOA governance and load balancing, so it can make sure services are highly-available and calls are well-balanced in terms of distribution to servers.
```ts
let config = {
servers: [
{url: 'http://localhost:8021/', weight: 10},
{url: 'http://localhost:8022/', weight: 3},
{url: 'http://localhost:8023/', weight: 4},
], // Servers' info
loadBalancer: smoothWRRLoadBalancer, // built-in Smooth-Weighted-Round-Robin LoadBalancer
log: {debug: true}, // Turn on debug mode
};let gateway = new Gateway(config);
gateway.start(8014);
```## APIs
### RPCServer
- `constructor (rpcSchema?: SchemaImplement)`
- methods:- `start(port?: number, hostname?: string): void`
`port` defaults to `80`, `hostname` defaults to `'localhost'`
- `stop(): void`
- `register(name: string, service: object): void`
- `register(name: string, service: ServiceConstructor, initializeForEachClient?: boolean): void`
Dynamically register new service. `initializeForEachClient` defaults to `false`
- `unregister(name: string): boolean`
Dynamically cancel registered service.
### createClient()
- parameters:
- `url`?: string
Defaults to `'http://localhost/'`
- returns:
- `RPCClient`
- usages:
```ts
let rpc = createClient(8080);let result = await rpc.myService.methodA(...args);
```### RPCClient
`RPCClient` should always be created by `createClient`. That way, service and schema type information will be attached to it.
- `constructor (url?: string)`
- methods:- `open(): void`
Open connection. Automatically called when created.
- `close(): void`
- `call(service: string, data: CallData): Promise`
- `call(service: string, method: string, params: any[], options: CallOptions): Promise`
Call remote service method.
### Gateway
#### GatewayConfig
```ts
let config: GatewayConfig = {
servers: [
{url: 'http://localhost:8015', weight: 10},
{url: 'http://localhost:8016', weight: 4},
{url: 'http://localhost:8017', weight: 6},
], // Servers' info
loadBalancer: smoothWRRLoadBalancer, // Optional: defaults to `evenLoadBalancer`.
downgrade: true, // Optional: defaults to `true`.
downgradeAtAnyError: false, // Whether any error thrown by server will cause its downgrade. Optional: defaults to `false`.
downgradeTolerantTime: 3, // How many tolerant times to fail before the server is downgraded to `0` weight. Optional: defaults to `3`.
downgradeDeadSleepTime: 5, // A many times can a `0` weight server be offered a chance to revive. Optional: defaults to `5`.
log: {
enable: true,
debug: true,
}, // Optional: Defaults to `{enable: true, debug: false}`
};
```#### Gateway
- `constructor(config: GatewayConfig)`
- methods:- `start(port?: number, hostname?: string): void`
Start gateway server. `port` defaults to `80`, `hostname` defaults to `'localhost'`
- `stop(): void`
- `reloadConfig(config: GatewayConfig): void`
- usage:
see [Core Concepts](#Core-Concepts)
## License
MIT, see the [LICENSE](/LICENSE) file for details.