const tokenResponse = await fetch(
"https://app.runtm.com/api/cloud/sessions/ses_abc123/ws-token",
{
method: "POST",
headers: {
"Authorization": "Bearer runtm_sk_live_abc123...",
"Content-Type": "application/json",
},
body: JSON.stringify({ capability: "collaborate" }),
}
);
const { token } = await tokenResponse.json();
const ws = new WebSocket(
`wss://app.runtm.com/api/cloud/sessions/ses_abc123/collaborate?token=${token}`
);
const activeUsers = new Map();
ws.onmessage = (event) => {
const msg = JSON.parse(event.data);
switch (msg.type) {
case "user_joined":
activeUsers.set(msg.user.id, msg.user);
console.log(`${msg.user.name} joined (${msg.active_users} online)`);
break;
case "user_left":
activeUsers.delete(msg.user.id);
console.log(`${msg.user.name} left (${msg.active_users} online)`);
break;
case "cursor_update":
console.log(`${msg.user_id} → ${msg.position.file}:${msg.position.line}`);
break;
}
};