How to use webhooks or similar in Supabase? #259
-
How do I configure webhooks in Supabase? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
hey @leibole what is the use-case you have in mind? |
Beta Was this translation helpful? Give feedback.
-
Hi,
I want to trigger functions for changes in the DB, similar to firebase functions with firestore changes.
…> On 20 Oct 2020, at 9:30, Ant Wilson ***@***.***> wrote:
hey @leibole what is the use-case you have in mind?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Beta Was this translation helpful? Give feedback.
-
Hey @leibole, we have actually built this into our Realtime server, but we haven't yet finished the Dynamic Configuration (supabase/realtime#48). We'll do this after we move out of Alpha. So here is a workaround ( it could use some improvements): 1. Enable the HTTP extension2. Create a function to send the payloadcreate or replace function send_to_webhook() returns trigger AS $trigger$
declare
rec RECORD;
payload TEXT;
column_name TEXT;
column_value TEXT;
payload_items JSONB;
begin
-- Set record row depending on operation
CASE TG_OP
WHEN 'INSERT', 'UPDATE' THEN
rec := NEW;
WHEN 'DELETE' THEN
rec := OLD;
ELSE
RAISE EXCEPTION 'Unknown TG_OP: "%". Should not occur!', TG_OP;
END CASE;
payload_items := coalesce(row_to_json(rec),'{}')::jsonb;
-- Build the payload
payload := json_build_object(
'timestamp',CURRENT_TIMESTAMP,
'operation',TG_OP,
'schema',TG_TABLE_SCHEMA,
'table',TG_TABLE_NAME,
'data',payload_items
);
-- Notify the webhook
perform extensions.http_post(TG_ARGV[0]::varchar, payload::varchar, 'application/json');
return rec;
end;
$trigger$ language plpgsql; UsageNow any time you want to send to a webhook for an update/insert/delete on a particular table, you can use it like this: -- example table
create table todos (
todo text,
is_completed boolean
);
-- sending to webhook
create trigger todos_webhook
after insert or update or delete on todos
for each row execute procedure
send_to_webhook( 'https://webhook.site/079aa292-4940-4df0-9ee3-191f2e3e15dc' ); You can change CaveatsPeople usually do something like this with postgresql's
|
Beta Was this translation helpful? Give feedback.
-
I'm running a self-hosted Supabase and Webhooks are currently not supported (see: https:/supabase/supabase/issues/12863). I noticed that this solution causes an issue if the work done by the webhook is inserting data to another table with a foreign key constraint on the record that was inserted causing the trigger. I believe this is because http_post is blocking and the row actually isn't returned until after that. Is there a way around this? |
Beta Was this translation helpful? Give feedback.
Hey @leibole, we have actually built this into our Realtime server, but we haven't yet finished the Dynamic Configuration (supabase/realtime#48). We'll do this after we move out of Alpha.
So here is a workaround ( it could use some improvements):
1. Enable the HTTP extension
2. Create a function to send the payload