Official package · Automation and infrastructure

Supabase table changes as realtime events

Send Supabase table changes to browsers and apps as Hibira events: a Database Webhook calls an Edge Function that publishes them.

  • A Database Webhook sends every insert, update and delete to an Edge Function, which publishes it with the pusher npm package.
  • Each table becomes a channel and each change an event (insert, update, delete), so browsers and apps subscribe with pusher-js or any Pusher client.

Sign in to see these snippets with your own cluster host. Sign in

  1. Set it up

    supabase functions new hibira-broadcast
    supabase secrets set HIBIRA_APP_ID=YOUR_APP_ID HIBIRA_APP_KEY=YOUR_APP_KEY \
      HIBIRA_APP_SECRET=YOUR_APP_SECRET HIBIRA_HOST=ws.hibira.io \
      WEBHOOK_SECRET=a-long-random-string
  2. Publish table changes from the function

    // supabase/functions/hibira-broadcast/index.ts
    import Pusher from 'npm:pusher@5';
    
    const env = (k: string) => Deno.env.get(k) ?? '';
    const pusher = new Pusher({
      appId: env('HIBIRA_APP_ID'),
      key: env('HIBIRA_APP_KEY'),
      secret: env('HIBIRA_APP_SECRET'),
      host: env('HIBIRA_HOST'),
      port: '443',
      useTLS: true,
    });
    
    // A Database Webhook posts { type, table, record, old_record }.
    Deno.serve(async (req) => {
      if (req.headers.get('x-webhook-secret') !== env('WEBHOOK_SECRET')) {
        return new Response('forbidden', { status: 403 });
      }
      const { type, table, record, old_record } = await req.json();
      // orders → channel "orders", event "insert" / "update" / "delete"
      await pusher.trigger(table, type.toLowerCase(), record ?? old_record);
      return new Response('ok');
    });
  3. Send table changes to the function

    ; supabase functions deploy hibira-broadcast --no-verify-jwt
    ; Then in the Supabase dashboard: Database → Webhooks → Create a new hook
    Table = public.orders
    Events = Insert, Update, Delete
    Type = Supabase Edge Functions
    Edge Function = hibira-broadcast
    HTTP Headers = x-webhook-secret: a-long-random-string
  4. Use it

    import Pusher from 'pusher-js';
    
    const pusher = new Pusher('YOUR_APP_KEY', {
      cluster: 'hkg',
      wsHost: 'ws.hibira.io',
      wsPort: 443,
      wssPort: 443,
      forceTLS: true,
      enabledTransports: ['ws', 'wss'],
    });
    
    pusher.subscribe('orders').bind('insert', (row) => {
      console.log('New order', row);
    });

Before you start

  • The function is deployed with --no-verify-jwt so Database Webhooks can call it, so it checks a shared secret header on every request.
  • Only publish rows on public channels when anyone may see them. For per-user data, publish to private-user-<id> channels and sign subscriptions in another Edge Function that checks the Supabase session.

Use the app key from your dashboard; the secret stays on your server. Channels, events and authorization endpoints work exactly as they do with Pusher. Create your free account →