Persistent Store

SBCanvas.store is async key-value storage backed by the StreamBot server. Data survives overlay disconnects, browser refreshes, and OBS restarts.

Writing Data

await SBCanvas.store.set('highScore', 42);
await SBCanvas.store.set('leaderboard', [
  { name: 'User1', score: 100 },
  { name: 'User2', score: 85 }
]);

Reading Data

const score = await SBCanvas.store.get('highScore'); // 42
const board = await SBCanvas.store.get('leaderboard'); // [{...}, {...}]
const missing = await SBCanvas.store.get('nonexistent'); // null

Deleting Data

await SBCanvas.store.delete('highScore');

Getting All Data

const everything = await SBCanvas.store.getAll();
// { highScore: 42, leaderboard: [...] }

Offline Resilience

If the overlay loses connection, writes are queued locally. On reconnect, queued writes replay in order. No data is lost during brief disconnections.
// This works even if the socket drops mid-write
await SBCanvas.store.set('counter', newValue);
// If offline: queued and retried automatically

Listening for Changes

A store:update event fires whenever a key is written:
SBCanvas.on('store:update', ({ key, value }) => {
  if (key === 'highScore') {
    document.getElementById('score').textContent = value;
  }
});

Example: Persistent Counter

SBCanvas.ready(async () => {
  let count = (await SBCanvas.store.get('followCount')) || 0;
  document.getElementById('count').textContent = count;

  SBCanvas.activities.on('follow', async () => {
    count++;
    document.getElementById('count').textContent = count;
    await SBCanvas.store.set('followCount', count);
  });
});

Example: Session Leaderboard

SBCanvas.ready(async () => {
  const board = (await SBCanvas.store.get('tipBoard')) || [];

  SBCanvas.activities.on('tip', async (activity) => {
    board.push({ user: activity.username, amount: activity.amount });
    board.sort((a, b) => b.amount - a.amount);
    board.length = Math.min(board.length, 10); // keep top 10
    await SBCanvas.store.set('tipBoard', board);
    renderLeaderboard(board);
  });
});

API Reference

MethodReturnsDescription
store.set(key, value)Promise<boolean>Store a value (JSON-serializable)
store.get(key)Promise<any>Retrieve a value (null if missing)
store.delete(key)Promise<boolean>Delete a key
store.getAll()Promise<object>Get all stored key-value pairs