I'm running Apache Answer on a DigitalOcean Droplet. I'm experimenting with running its database on Supabase Postgres. I have a DigitalOcean Function that makes an API update to Apache Answer whenever I call it manually, and it's working well.
Now I want that DigitalOcean Function to get called automatically whenever a new question is inserted into the Postgres database.
In answer to another question, you suggested the below approach. Can you help me get this setup and running step by step.
A. Using Supabase Realtime API
Supabase provides a Realtime feature that can listen to changes in your database. You can subscribe to new inserts and, once detected, trigger your DigitalOcean Function.
Steps:
-
Set up Realtime:
import { createClient } from '@supabase/supabase-js'; const supabase = createClient('https://your-project-id.supabase.co', 'your-public-anon-key'); const channel = supabase .channel('*') .on('postgres_changes', { event: 'INSERT', schema: 'public', table: 'questions' }, payload => { console.log(payload); // Call the DigitalOcean function with the new question payload fetch('https://faas-nyc1-2ef2e6cc.doserverless.co/api/v1/namespaces/fn-4debf4e9-6121-41dc-a674-9ba721490ff8/actions/answer/test-pg-chatgpt-4o?blocking=true&result=true', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Basic ${process.env.DO_TOKEN}`, }, body: JSON.stringify({ question_id: payload.new.id }), }).then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error('Error:', error)); }) .subscribe();
-
Deploy and Run:
- Deploy this script on a serverless environment or a microservice framework to ensure it runs continuously.
- Handle potential network interruptions and auto-reconnect to Supabase Realtime.