An open API service indexing awesome lists of open source software.

https://github.com/sunquakes/jsonrpc4js

A javascript client and server over http or tcp implementation of JSON-RPC 2.0.
https://github.com/sunquakes/jsonrpc4js

consul jsonrpc2 miscroservices nacos nodejs nodejs-client nodejs-server typescript

Last synced: 13 days ago
JSON representation

A javascript client and server over http or tcp implementation of JSON-RPC 2.0.

Awesome Lists containing this project

README

          

English | [πŸ‡¨πŸ‡³ δΈ­ζ–‡](https://github.com/sunquakes/jsonrpc4js/blob/main/README_ZH.md)

# JSONRPC4JS



jsonrpc4js logo



Node
GitHub
jsonrpc4js

## Documentation

Visit [https://www.moonquakes.io/guide/javascript.html](https://www.moonquakes.io/guide/javascript.html).

## Quick Start

### Installing

```bash
pnpm add jsonrpc4js
```

### Server

```ts
const { NewServer } = require('jsonrpc4js')

class TcpRpc {
add(a: number, b: number): number {
return a + b
}
}
const port = 3200
const server = NewServer('tcp', port) // Also support http protocol.
server.register(new TcpRpc())
server.start()
```

### Client

```ts
const { NewClient } = require('jsonrpc4js')

const client = NewClient('TcpRpc', 'tcp', `localhost:3200`) // Also support http protocol.
let res = await client.call('add', 1, 2) // 3
```

## Service registration & discovery

### consul

```ts
const { Consul } = require('jsonrpc4js')

const discovery = new Consul('http://localhost:8500')

// Set discovery in server and client.
// const server = NewServer('tcp', port, discovery)
// const client = NewClient('TcpRpc', 'tcp', discovery)
```

### nacos

```ts
const { Nacos } = require('jsonrpc4js')

const discovery = new Nacos('http://localhost:8848')

// Set discovery in server and client.
// const server = NewServer('tcp', port, discovery)
// const client = NewClient('TcpRpc', 'tcp', discovery)
```

## Test

```bash
pnpm test
```

## License

[Apache-2.0 license](https://github.com/sunquakes/jsonrpc4js/blob/main/LICENSE)