Spoosh
Plugins

Polling

Automatic periodic refetching

The polling plugin automatically refetches data at specified intervals.

Installation

npm install @spoosh/plugin-polling

Usage

import { Spoosh } from "@spoosh/core";
import { pollingPlugin } from "@spoosh/plugin-polling";

const client = new Spoosh<ApiSchema, Error>("/api").use([pollingPlugin()]);

// Poll every 5 seconds
injectRead((api) => api("notifications").GET(), { pollingInterval: 5000 });

// Disable polling (default)
injectRead((api) => api("posts").GET(), { pollingInterval: false });

Dynamic Polling

The polling interval can be a function that adjusts based on the response:

injectRead((api) => api("jobs/:id").GET({ params: { id: jobId } }), {
  pollingInterval: (data, error) => {
    // Stop polling when job is complete
    if (data?.status === "completed") return false;

    // Poll faster while job is running
    if (data?.status === "running") return 1000;

    // Slower polling on error
    if (error) return 10000;

    // Default interval
    return 5000;
  },
});

Options

Per-Request Options

OptionTypeDescription
pollingIntervalnumber | false | (data, error) => number | falsePolling interval in ms, false to disable, or a function for dynamic intervals

Example: Real-Time Dashboard

@Component({
  selector: "app-dashboard",
  template: `
    <div>
      <app-stats-panel [stats]="stats.data()" />
      <app-alerts-list [alerts]="alerts.data()" />
    </div>
  `,
})
export class DashboardComponent {
  stats = injectRead(
    (api) => api("stats").GET(),
    { pollingInterval: 30000 } // Refresh every 30 seconds
  );

  alerts = injectRead(
    (api) => api("alerts").GET(),
    { pollingInterval: 5000 } // Check alerts more frequently
  );
}

On this page