This module provides a convenient way to wrap gRPC protocol messages into envelopes that can be seamlessly transmitted through a WebSocket proxy server. This package aids in the creation of WebSocket RPC streams for unary or bi-directional streaming gRPC methods. Additionally, this package includes the Code RPC protocol.
Learn more about Code at https://getcode.com. See the docs for more information.
This package wraps messages in envelopes that can be transmitted through a WebSocket proxy server. It also provides support for unary and bi-directional gRPC method streams. The streams can be server side or client side only streams as well.
Unlike other solutions, for example, gRPC-web or connect-es, this package supports bidi streams by using web-sockets instead of HTTP/2 or Fetch.
This package is not designed to be used directly by developers, but rather as a dependency for other packages within the Code SDK.
To install the module, you can use npm or yarn:
npm install @code-wallet/rpc
Here's a basic unary method example:
import * as proto from '@code-wallet/rpc';
// Create a unary method stream for sending messages
const msgSend = await proto.RpcStream.createUnaryMethod(proto.Messaging, "sendMessage", config.wsPath());
// Use the unary method stream to send a message
const res = await msgSend(req);
if (res.result != proto.SendMessageResponse_Result.OK) {
// Handle message sending failure
return;
}
Here's a basic bi-directional stream example:
// Create a bi-directional stream for opening message streams with event handlers
const msgStream = await proto.RpcStream.create(proto.Messaging, "openMessageStream", config.wsPath(), {
onClose: () => {
// Handle WebSocket stream closure
},
onError: (err: any) => {
// Handle errors
},
});
// Use the bi-directional stream
msgStream.write(new proto.OpenMessageStreamRequest({
rendezvousKey: new proto.RendezvousKey({
value: rendezvousKeypair.publicKey,
}),
}));
for await (const [res, err] of msgStream.read()) {
// Handle streamed responses and errors
}