Closed
Description
Currently LoopBack supports RPC style APIs such as REST or gRPC. We would like to extend that with messaging/eventing capabilities.
Use cases
- Expose endpoints to accept a stream of messages/events and/or return a stream of messages/events (such as SSE)
- Connect to backend resources (such as Kafka or MQTT Broker) to produce/consume (including pub/sub) messages/events
- Define business functions to produce/consume a stream of messages/events (synchronously or asynchronously)
- Create serverless actions (such as IBM Cloud Functions and AWS Lambda) with LoopBack 4 as event handlers
- Observe events and react on them (hooks? TBD)
- Make it easy to implement web hooks
Protocols
- WebSocket
- HTTP/2 with SSE
- MQTT
Mapping to LoopBack 4 concepts
Servers
: listen on WebSocket, MQTT, and HTTP/2 protocolsConnectors
: subscribe to an event source or message broker or publish messages to backend systemsControllers
: create methods (with decorators) to produce/consume/process a stream of events.Events
: define models representing various types of events
Examples
app.server(WebSocketServer, config);
app.datasource({name: 'kafka', ...};
export class MoveEvent {
gameId: string;
sequence: number;
player: string;
shape: 'o' | 'x';
move: [number, number];
}
export class GameStatus {
id: string;
board: ('o' | 'x' )[][];
winner: string;
status: string;
}
export class TicTakToeController {
async createGame(player1: string, player2: string): number {}
// Should we support event by event, a stream of events, or both
handleMoves(move: AsyncIterator<MoveEvent>): AsyncIterator<GameStatus> {}
async handleMove(move: MoveEvent): GameStatus {}
}
export class MyEventController {
@subscribe('order.created')
async onNewOrders(@event('order.created') orderCreatedEvent: OrderCreated) {}
@publish('invoice.created')
generateInvoices(): AsyncIterator<InvoiceCreated> {}
}