https://supabase.com/ logo
#off-topic
Title
# off-topic
k

kingpeppe

04/09/2022, 10:24 PM
Does anyone else experience this when using realtime in svelte?
s

silentworks

04/09/2022, 10:35 PM
Please provide context as to what you have tried that resulted in this error, its hard for someone to help without any knowledge of what you are trying.
k

kingpeppe

04/10/2022, 4:12 AM
lib/supabase.js
Copy code
js
import { createClient } from '@supabase/supabase-js';

const supabaseUrl = import.meta.env.VITE_PUBLIC_SUPABASE_URL;
const supabaseKey = import.meta.env.VITE_PUBLIC_SUPABASE_KEY;

export const supabase = createClient(supabaseUrl, supabaseKey);
data is then fed into tokens and partners stores like so lib/stores/tokens.js
Copy code
js
import { readable, get } from 'svelte/store';
import { supabase } from '$lib/supabase.js';

let initiated = false;
export const tokens = readable(null, (set) => {
    if (!initiated) {
        supabase
            .from('tokens')
            .select('*')
            .then(({ error, data }) => set(data));
        initiated = true;
    }

    const subscription = supabase
        .from('tokens')
        .on('*', (payload) => {
            if (payload.eventType === 'INSERT') {
                set([...get(tokens), payload.new]);
            }
            if (payload.eventType === 'DELETE') {
                set([...get(tokens)].filter((token) => token.id != payload.old.id));
            }
            if (payload.eventType === 'UPDATE') {
                const index = [...get(tokens)].findIndex((token) => token.id === payload.new.id);
                let original = [...get(tokens)];
                original[index] = payload.new;

                set(original);
                console.log('UPDATE TRIGGERED AND STORE SET');
            }
        })
        .subscribe();

    return () => supabase.removeSubscription(subscription);
});
to be used by pages and components
So i've narrowed down the cause of errors with supabase realtime.
Firefox can’t establish a connection to the server at wss://qwvcbjrghetgbxqowrqc.supabase.co/realtime/v1/websocket?blahblahblah
The connection to wss://qwvcbjrghetgbxqowrqc.supabase.co/realtime/v1/websocket?blahblahblah was interrupted while the page was loading.
It looks like these errors start to appear as soon as I enable the context module script in my __layout.svelte file. Its purpose is to not show the page until the data has loaded.
Copy code
js
<script context="module">
  import { tokens } from '$lib/stores/tokens';

  export async function load() {
    const data = await new Promise((resolve, reject) => {
    let unsubscribe = () => {};
      unsubscribe = tokens.subscribe((data) => {
        if (!data) return;
        unsubscribe();
        resolve(data);
       });
      });
    return {};
  }
</script>
Any idea how to resolve this issue?