Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/farwayer/ws-rpc
Simple, small rfc-correct JSON-RPC 2.0 implementation via websockets
https://github.com/farwayer/ws-rpc
api jsonrpc msgpack rpc websocket
Last synced: 2 months ago
JSON representation
Simple, small rfc-correct JSON-RPC 2.0 implementation via websockets
- Host: GitHub
- URL: https://github.com/farwayer/ws-rpc
- Owner: farwayer
- Created: 2019-05-21T20:44:37.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-09-16T18:10:35.000Z (3 months ago)
- Last Synced: 2024-09-16T22:27:50.908Z (3 months ago)
- Topics: api, jsonrpc, msgpack, rpc, websocket
- Language: JavaScript
- Homepage:
- Size: 5.51 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ws-rpc
*Simple, small rfc-correct JSON-RPC 2.0 implementation with encoders support*
Supports binary data in requests with `MsgpackEncoder`
## Usage
### Server
```js
import {Server} from '@ws-rpc/server'
import {MsgpackEncoder} from '@ws-rpc/encoder-msgpack'// see ws lib options
let wss = new Server({
port: 8081,
encoders: [MsgpackEncoder], // json by default
ctx: {db},
})let bookUpsert = async (ctx, data) => {
let {book, created} = await ctx.db.bookUpsert(data)
if (created) {
ctx.emit('book.created', book) // emit event to current client
// ctx.emitAll('book.created', book) // or to all clients
}
return book
}wss.onrpc = async (ctx, method, ...args) => {
switch (method) {
case 'book.upsert':
return bookUpsert(ctx, ...args)
default:
ctx.throwMethodNotFound()
}
}wss.onevent = (name, ...args) => {
// process event from client
}
```### Client
```js
import {Client} from '@ws-rpc/client'
import {MsgpackEncoder} from '@ws-rpc/encoder-msgpack'let wsc = await new Client({
url: 'ws://localhost:8080',
encoders: [MsgpackEncoder], // json by default
}).connect()wsc.onevent = (event, ...args) => {
// process event from server
}let book = await wsc.rpc('book.upsert', {
name: 'Dune',
author: 'Franklin Patrick Herbert',
cover: new Uint8Array(),
})
```