Realtime on Cloudflare Workers
# help-and-questions
h
Has anyone use realtime broadcast channels on Cloudflare Workers? I'm getting the error
Error: WebSocket Constructor: The protocols array cannot be empty.
when calling
client.subscribe
. I assume it has something to do with the Cloudflare runtime environment not being a full Node environment.
Ah, turns out the CF WebSocket implementation doesn't like any empty protocols array in the constructor, which is what the @supabase/realtime-js library passes. Worked around by creating a proxy subclass:
Copy code
class WebSocketProxy extends WebSocket {
  public constructor(url: string, protocols?: string | string[]) {
    super(url, protocols?.length ? protocols : undefined);
  }
}
...
  const client = new RealtimeClient(env.REALTIME_URL, {
    params: {
      apikey: env.SUPABASE_KEY,
      eventsPerSecond: 10,
    },
    // @ts-ignore
    transport: WebSocketProxy,
  });
While I'm here, two other questions I still need to figure out: 1. How do you start the local websocket server? 2. Should I be using
@supabase/realtime-js
or
@supabase/supabase-j
? Both seem to implement similar functionality.
g
supabase-js will handle the token refreshes for you. realtime-js you will have to keep updating the token if it gets refreshed... If I recall correctly. Assuming you are using a user jwt. Probably does not matter which way other wise.
h
Thanks! Seems like the only way to run the local server is to clone https://github.com/supabase/realtime and
docker compose up
. My next confusion is which DB to use -- the one defined in that repo, or connecting it to my existing local DB? The former doesn't seem to be working (getting a failure due to missing
postgres
role) and for the latter, I'm unsure of the correct values for
API_JWT_SECRET
and
SECRET_KEY_BASE
.
I might skip local dev for now and simply test in production. 🤷‍♂️
g
You should start a new question on local realtime or search further in this forum. It comes up weekly. I don't do local dev and your title will not attract users who do.