RPC Variants
Maglev RPC supports streaming and unary data, both in the requesting and response directions. This can be extraordinarily useful in many cases. The API for each is a little different though.
Variants#
There are 4 possible RPC variants in proto3:
Clients#
Synthesized client code is used a little different depending on unary vs streaming for an RPC. Given the above proto definition we can create a client and invoke the various RPC types like so:
Hosting an RPC#
At the other end of the RPC is the 'host', ie the node responding to the RPC call. It's important to keep that fact in mind, as the request and response are logically flipped. Unlike the client API, the host API offers several variations on implementing the same RPC type. Depending on what your use case is, one might be better than another.
The handler object can be either a plain old JS object (which is much easier to
deal with in TypeScript because of type inference) or a class instance that
implements the <SERVICE-NAME>Handler interface. We will use the latter in
these examples.
As mentioned before, RPC handlers for each type come in a few variants.
Unary#
Unary RPC handlers need to either return a value directly, or a Promise<T>.
Streaming From Host#
Streaming from the host means the handler is returning a stream of data to the client. This can be achieved in one of 3 ways.
By returning a ChannelReceiver<T>, which is often useful if the data is being
"generated" elsewhere in code. The ChannelReceiver<T> can be returned
directly, or as a Promise<ChannelReceiver<T>>.
If the data is being generated directly though, it's often more useful to use the ES6 async generator API.
Streaming To Host#
Streaming to the host is a little less interesting, the req parameter is
just a ChannelReceiver<T>.
Bi-directional Streaming#
Bi-directional streaming has the same variants as streaming from the host, ie.
you can either return a ChannelReceiver<T> or use an ES6 async generator. For
example...