@appweaver/realtime
v2.10.0
Published
Appweaver - the backend framework for AI-first development (@realtime)
Maintainers
Readme
Description
@appweaver/realtime pushes events to the clients of an Appweaver API over
a WebSocket or Server-Sent Events, a module enabled by installing it (it requires @appweaver/web). The clients
subscribe to:
- the channels of the application and of the modules, created with
createChannel - the resource events (
create,update,delete) of the models exposing them withcreateRoutes({ events })
and call the custom actions of createRealtimeAction over the WebSocket, waiting for the result or
fire-and-forget. The streams carry chunks rather than events, i.e. the tokens of an LLM answer: a streaming action
streams its chunks to its caller, and createStream to the subscribers of a key, each subscriber checked once and never
per chunk. An action with an input receives the chunks a client streams over the WebSocket, i.e. the
audio of a microphone. A stream bound to a resource lets subscribe the users the policy of its model lets read the
record, and a logout, a disabling or a lost role ends the calls of a user at once. Nothing is exposed unless declared, and every event is checked against the access of the subscribed
user: the callbacks of the channel, or the events callback of the resource policy, else its checkAccess and
readRestrictions. With @appweaver/redis the events reach the connections of every instance through the Redis
pub/sub.
Installation
npm install @appweaver/realtimeor weaver module add realtime in a project created with @appweaver/create.
Usage
import { Type } from '@sinclair/typebox';
import { createChannel, createRealtimeAction } from '@appweaver/realtime';
export const orderShipped = createChannel({
name: 'order.shipped',
payload: Type.Object({ orderId: Type.Integer(), customerId: Type.Integer() }),
deliver: (user, payload) => payload.customerId === user?.id
});
createRealtimeAction({
name: 'chat.typing',
payload: Type.Object({ roomId: Type.Integer() }),
handler: async ({ roomId }, { user }) =>
typing.publish({ roomId, userId: user!.id })
});
await orderShipped.publish({ orderId: 7, customerId: 1 });
createRealtimeAction({
name: 'assistant.prompt',
payload: Type.Object({ prompt: Type.String() }),
stream: { codec: 'text', coalesce: 16 },
async *handler({ prompt }, { signal }) {
for await (const token of llm.stream(prompt, { signal })) yield token;
}
});The client generated by weaver-client generate connects with a ticket of POST /api/realtime/ticket:
client.realtime.subscribe('order.shipped', (payload) => console.log(payload));
client.realtime.on('Order', (event) =>
console.log(event.action, event.current)
);
await client.realtime.send('chat.typing', { roomId: 1 });
for await (const token of client.realtime.call('assistant.prompt', { prompt })) {
output += token;
}See the realtime reference for the protocol, the config and the limits.
License
Appweaver is MIT licensed.
