https://github.com/mikeal/znode
Bi-directional RPC through any stream.
https://github.com/mikeal/znode
Last synced: 3 months ago
JSON representation
Bi-directional RPC through any stream.
- Host: GitHub
- URL: https://github.com/mikeal/znode
- Owner: mikeal
- Created: 2017-08-12T06:59:10.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2021-01-04T19:33:24.000Z (over 4 years ago)
- Last Synced: 2024-10-18T09:13:55.470Z (8 months ago)
- Language: JavaScript
- Size: 18.6 KB
- Stars: 206
- Watchers: 10
- Forks: 13
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# znode
znode is a remote method execution library for Node.js and the browser.
* Bi-directional RPC over *any* stream (WebRTC, [WebSockets](https://github.com/maxogden/websocket-stream), TCP, etc)
* Supports binary types natively without serializing to strings. [1]
* Simple API using async await.
* Supports RPC methods **returning additional RPC methods.**[1] Underlying implementation uses msgpack5. Performance is optimized for binary type usage (JSON would be faster for cases other than binary types).
## Full Usage
```javascript
const RPC = {
/* basic method support */
ping: () => 'pong',
/* async methods work identicaly to sync methods */
ping2: async () => 'pong2',
/* supports binary types */
pingBuffer: () => Buffer.from('pong'),
/* you can also add static properties */
API: 'v1'
}net.createServer(async socket => {
let remote = await znode(socket, RPC)let concater = await remote.createConcat('pre-')
console.log(await concater.concat('post')) // priv-pre-post
})
.listen(async () => {const dynamicRPC = {
createConcat: str => {
let _private = 'priv-'
return {concat: _str => _private + str + _str}
}
}let socket = net.connect(port)
let remote = await znode(socket, dynamicRPC)
console.log(await remote.ping()) // pong
console.log(await remote.ping2()) // pong2
console.log(await remote.pingBuffer()) //
console.log(remote.API) // v1
})
```RPC methods can return anything that can be serialized by msgpack5.
RPC methods can also return objects with additional methods that will
be turned into additional remote methods.