A TypeScript toolkit for consuming the Bluesky network in real-time.
at main 52 lines 1.3 kB view raw
1// Throughput Counter 2// Connects to Jetstream and counts post creates per second — a simple, 3// real-time throughput meter. Prints a line every second with the current 4// rate and running total. 5// 6// Usage: npx tsx examples/throughput.ts 7 8import { startStreamWithReconnect, isPostCreate } from "../lib/index.js"; 9 10// --- Counters --- 11 12let total = 0; 13let thisSecond = 0; 14const startTime = Date.now(); 15 16// --- Summary on exit --- 17 18const printSummary = () => { 19 const elapsed = (Date.now() - startTime) / 1_000; 20 const avg = elapsed > 0 ? (total / elapsed).toFixed(1) : "0"; 21 console.log(`\n--- Session summary ---`); 22 console.log(`Duration: ${elapsed.toFixed(1)}s`); 23 console.log(`Total: ${total} posts`); 24 console.log(`Average: ${avg} posts/sec`); 25 process.exit(0); 26}; 27 28process.on("SIGINT", printSummary); 29process.on("SIGTERM", printSummary); 30 31// --- Print loop (every 1 second) --- 32 33setInterval(() => { 34 const rate = thisSecond; 35 total += rate; 36 console.log(`${rate} posts/sec | ${total} total`); 37 thisSecond = 0; 38}, 1_000); 39 40// --- Stream --- 41 42startStreamWithReconnect({ 43 config: { wantedCollections: ["app.bsky.feed.post"] }, 44 onEvent: (event) => { 45 if (isPostCreate(event)) { 46 thisSecond++; 47 } 48 }, 49 onOpen: () => { 50 console.log("Connected to Jetstream. Counting posts per second...\n"); 51 }, 52});