Skip to main content

Realtime

cstar.realtime opens a Server-Sent Events stream and dispatches every event the team's API key can see. Auto-reconnect with exponential backoff, gap replay via Last-Event-ID, and the same wildcards the webhook subscriptions accept.

Subscribe to specific events

subscribe.js
// Specific event
const offCreated = cstar.realtime.on('ticket.created', (event) => {
  console.log('New ticket', event.data.ticket.id);
});

// Wildcard
const offAllTickets = cstar.realtime.on('ticket.*', (event) => {
  console.log(event.type, event.data);
});

// Tear down
offCreated();
offAllTickets();

Subscribe to everything

onany.js
const off = cstar.realtime.onAny((event) => {
  log.event(event.type, event.data);
});

Connection lifecycle

lifecycle.js
cstar.realtime.onConnectionChange((connected) => {
  // Show or hide your "live" indicator
  setLiveIndicator(connected);
});

cstar.realtime.onError((err) => {
  console.warn('SSE error', err);
});

console.log(cstar.realtime.connected); // boolean

// Tear everything down on logout — also cleans up the realtime client
cstar.destroy();

Server-side filters

Pass options.types when constructing the client to scope the stream at the server. The default subscribes to everything; specifying types reduces traffic on the wire and processing in your handlers.

scoped.js
const cstar = new CStarClient({
  apiKey: process.env.CSTAR_KEY,
  teamId: process.env.CSTAR_TEAM_ID,
  // Realtime is configured via the realtime instance directly:
});

// Then on the instance:
cstar.realtime.on('ticket.created', handler);
cstar.realtime.on('ticket.updated', handler);
// Other events still arrive — but only registered handlers fire.

Realtime vs. webhooks

Webhooks deliver to a server you host; realtime delivers to a client you've already authenticated. Use webhooks when the receiver is your backend (CRM sync, billing automation). Use realtime when the receiver is your own dashboard or admin app — no public endpoint to expose, no signing secrets to rotate.

Next up