Skip to main content

Next.js — Flush and shutdown

Flush queued events immediately with flush(), or stop the flush timer and flush with shutdown() before process exit. Both are async (return a Promise). Note: These methods are server-side only. For client-side flushing, see Recording control.

flush()

Flush all queued events to the server immediately. Returns a Promise. When to use: When you need to ensure events are sent before a response or before the process exits (e.g. in a serverless function or after a critical path).
// app/api/checkout/route.ts
import { signal } from '@/lib/signal-server';
import { NextRequest } from 'next/server';

export async function POST(request: NextRequest) {
  // ... process checkout ...

  // Flush events before returning response
  await signal.flush();

  return Response.json({ success: true });
}

shutdown()

Stop the flush timer and flush remaining events. Call before process exit for graceful shutdown. Returns a Promise. When to use: In cleanup handlers (e.g. process.on('SIGTERM', ...)) so no events are lost when the process exits.
// lib/signal-server.ts
import { createSignalServer } from '@signal-js/nextjs/server';

export const signal = createSignalServer({
  apiKey: process.env.SIGNAL_API_KEY!,
  projectId: process.env.SIGNAL_PROJECT_ID!,
  // endpoint is optional (defaults to https://api.trysignal.ai/)
});

// Graceful shutdown
process.on('SIGTERM', async () => {
  await signal.shutdown();
  process.exit(0);
});

Automatic flushing

By default, the server client automatically flushes events:
  • When flushBatchSize (default: 20) events are queued
  • Every flushInterval milliseconds (default: 10000ms)
Configure these in createSignalServer options. See Configuration.

See also