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.
- Host: GitHub
- URL: https://github.com/sunquakes/jsonrpc4js
- Owner: sunquakes
- License: apache-2.0
- Created: 2023-11-13T14:10:44.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-04T11:12:46.000Z (over 2 years ago)
- Last Synced: 2024-03-04T16:38:19.220Z (over 2 years ago)
- Topics: consul, jsonrpc2, miscroservices, nacos, nodejs, nodejs-client, nodejs-server, typescript
- Language: TypeScript
- Homepage:
- Size: 114 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
English | [π¨π³ δΈζ](https://github.com/sunquakes/jsonrpc4js/blob/main/README_ZH.md)
# 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)