All RPC methods must return a promise or an async generator
Property access on the RPC object is not supported
RPC Arguments must not be promises (though may be functions that return promises)
The values resolved/yielded from an RPC function must be serializable (e.g. contain no functions) unless custom types are used (see Custom Types below)
AsyncGenerators returned from RPC methods must be either read to completion, or their .return/.throw methods invoked
Callback functions (e.g. functions passed as arguments) should return promises or async generators
Callback functions may return void, but if so they must not throw
Example: Getting started
import { rpc } from'it-rpc'
// the invocation target interface - used by the client and the server interfaceTarget { sayHello(): Promise<string> }
// the target implementation, lives on the server side consttarget: Target = { asyncsayHello () { return'hello' } }
// create client and server constserver = rpc() constclient = rpc()
// pipe the streams together voidserver.sink(client.source) voidclient.sink(server.source)
// a string that is the same on both the server and the client constobjectName = 'target'
// expose target implementation to RPC calls on the server side server.createTarget(objectName, target)
// create a client-side version of target constclientTarget = client.createClient<Target>(objectName)
// invoke a remote method awaitclientTarget.sayHello() // 'hello'
Example: Streaming data from the server to the client
Any abort signals passed as arguments will have equivalents passed on to the
remote method invocation and these will fire their abort event when the
client side signal fires.
Your RPC objects must follow a few rules:
.return
/.throw
methods invokedvoid
, but if so they must not throwExample: Getting started
Example: Streaming data from the server to the client
Example: Aborting remote method invocations
Any abort signals passed as arguments will have equivalents passed on to the remote method invocation and these will fire their
abort
event when the client side signal fires.Custom types
It is possible to extend
it-rpc
to support serializing/deserializing custom types by passingValueCodec
s to the constructor.Each
ValueCodec
needs a uniquetype
field which identifies the value type on the wire.it-rpc
uses value types starting at1024
and has a catch-all2147483647
type which resolves to plain objects.You should define your type values higher than the max value
it-rpc
uses (2048
is a safe value) but lower than the catch-all type value.Matching codecs are searched for in
type
order so you can override the built-in codecs by specifying atype
field lower than1024
.Both the server and the client must be configured with the same set of custom
ValueCodec
sExample: Custom Types