Sending broadcast events from edge function
I want to send a broadcast event from an edge function but have been unable to do so so far. I'd like to use broadcast instead of listening to postgres changes because I'd like to hide some info from the client.
Using supabase 2.57.4
- I think. I have this version installed in my client code. I used a starter app for edge functions, and later realized it was using 2.35 in the deno.json file. I updated it and restarted etc but not sure if there's something else I need to do.
Using a local supabase instance with supabase start and supabase functions serve.
I've verified that broadcasting events works between clients. I have two clients and call:
channel.send({ type: 'broadcast', event: 'update', payload: { foo: 'bar' } })
and the other receives it correctly.
In my edge function, I'm not subscribing to the channel. My understanding is that calling channel.send without subscribing will invoke a rest call which is correct.
In my docker logs, it displays the following errors:
"POST /realtime/v1/api/broadcast HTTP/1.1" 500 23 "-" "Deno/2.1.4 (variant; SupabaseEdgeRuntime/1.69.8)"
Ranch listener RealtimeWeb.Endpoint.HTTP, connection process #PID<0.34838.0>, stream 2 had its request process #PID<0.34840.0> exit with reason {{{%MatchError{ term: [""] }, ...
Any ideas? Broadcasting from an edge function is supported right?19 Replies
This is the method to use.
https://supabase.com/docs/guides/realtime/broadcast?queryGroups=http&http=POST#send-messages-using-rest-calls
Are you using private channels (with RLS on realtime.messages) or the default public channel?
This is my edge function code. I believe I'm using the default public channel - I don't have any rls policies set up for realtime.
I also tried setting up the supabase client with the anon key in the edge function instead of the service role key, but no difference.
Wonder if somehow you are still running 2.35 as this would for sure not work with that version.
You exact code ran fine in basic JS.
If I make a new project and start fresh with 2.57, it won't possibly use 2.35 right? Like the docker install doesn't cache it or something? I'll try real quick
I just used the dashboard to create a test edge function and get this with your code:

I'm not really up on all the ins and outs of using the CLI. I just used a standard Supabase example with import to get the supabase-js code.

I turned on the deno lockfile, and ran
deno install --entrypoint index.ts in my edge function. Here is what it put in the lockfile:
So I tried deploying my edge function to a supabase instance online instead of local, and it worked! Not sure what's going on locally, but now that I know I can broadcast events from an edge function I am unblocked. I will update if I figure out what's up with my local configuration. ty for your help @garyaustinIt is highly likely you are not pulling in the correct supabase-js and it is using the older one.
I think so too, as I believe rest calls for broadcast events was added in 2.37. But it all looks right. 🤷
You can log out the supabase client (or at least you can on plain JS) and it will have its version scattered in the info.

Yeah, 2.57, client and server
That is your app?
I thought this was in edge functions?
Yeah in the edge function, I created supabase there and called supabase.auth.signOut() and saw the version
Sort of confused. Why are you using auth in edge functions?

And that exact edge function the send does not work?
Did you add that code to your "failing" function?

I uploaded something similar to this to my online instance and it worked fine. I do think it's something with my configuration somewhere, but idk. But honestly I'm ok as I'm unblocked now.
K.