Socket
Socket
Sign inDemoInstall

@mini-rpc/server

Package Overview
Dependencies
27
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @mini-rpc/server

simple but effective RPC framework. Both server and client.


Version published
Weekly downloads
14
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Mini-RPC

simple but effective RPC framework. Both server and client.

Usage

Get start

Install
npm i @mini-rpc/server
# or
pnpm add @mini-rpc/server
# or
yarn add @mini-rpc/server
Config
  • tsconfig.json in server
{
  "compilerOptions": {
    "target": "ES2018",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
  }
}
  • install reflect-metadata
npm i reflect-metadata
# or
pnpm add reflect-metadata
# or
yarn add reflect-metadata
  • import reflect-metadata on entry (e.g: index.ts)
Server
import { Service, Callable } from "@mini-rpc/server";

@Service()
class MyService {

	@Callable()
	add(...nums: number[]) {
		return nums.reduce((acc, cur) => acc + cur);
	}

}

const server = new RPCServer();
server.registerServices([MyService]);

server.listen(3000);

Client

/*
 * you need to install `@mini-rpc/client` on the client side
 */
import { RPCClient } from "@mini-rpc/client";

const client = new RPCClient("ws://localhost:3000");
client.connect();

client
  .call("MyService.add", [1,2,3])
  .then((result) => {
    console.log(result); // 6
  })
  .catch((err) => {
    console.error(err);
  });

Tips

  • the @Callable method can be asynchronous.

class x {
	//...

	@Callable()
	async add(a:number,b:number) {
		// asynchronous code
	}

	// or declare the return type Promise

	@Callable()
	promiseAdd(a:number,b:number):Promise<number> {
		return Promise<number>()
	}
}

Keywords

FAQs

Last updated on 25 Jul 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc