Subscribe to document insert, update, or delete mutations happening inside MongoDB.
const channel = baas.realtime.collection("todos").subscribe();
channel.on("created", (todo) => {
console.log("New todo created:", todo);
});
channel.on("updated", (todo) => {
console.log("Todo updated:", todo);
});
channel.on("deleted", ({ documentId }) => {
console.log("Todo deleted ID:", documentId);
});
// Clean up connection:
channel.unsubscribe();Subscribe with generic interface arguments to type the mutation payloads:
import type { Document } from "@kroxt/baas-sdk";
interface Todo {
text: string;
completed: boolean;
}
const channel = baas.realtime.collection("todos").subscribe();
// doc is typed as Document<Todo> automatically
channel.on("created", (doc: Document<Todo>) => {
console.log("Created Todo text:", doc.data.text);
});