Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rumkin/urpc
μRPC is a transport agnostic JSONRPC 1.0 implementation
https://github.com/rumkin/urpc
javascript js jsonrpc nodejs rpc
Last synced: 4 days ago
JSON representation
μRPC is a transport agnostic JSONRPC 1.0 implementation
- Host: GitHub
- URL: https://github.com/rumkin/urpc
- Owner: rumkin
- License: mit
- Created: 2016-07-03T17:13:50.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T20:23:01.000Z (about 2 years ago)
- Last Synced: 2025-01-03T01:16:27.076Z (about 1 month ago)
- Topics: javascript, js, jsonrpc, nodejs, rpc
- Language: JavaScript
- Homepage: https://npmjs.com/package/urpc
- Size: 135 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 5
-
Metadata Files:
- Readme: readme.md
- Changelog: changelog.md
- License: license
Awesome Lists containing this project
README
# μRPC v5
Transport agnostic bidirectional JsonRPC 1.0 implementation for browser and
node.js.### Install
Install via npm:
```
npm i urpc
```Import in browser:
```html
```
And then use in some script:
```htmlconst rpc = new Urpc.Connection()
```
Single-file ESM and CommonJS versions are also distributed with a
build.### Usage
Hello world example. This is a simple endpoint which provide single
method `greet(name)`, which returns a greeting message as a result.```javascript
import {Connection} from 'urpc';async function handler({req, res}) {
if (req.method === 'greet') {
res.result = `Hello, ${req.params[0]}!`;
}
};// Create listening (server) connection with custom server
wsServer.on('connection', (conn) => {
const rpc = new Connection(handler);// Message exchange
conn.on('message', (message) => {
rpc.write(message);
});rpc.on('message', (message) => {
conn.write(message);
});// Connection state syncing
conn.on('close', () => {
rpc.close();
});rpc.on('close', () => {
conn.close();
});// Call remote end with the same handler
rpc.call('greet', ['World'])
.then((result) => {
result; // "Hello, World!"
});// Send notification
rpc.publish('event', []);
});
```## Codecs
By default all messages passed in or out of a connection is encoded and decoded
via built-in default codec (which is JSON). But you may use no codec:```js
import {Connection} from 'urpc';const rpc = new Connection({
codec: null, // no codec
});
```Or define another codec:
```js
import {Connection} from 'urpc';
import CBOR from 'cbor';const cborCodec = {
encode(v) {
return CBOR.encode(v)
},
decode(v) {
return CBOR.decode(v)
},
};const rpc = new Connection({
codec: cborCodec,
});
```## License
MIT © [Rumkin](https://rumk.in)