cli icon indicating copy to clipboard operation
cli copied to clipboard

Realtime tables not created when I run supabase start causing realtime not to work when hosting locally

Open thorhelgason opened this issue 2 years ago • 6 comments

Describe the bug Hi I am trying to use realtime when running supabase locally in a docker container and the client in not able to create a websocket to the server. I believe that it is caused by the missing realtime tables but I am not sure.

I have read numerous issues that have been posted to this and other supabase repositories and they are not providing me with fixes that work and some state that it should be fixed in the latest versions. However, I have updated the supabase cli and the supabase-js client to the latest versions and it is still not working. For example: https://github.com/supabase/realtime/issues/415.

As you can see the realtime schema is completely empty. Screenshot 2024-01-16 at 13 49 00

This is the error i am getting in the browser GET ws://localhost:54321/realtime/v1/websocket?apikey=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InZyenZjdmV4ZHl6bWltd3JraGdtIiwicm9sZSI6ImFub24iLCJpYXQiOjE2OTk5NzYwOTQsImV4cCI6MjAxNTU1MjA5NH0.jKrBDAij62rjCkyv2k5Uma-9lvb5Box6d_oDE3gnvSo&vsn=1.0.0 -> 400 Bad Request

On Firefox. Screenshot 2024-01-16 at 13 51 44

On Chrome Screenshot 2024-01-16 at 13 54 30

On Safari

Screenshot 2024-01-16 at 13 55 36

Screenshot 2024-01-16 at 13 55 58

Screenshot 2024-01-16 at 14 27 39

If I look at the logs from realtime-dev.supabase_realtime_supabase I am getting the following errors.

2024-01-16 14:27:49 14:27:49.919 project=realtime-dev external_id=realtime-dev [error] Auth error: {:error, :signature_error}
2024-01-16 14:27:51 14:27:51.129 project=realtime-dev external_id=realtime-dev [error] Auth error: {:error, :signature_error}
2024-01-16 14:27:52 14:27:52.323 [warning] Replica region not found, defaulting to Realtime.Repo
2024-01-16 14:27:52 14:27:52.332 [debug] QUERY OK source="tenants" db=6.9ms queue=0.2ms idle=1922.2ms
2024-01-16 14:27:52 SELECT t0."id", t0."name", t0."external_id", t0."jwt_secret", t0."postgres_cdc_default", t0."max_concurrent_users", t0."max_events_per_second", t0."max_bytes_per_second", t0."max_channels_per_client", t0."max_joins_per_second", t0."suspend", t0."inserted_at", t0."updated_at" FROM "tenants" AS t0 WHERE (t0."external_id" = $1) ["realtime-dev"]
2024-01-16 14:27:52 14:27:52.336 [debug] QUERY OK source="extensions" db=3.4ms idle=929.6ms
2024-01-16 14:27:52 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-16 14:27:52 14:27:52.337 project=realtime-dev external_id=realtime-dev [error] Auth error: {:error, :signature_error}
2024-01-16 14:27:53 14:27:53.505 project=realtime-dev external_id=realtime-dev [error] Auth error: {:error, :signature_error}
2024-01-16 14:27:54 14:27:54.154 [warning] Using execute/3 with a single event value is deprecated. Use a measurement map instead.

Do you know how I can fix this or is this an error with how the local supabase works?

I have turned realtime on, on the tables being used in the realtime. Screenshot 2024-01-16 at 14 23 37

To Reproduce Steps to reproduce the behavior:

  1. Run supabase init (default project)
  2. Run supabase start
  3. Check the local dashboard through http://localhost:54323/project/default/editor and see that the realtime tables are non existant.
  4. Try to connect realtime from a supabase client exactly the same way as is done in a hosted supabase, but realize that it is not working even though it works perfectly fine when connecting to a hosted supabase instance. The code I am using client side:
const subscribeToChannel = () => {
      return supabase
        .channel(`realtime for chat ${chatID} on table message`)
        .on(
          "postgres_changes",
          {
            event: "*",
            schema: "public",
            table: "message",
            filter: `chat_id=eq.${chatID}`,
          },
          (payload) => {
            if (payload.eventType === "INSERT") {
              const newMsg = payload.new as Message["Row"];

              // Updating chat members if this is a new chat member
              setChatMembers((prev) => {
                if (prev == null) {
                  return prev;
                }

                if (prev[newMsg.user_id] === undefined) {
                  return {
                    ...prev,
                    [newMsg.user_id]: null,
                  };
                }

                return prev;
              });

              setMessages((prev) => {
                if (prev == null) {
                  return [newMsg];
                }

                return [...prev, newMsg];
              });
            } else if (payload.eventType === "UPDATE") {
              const updatedMsg = payload.new as Message["Row"];

              setMessages((prev) => {
                if (prev == null) {
                  return [updatedMsg];
                }

                return prev?.map((msg) => {
                  if (msg.message_id === updatedMsg.message_id) {
                    return updatedMsg;
                  } else {
                    return msg;
                  }
                });
              });
            } else if (payload.eventType === "DELETE") {
              const deletedMsg = payload.old as Message["Row"];

              setMessages((prev) => {
                if (prev == null) {
                  return [];
                }

                return prev?.filter(
                  (msg) => msg.message_id !== deletedMsg.message_id
                );
              });
            }
          }
        )
        .subscribe((status, err) => {
          console.log("status", status, err);
          if (status === "CHANNEL_ERROR") {
            if (channel != undefined) supabase.removeChannel(channel);

            fetchData();
            channel = subscribeToChannel();
          }

          console.log(
            "Message realtime channel status: ",
            status,
            new Date().toISOString()
          );
        });
    };

Expected behavior When you run supabase start and the docker images are being set up the realtime tables should be created and realtime should work.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: MacOS - Sonoma 14.2.1
  • Browser: Chrome, Safari, Firefox.
  • Version of CLI: 1.131.4
  • Version of supabase-js: 2.39.3
  • Version of Node.js: v21.2.0

Additional context

thorhelgason avatar Jan 16 '24 14:01 thorhelgason

This is occurring for me as well on macOS. No tables are created, and after 10 seconds, I get a TIMED_OUT status message from the subscription callback. Tried everything, both downgrading and upgrading.

soulofmischief avatar Jan 21 '24 13:01 soulofmischief

@soulofmischief can you post your cookies? I sometimes find some weird cookies that are supposed to look like sb-127-auth-cookie but end up with weird suffixes like sb-127-auth-cookie.0. If I delete all the .0 ones, my subscriptions start working again.

However, I can confirm that I am experiencing this as well.

barrownicholas avatar Jan 21 '24 18:01 barrownicholas

Could you run supabase services and share here the versions output? It helps the team to reproduce this error.

sweatybridge avatar Jan 26 '24 06:01 sweatybridge

Here you go :)

        SERVICE IMAGE      │        LOCAL         │  LINKED
  ─────────────────────────┼──────────────────────┼───────────
    supabase/postgres      │ 15.1.1.3             │ 15.1.1.3
    supabase/gotrue        │ v2.132.3             │ v2.132.3
    postgrest/postgrest    │ v12.0.2              │ v12.0.2
    supabase/realtime      │ v2.25.50             │ -
    supabase/storage-api   │ v0.43.11             │ v0.46.4
    supabase/edge-runtime  │ v1.29.1              │ -
    supabase/studio        │ 20240101-8e4a094     │ -
    supabase/postgres-meta │ v0.75.0              │ -
    supabase/logflare      │ 1.4.0                │ -
    bitnami/pgbouncer      │ 1.20.1-debian-11-r39 │ -
    darthsim/imgproxy      │ v3.8.0               │ -

Run supabase link to sync your local image versions with the linked project.

thorhelgason avatar Jan 26 '24 14:01 thorhelgason

I have the same error locally, cleaned up all images from docker and did a clean supabase start but I get still these errors.

arthurliebhardt avatar Jan 29 '24 15:01 arthurliebhardt

I have resolved the issue. The problem was on my site, sorry. After testing a lot, I realized that I had the wrong SUPABASE_SERVICE_ROLE_KEY and NEXT_PUBLIC_SUPABASE_ANON_KEY, which, of course, led to the error.

Before I upgraded supabase cli, deleted everything in docker, did a fresh supabase init and saw that there where some new things in the config.toml. Started again everything with supabase start and it works :)

Working for me now.

arthurliebhardt avatar Jan 30 '24 22:01 arthurliebhardt

It is fixed now, I ran supabase status and copied the anon key again to my env variable and now realtime is working like expected.

thorhelgason avatar Mar 21 '24 11:03 thorhelgason