Realtime
Broadcast messages and track who's online.
Channels let clients send each other messages (Broadcast) and share who's online (Presence). Subscriptions use Server-Sent Events.
Base URL: https://api.clouzark.com/realtime/<database ref>/channels/<channel>
Channel names use letters, digits, and _ - : .. Channels starting with private: refuse the anon key, so only signed-in users (or the service key) can join them.
Subscribe
EventSource can't send headers, so pass credentials as query parameters (apikey, and access_token for a user token):
const url = `https://api.clouzark.com/realtime/my-app-db/channels/room-1?apikey=${ANON_KEY}`;
const events = new EventSource(url);
let connectionToken = '';
events.addEventListener('system', (e) => {
connectionToken = JSON.parse(e.data).connectionToken;
});
events.addEventListener('broadcast', (e) => console.log(JSON.parse(e.data)));
events.addEventListener('presence', (e) => console.log(JSON.parse(e.data)));Broadcast
await fetch(`https://api.clouzark.com/realtime/my-app-db/channels/room-1/broadcast`, {
method: 'POST',
headers: { apikey: ANON_KEY, 'Content-Type': 'application/json' },
body: JSON.stringify({ event: 'cursor', payload: { x: 10, y: 20 }, connectionToken }),
});Pass the connectionToken from your subscription to skip echoing the message back to yourself, or self: true to receive it too.
Presence
| Request | Does |
|---|---|
GET .../presence | Who's on the channel |
POST .../presence { connectionToken, key?, state } | Share this connection's state |
DELETE .../presence { connectionToken } | Stop sharing |
Subscribers get presence events whenever someone joins, leaves, or updates their state.