Simple RPC over Protomux channels.
npm install protomux-rpc
On the server side:
const ProtomuxRPC = require('protomux-rpc')
const rpc = new ProtomuxRPC(stream)
rpc.respond('echo', (req) => req)
On the client side:
const ProtomuxRPC = require('protomux-rpc')
const rpc = new ProtomuxRPC(stream)
await rpc.request('echo', Buffer.from('hello world')))
// <Buffer 'hello world'>
Construct a new RPC channel from a framed stream.
Options include:
{
// Optional binary ID to identify this RPC channel
id: buffer,
// Optional protocol name
protocol: 'protomux-rpc',
// Optional default value encoding
valueEncoding: encoding,
// Optional handshake
handshake: buffer,
// Optional encoding for the handshake
handshakeEncoding: encoding
}
Construct a new RPC channel from an existing muxer.
Options are the same as new ProtomuxRPC(stream)
.
Whether or not the RPC channel is closed.
The muxer used by the channel.
The stream used by the channel.
Register a handler for an RPC method. The handler is passed the request value and must either return the response value or throw an error.
Only a single handler may be active for any given method; any previous handler is overwritten when registering a new one.
Options include:
{
// Optional encoding for both requests and responses, defaults to raw
valueEncoding: encoding,
requestEncoding: encoding, // Optional encoding for requests
responseEncoding: encoding // Optional encoding for responses
}
Remove a handler for an RPC method.
Perform an RPC request, returning a promise that will resolve with the value returned by the request handler or reject with an error.
Options are the same as rpc.respond()
with the addition of:
{
timeout: -1 // Optional request timeout in milliseconds
}
Perform an RPC request but don't wait for a response.
Options are the same as rpc.respond()
.
Cork the underlying channel. See channel.cork()
for more information.
Uncork the underlying channel. See channel.uncork()
for more information.
Gracefully end the RPC channel, waiting for all inflights requests and response handlers before closing.
Forcefully close the RPC channel, rejecting any inflight requests.
Emitted when the remote side adds the RPC protocol.
Emitted when the RPC channel closes, i.e. when the remote side closes or rejects the RPC protocol or we closed it.
Emitted when the RPC channel is destroyed, i.e. after close
when all pending promises has resolved.
All types are specified as their corresponding compact-encoding codec.
uint
The ID of the requeststring
The method to callraw
The request value
A request ID of 0
indicates an event call and must not be responded to.
bitfield(1)
Flagserror
uint
The ID of the request- (if
error
is set)string
The error message - (if
error
is not set)raw
The response value
Apache-2.0