Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Realtime event broadcasting through REST calls does not work #774

Closed
FranciscoMessina opened this issue Jan 4, 2024 · 8 comments
Closed

Comments

@FranciscoMessina
Copy link

Describe the bug
When attempting to send a Realtime broadcast without establishing a Web Socket connection from a backend Node application, using the documented channel.send() method, it returns the string error without any explanation.

To Reproduce
Steps to reproduce the behavior:

  1. Have a supabase project running locally
  2. Attempt to send a broadcast with the following code
import { createClient } from '@supabase/supabase-js';
import { Env } from '../../config/env';


const supabaseClient = createClient(Env.SUPABASE_URL, Env.SUPABASE_SERVICE_KEY)

supabaseClient.channel('channel1').send({
   event: 'event1',
   type: 'broadcast',
   payload: {
      some: 'data'
   }
}).then(res => { console.log(res)})
  1. You should see the string "error" as a result, and any connected clients listening to that event won't be notified

Expected behavior
I would expect it to work or at least give an informative reason as to why it is not working.

Desktop (please complete the following information):

  • OS: PopOS (linux) and Windows
  • Version of CLI 1.127.4
  • Version of supabase-js 2.39.2
  • Version of Node.js (if applicable) 20.9.0
@fischjer4
Copy link

Same thing is happening to me. I think it's because the realtime tables are not being created on supabase start
I haven't figured out a solution yet. Just posting here for visibility and support of the issue.

Here are the service versions I'm using locally:
image

@w3b6x9 w3b6x9 transferred this issue from supabase/cli Jan 5, 2024
@filipecabaco
Copy link
Member

Hi

would it be possible to provide some logs from the container supabase/realtime @FranciscoMessina ?

@FranciscoMessina
Copy link
Author

Yes of course, these are the logs that appear when attempting to send the event.

2024-01-09 10:32:15 13:32:15.692 [debug] QUERY OK source="extensions" db=0.5ms idle=386.7ms
2024-01-09 10:32:15 SELECT e0."id", e0."type", e0."settings", e0."tenant_external_id", e0."inserted_at", e0."updated_at", e0."tenant_external_id" FROM "extensions" AS e0 WHERE (e0."tenant_external_id" = $1) ORDER BY e0."tenant_external_id" ["realtime-dev"]
2024-01-09 10:32:15 13:32:15.693 project=realtime-dev [info] Billing metrics: [:realtime, :connections]
2024-01-09 10:32:20 13:32:20.699 project=realtime-dev [info] Billing metrics: [:realtime, :connections]
2024-01-09 10:32:20 13:32:20.775 request_id=F6ixD-R0MPRfEjMAAAJB [info] POST /socket/api/broadcast
2024-01-09 10:32:20 13:32:20.775 request_id=F6ixD-R0MPRfEjMAAAJB [info] Sent 404 in 360µs
2024-01-09 10:32:20 13:32:20.775 request_id=F6ixD-R0MPRfEjMAAAJB [debug] Converted error Phoenix.Router.NoRouteError to 404 response
2024-01-09 10:32:25 13:32:25.702 project=realtime-dev [info] Billing metrics: [:realtime, :connections]

@filipecabaco
Copy link
Member

@w3b6x9 could it be on the client? it's weird that it has a request going to /socket/api/broadcast 👀

@Mukhammadali
Copy link

Mukhammadali commented Jan 10, 2024

@filipecabaco
I am also facing the same issue, channel.send returns error string, then I intercepted the network request and noticed that
https://<ID>.supabase.co/realtime/v1/api/broadcast is returning 401 with the following error.

image

Library version: @supabase/supabase-js: ^2.39.2
Environment: React Native (Expo)

I followed the supabase documentation, everything is working fine including realtime websocket connection. WSS is connected with the same apikey and access token and it is receiving events. No issues at all.
The only issue is with channel.send which is using REST api instead of websockets and that returns 401, I did not modify or overriding anything, just followed the documentation and when I debugged via network interceptor /api/broadcast REST api has got the same apikey and accesstoken sent as a Cookie

UPDATES:
After further debugging I noticed that accessToken was sent as an apikey to /api/broadcast endpoint but it expects only anon or service role tokens as a valid token, after manually changing apikey to anon key and re-send the same request using cURL it worked as expected

@Mukhammadali
Copy link

The issue seems to be here in this line in realtime-js package. this.socket.accessToken gives user accessToken not anon key, therefore that endpoint is returning 401

@Mukhammadali
Copy link

Mukhammadali commented Jan 10, 2024

It was reported in realtime-js repo too. I fixed the bug and raised PR there (supabase/realtime-js#273)
supabase/realtime-js#271

@w3b6x9
Copy link
Member

w3b6x9 commented Jan 11, 2024

this has been fixed in realtime-js v2.9.2. will get the new version in supabase-js soon but you can upgrade it yourself if you don't want to wait.

thanks everyone for the feedback!

@w3b6x9 w3b6x9 closed this as completed Jan 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants