@autobe/rpc
RPC module of @autobe
for WebSocket Communication.
If you are considering to make an @autobe
based service, WebSocket protocol is essentially required. Chatting application is basically a two-way communication. AI chatbot is also a two-way chat between an user and an assistant, based on events. Therefore, it cannot be a one-way communication HTTP Restful API, and a two-way protocol such as WebSocket is required.
However, donāt be afraid of WebSocket application development. Below is the example codes of WebSocket application development utilizing @autobe/rpc
in both client and server side. Look at the example codes, and feel how easy and type-safe it is.
Client Application
import { IAutoBeRpcListener, IAutoBeRpcService } from "@autobe/interface";
import { Driver, WebSocketConnector } from "tgrid";
const connector: WebSocketConnector<
null,
IAutoBeRpcListener, // provider to remote
IAutoBeRpcService // controller of remote
> = new WebSocketConnector(null, {
assistantMessage: async (evt) => {
console.log("assistant", evt.text);
},
analyzeComplete: async (evt) => {
console.log("analyze completed", evt.files);
},
prismaComplete: async (evt) => {
console.log("prisma completed", evt.schemas);
},
interfaceComplete: async (evt) => {
console.log("interface completed", evt.files);
},
testComplete: async (evt) => {
console.log("test completed", evt.files);
},
realizeComplete: async (evt) => {
console.log("realize completed", evt.files);
},
});
await connector.connect("ws://localhost:3001");
const driver: Driver<IAutoBeRpcService> = connector.getDriver();
await driver.conversate("Hello, what you can do?");
Setup
NodeJS Server
npm
npm install @autobe/agent @autobe/compiler @autobe/interface
npm install @autobe/rpc tgrid
To develop NodeJS WebSocket server of @autobe
, you need to install these packages.
At first, install @autobe/agent
, @autobe/compiler
and @autobe/interface
packages, which are required for chatbot. And then, install @autobe/rpc
and tgrid
packages.
tgrid
is a TypeScript based RPC (Remote Procedure Call) framework supporting WebSocket protocol, and @autobe/rpc
is a wrapper module of @autobe/agent
following the WebSocket RPC.
NestJS Server
npm
npm install @nestjs/common @nestjs/core @nestjs/platform-express
npm install @autobe/agent @autobe/compiler @autobe/interface
npm install @autobe/rpc tgrid
npm install -D nestia
npx nestia setup
Install NestJS and @autobe
packages, and then setup nestia
.
@nestia
is a set of helper libraries for NestJS, and it supports WebSocket protocol that is following the RPC (Remote Procedure Call) paradigm.
Client Application
npm
npm install @autobe/rpc tgrid
Just install @autobe/rpc
, tgrid
packages.
Remote Procedure Call
WebSocket protocol with RPC paradigm for @autobe
.
Header
Header value delivered from client to server after the connection.
In TGrid
ās RPC (Remote Procedure Call) paradigm, header means a value that is delivered after the connection from a client to the server. And header is used in most cases to authenticate the connecting client.
In the above example project, IAuthorizationHeader
is the header type, and is used by server to determine whether to accept the clientās connection or not. If the clientās header is not valid, the server would reject the connection.
Provider
Functions provided from remote system.
Provider is an object instance containing some functions provided for the remote system for RPC (Remote Procedure Call). In many cases, the provide becomes a class instance containing some methods to be called, but it is okay that composing the provider by just an interface type.
Also, the opposite remote system will call providerās functions by the Driver<Remote>
instance. In the above example, client application is providing IAutoBeRpcListener
to the server, and server is providing AutoBeRpcService
(IAutoBeRpcService
) to the client.
Driver
Driver of RPC (Remote Procedure Call).
Driver
is a proxy instance designed to call functions of the remote system. It has a generic argument Remote
which means the type of remote systemās Provider, and you can remotely call the functions of the Provider asynchronously through the Drive<Remote>
instance.
When you call some function of remote Provider by the Driver<Listener>
instance, it hooks the function call expression, and delivers the function name and arguments (parameter values) to the remote system through the Communicator. If the remote system succeeded to reply the result of the function call, Communicator resolves the promise of the function call expression with the result, so that makes Driver<Remote>
working.
In the above example, client application is calling IAutoBeRpcService.conversate()
function remotely through the Driver<IAutoBeRpcService>
typed instance. In that case, IAutoBeRpcService
is the Provider instance from server to client.