Skip to content
Docs

Recipes

The same requests for 1 visitor or 10,000

A page many people open shows the latest calls. Your server reads /v1/feed at the pace it names, once a minute today, and keeps the result; every visitor's page is built from that copy.

On this page
  1. Gather the parts
  2. Copy this file into your backend
  3. Read it from your route handlers
  4. What it handles

Your key never leaves your server.

  1. Gather the parts

    A Partner API key with access to /v1/feed, Node 18 or later, and feed-client.mjs copied from the API quickstart.

  2. Copy this file into your backend

    Save it as cache.mjs next to feed-client.mjs.

    cache.mjs
    // The same requests for 1 visitor or 10,000: reads the feed of your tickers at the pace it names and
    // keeps the result for every visitor's page. Copy it into your backend next to feed-client.mjs (Node 18 or
    // later, no packages).
    import { followFeed } from './feed-client.mjs';
    
    export function startCallsCache({ key, tickers }) {
      let problem = null, ready = false;
      const feed = followFeed({
        key, tickers,
        onChange: () => { problem = null; ready = true; },
        onProblem: (error) => { problem = error.message; console.error(problem); },
      });
      return {
        // The newest `limit` calls on one ticker, or on all of them; never a request to Buzzberg.
        calls(ticker = '', limit = 20) {
          const symbol = String(ticker).trim().toUpperCase();
          return [...feed.posts.values()]
            .filter((post) => !symbol || post.ticker === symbol)
            .sort((a, b) => b.published_at.localeCompare(a.published_at))
            .slice(0, limit);
        },
        // False until the first complete read: there are no calls to show yet.
        ready: () => ready,
        // True when the last read failed: the calls are the last ones that arrived.
        stale: () => problem !== null,
        stop: feed.stop,
      };
    }
    
  3. Read it from your route handlers

    Your server
    import { startCallsCache } from './cache.mjs';
    
    const cache = startCallsCache({ key: process.env.BUZZBERG_API_KEY, tickers: ['BTC', 'ETH', 'NVDA'] });
    
    // In the handler of your page's route. Before the first read, or while reads fail, send your page
    // without calls or mark it stale, and do not let a shared cache keep that response.
    const calls = cache.ready() ? cache.calls('NVDA') : [];   // the newest 20 calls on NVDA, from the kept copy
    const stale = !cache.ready() || cache.stale();
    

What it handles

Many visitorsNo visitor waits on Buzzberg.
Before the first readready() is false and there are no calls yet.
Failed readThe last calls stay available and stale() is true.
Untracked ticker, 429, rejected keyThe feed client leaves the untracked ticker out, waits Retry-After, or stops polling (feed client).