Skip to Content

Setup

Terminal
npm install @autobe/rpc tgrid

You can start WebSocket client application by installing @autobe/rpc and tgrid packages.

tgrid is a TypeScript based RPC (Remote Procedure Call) framework supporting WebSocket protocol, and @autobe/rpc is an wrapper module of @autobe/core following the WebSocket RPC.

Development

client/main.ts
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?");

You can develop WebSocket client application like above.

At first, create an WebSocketConnector instance with IAutoBeRpcListener and IAutoBeRpcService type specifications, and constructing the IAutoBeRpcListener typed instance to provide event handlers. The IAutoBeRpcListener typed would be provided to the WebSocket server, so that the server will call the IAutoBeRpcListener functions remotely (Remote Procedure Call).

And then connect to the server with WebSocketConnector.connect() method specifying the server’s WebSocket address. If server accepts the connection, you can go to the next step. Otherwise server rejects your connection, an exception would be thrown.

After the connection, you can start conversation by calling the server’s functions remotely (Remote Procedure Call) to the Driver<IAutoBeRpcService> typed instance obtained from the WebSocketConnector.getDriver() method. Whenever you call some functions of the Driver<IAutoBeRpcService> instance, the server will call the corresponding functions of the IAutoBeRpcListener instance.

Note that, WebSocket protocol is different with HTTP protocol. The connection would be kept until you call the WebSocketConnector.close() method manually. So, if your business has been completed, please don’t forget to closing the connection.

Event Listener

client/main.ts
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?");

Looking at the IAutoBeRpcListener interface provided from client to server for event listening, you can find that assistantMessage and {analyze|prisma|interface|test|realize}Complete functions are mandatory, and the other functions are all optional. You have to implement these functions to handle the events from the server.

The other optional functions, they are not mandatory, but recommended to implement. They are used to track the progress of the server’s work, so that you can show the progress to the user.

For example, when the @autobe server is on the prisma phase, the progress would be started from the prismaStart function, and then prismaComponents, prismaSchemas, prismaComplete functions would be called in order. During the prisma phase, if validation or compilation errors are occurred, the prismaValidate or prismaCorrect function would be called.

Last updated on