We've got you covered in all your real-time collaboration use cases.
import Cyxth from '@cyxth/core';
import Colab, { type Context } from '@cyxth/colab';
const APP_URL = 'my-app.apps.cyxth.com';
const cyx = new Cyxth(APP_URL);
cyx.register([Colab]);
await cyx.connect('USER_TOKEN');
const stateId = 'tasks';
const tasks: Task[] = [];
const colab: Colab = await cyx.colab('https://cdn.cyxth/com/colab-0.0.1.wasm');
// start a new instance , use join or load for existing
await colab.start(stateId);
const ctx: Context = colab.getChangeContext();
// modify state and listen for changes
const addTask = async (task) => {
await ctx.tree().path("tasks").list().push(task);
};
colab.on('change', ({ senderId, data }) => {
data.apply(tasks);
});
// sync any presence data
const sendCursors = async (pos) => {
colab.presence({ type: "cursor", pos })
}
colab.on('presence', ({ senderId, data }) => {
if (data.type === 'cursor') {
// render cursor positions
}
});
// ...