3

I have a simple page where I'm inserting a message into a message table and then on the frontend detecting it. I have used the sample code provided.

However, everytime, I seem to be detecting 3 events for every insert.

Any ideas?

mounted() {
    const channel = supabase.channel('messages');

    channel.on(
        'postgres_changes',
        {
            event: 'insert',
            schema: 'public',
            table: 'messages',                
        },
        (payload) => console.log('payload:', payload)
    );

    channel.subscribe(async (status) => {
        if (status === 'SUBSCRIBED') {                
        }
    });

    const res = await supabase.from('messages').insert({
                conversationSID: 'CH3165c39d3d784823af47039081fe7c47',
                messageSID: '4567',
                body: 'hi',
                source: 'SDK',
                author: 'John',
                patNum: 6,
                dateCreated: new Date()
            });
            console.log('inserted',res);
  },

enter image description here enter image description here

2 Answers 2

0

The event parameter is set to '*', which means you're receiving notifications for all types of events, like insert, update, and delete for the table messages.

Try setting the event parameter to only listen for 'INSERT' events, which could help narrow down what you're receiving notifications for.

Another thing to keep in mind is that you're inserting data into the messages table within the subscribe method, which could be causing you to receive multiple notifications for each insertion. I suggest that you move the insertion code outside of the subscribe method, so it's only executed once.

This could help streamline things to isolate what could be causing the issue.

5
  • I did what you said and moved the insert out of subscribe and also changed "*" to "insert". However, I'm still seeing duplicates. I'm attaching new image to the question above Apr 23, 2023 at 14:37
  • I attached another image with events details and you can see that it's the EXACT SAME INSERT event that I'm detecting it twice. In fact, I just had a scenario where I detected it 3 times. So it's very random. Sometimes I get 1, 2 or 3 times. Apr 23, 2023 at 14:50
  • @SaumilShah Could be the framework you're using is causing the component to be rendered randomly
    – Monica
    Apr 24, 2023 at 13:27
  • It's a simple VUE component. I also printed in my created() and mounted() function to make sure they're not getting called multiple times and that perhaps was what was causing this. However, that's not the case. They're only getting called once. Apr 25, 2023 at 14:21
  • I'm having the same issue here in React Native/Expo. I'm listening to a combination of INSERT and UPDATE, and I get 2 events of each when I update or insert a row. No idea where it could come from though
    – johnnyBoy
    May 29, 2023 at 20:55
0

I don't know which framework you're using but in my case I think I had multiple subscriptions caused by useEffect(), and adding the right dependency fixed it. I guess it now reloads the component properly. Hope it'll help !

    let channel = null;

    useEffect(() => {
        if (!channel && user) {
            channel = supabase.channel('orders-changes')
                .on(
                    'postgres_changes',
                    {
                        event: 'INSERT',
                        schema: 'public',
                        table: 'orders',
                        filter: `employee_id=eq.${user.id}`,
                    },
                    (payload: any) => handleNewOrder(payload)
                )
                .on(
                    'postgres_changes',
                    {
                        event: 'UPDATE',
                        schema: 'public',
                        table: 'orders',
                        filter: `employee_id=eq.${user.id}`,
                    },
                    (payload: any) => handleNewOrder(payload)
                )
                .subscribe();
        }
    }, [user])

    const handleNewOrder = (payload: any) => console.log(payload)
1
  • I was using Vue version 2 Jun 1, 2023 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.