supabase-js: WebSocket connection failed on subscribing to realtime channel

Bug report

Describe the bug

I’m trying to use Supabase’s realtime updates locally, and I’m getting the following error

WebSocket connection to 'ws://localhost:54321/realtime/v1/websocket?apikey=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0&vsn=1.0.0' failed

To Reproduce

  1. Clone this repo https://github.com/skulltech/sample-realtime-app
  2. Run yarn install and supabase start, and then yarn dev to start the server.
  3. Go to http://localhost:3000 and check the console to see the above mentioned error.
  4. The webapp has some basic functionality for writing and reading to database to make sure that the other Supabase functionalities are working as expected.

Expected behavior

No errors while subscribing to the realtime channel.

Screenshots

Screenshot 2023-01-08 at 1 13 15 AM

System information

  • OS: macOS
  • Browser: Chrome
  • Version of supabase-js: 2.2.3
  • Version of Node.js: v19.4.0
  • Version of Supabase CLI: 1.29.2
  • Version of Docker desktop for Mac: 4.15.0

Additional context

The same code seems to be working on production Supabase at app.supabase.com.

The same code was working before I updated my whole stack, i.e. updated Supabase CLI as well as @supabase/supabase-js, moved to Postgres v15 locally (i.e. in config.toml), from v14 previously, and ran brew upgrade which included updating docker desktop.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 16
  • Comments: 42 (1 by maintainers)

Most upvoted comments

I am having the same issue.

Fixed in "@supabase/supabase-js": "2.4.0" @skulltech @aerophobic

I continue to have this issue after running brew upgrade supabase followed by supabase stop and supabase start.

Testing with:

this.supabase.client
      .channel('table-db-changes')
      .on(
        'postgres_changes',
        {
          event: '*',
          schema: 'public',
          table: 'my_table',
        },
        (payload) => {
          console.log('payload', payload);
        }
      )
      .subscribe()

@atanaskanchev I am having the same issues and I am using “@supabase/supabase-js”: “2.4.0”.

If you have it behind nginx, make sure to enable upstream websocket rule in configuration also the headers

                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection $connection_upgrade;

This fixed for me.

Was also experiencing the same issue and restarted supabase a dozen times. What finally fixed it was clearing out all the local/session storage and cookies from the browser.

For anyone having the same issue, what @DavidMelnychuk mentioned seems to solve the problem. At least it did for me.

I am running 2 separate supabase local instances (each with its own ports and project_id. Every time I switch to a project (to work on development), I need to remove the cache, and cookies from the browser, as it uses the cookies from the last used supabase instance.

Hope it helps!

Try changing SUPABASE_URL from localhost:54321 to 127.0.0.1:54321

^ This is the only thing that solved it for me locally

I had the wrong anon key, updating the key found by running npx supabase status fixed the issue for me

If you have it behind nginx, make sure to enable upstream websocket rule in configuration also the headers

                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection $connection_upgrade;

This fixed for me.

@adityadhawan22 suggestion worked for me! I already lost 4 hours on this and almost gave up. Thank you!

@fbeutel how did you re-generate the JWT secret? Experiencing the same issue

We had the same issue, but it seemed like re-generating the JWT secret (and therefore changing the API keys) solved the problem…

Also have this issue:

supabase/postgres │ 15.1.0.117 │ - supabase/gotrue │ v2.99.0 │ - postgrest/postgrest │ v11.2.0 │ - supabase/realtime │ v2.10.1 │ - supabase/storage-api │ v0.40.4 │ - supabase/edge-runtime │ v1.20.2 │ - supabase/studio │ 20230921-d657f29 │ - supabase/postgres-meta │ v0.68.0 │ - supabase/logflare │ 1.4.0 │ - bitnami/pgbouncer │ 1.20.1-debian-11-r39 │ - darthsim/imgproxy │ v3.8.0 │ -

Error:

` WebSocket connection to 'ws://localhost:54321/realtime/v1/websocket?apikey={apiKey}&vsn=1.0.0: failed

` Code:

` const entriesChannel = supabase .channel(‘custom-all-channel’) .on(‘postgres_changes’, { event: ‘*’, schema: ‘public’, table: ‘entries’ }, (payload) => { console.log(‘Change received!’, payload); }) .subscribe();

`

dependencies:

"@supabase/auth-helpers-sveltekit": "^0.10.3", "@supabase/auth-ui-shared": "^0.1.8", "@supabase/auth-ui-svelte": "^0.2.7", "@supabase/supabase-js": "^2.38.1",

Supabase CLI: 1.106.1