Subscription pattern for multiple conversations.
So I basically have a messenger in my app using Supabase realtime which works great!
With RLS it only listens to the "messages" relevant to that user (Where they are the recipient or sender by id.)
Currently each "conversation" has a subscription to the "messages" table that looks for inserts where the current channel id is the channel id of the new entry so that we don't refetch messages when it's not relevant to the current conversation.
I'm just wondering if this is the correct pattern as technically this only allows one subscription per user to the table at a time unless they have multiple tabs open with multiple "conversations" and the subscription technically gets destroyed when they navigate back to the list view.
I need to add logic to check new messages and if so refetch the "conversations" when on the list view as I cache the last message preview on the conversation level so I don't need to fetch the last message for every "conversation" every time they're in list view but I need to count how many new / unread messages they have so should I also cache this on the conversation by association? Should I simply increment and decrement an unread_messages column on "conversations" as a message is sent and when they open the "conversation" just set it to 0?
So should sum of all conversations unread_messages to show in the message notification bubble
With RLS it only listens to the "messages" relevant to that user (Where they are the recipient or sender by id.)
Currently each "conversation" has a subscription to the "messages" table that looks for inserts where the current channel id is the channel id of the new entry so that we don't refetch messages when it's not relevant to the current conversation.
I'm just wondering if this is the correct pattern as technically this only allows one subscription per user to the table at a time unless they have multiple tabs open with multiple "conversations" and the subscription technically gets destroyed when they navigate back to the list view.
I need to add logic to check new messages and if so refetch the "conversations" when on the list view as I cache the last message preview on the conversation level so I don't need to fetch the last message for every "conversation" every time they're in list view but I need to count how many new / unread messages they have so should I also cache this on the conversation by association? Should I simply increment and decrement an unread_messages column on "conversations" as a message is sent and when they open the "conversation" just set it to 0?
So should sum of all conversations unread_messages to show in the message notification bubble