Skip to main content

Emit events

Your server calls vault.emit() to push events to subscribed clients in real time. Events can carry any JSON payload.

Emitting via vaultMiddleware

app.post('/process', walletMiddleware(...), payMiddleware(...), vaultMiddleware(), async (req, res) => {
  const { items } = req.body;
  const vault = req.vault!;

  // Respond immediately — client subscribes to the stream in parallel
  res.json({
    vaultId:   vault.id,
    streamUrl: `https://api.prudra.dev/vaults/${vault.id}/events`,
  });

  // Run job after response
  setImmediate(async () => {
    await vault.emit('job.started', {
      itemCount: items.length,
      startedAt: new Date().toISOString(),
    });

    for (let i = 0; i < items.length; i++) {
      await processItem(items[i]);
      await vault.emit('job.progress', {
        current: i + 1,
        total:   items.length,
        percent: Math.round(((i + 1) / items.length) * 100),
        item:    items[i],
      });
    }

    await vault.addDocument({ results }, 'Job results');
    await vault.seal('All items processed');
  });
});

Emitting via API

import { initialise } from '@prudra/core';
import { emitVaultEvent } from '@prudra/vault';

initialise({ apiKey: process.env.PRUDRA_API_KEY! });

await emitVaultEvent({
  vaultId: 'vlt_clx1abc123',
  type:    'job.progress',
  payload: { current: 5, total: 10 },
});

Parameters

ParameterTypeRequiredDescription
vaultIdstringYesThe vault to emit to
typestringYesEvent type string (any value; use dot notation convention)
payloadobjectNoJSON payload — any structure

Event naming conventions

Use dot-notation event types for clarity:
job.started
job.progress
job.step.completed
job.completed
item.error
Avoid spaces and special characters. Maximum type length is 128 characters.

Error handling

ErrorStatusCauseResolution
vault-sealed409Cannot emit to a sealed vaultEmit before sealing
vault-expired404Vault has expiredNo action — vault is gone