How to queries in supabase realtime?

Viewed 58

Most of the blogs and stacks suggests below database for chat.

message_table
-id
-message
-conversationId
-sender
-receiverId

conversation_table
-id
-conversationId

Now message_table look like this.

enter image description here

So, for the chat screen I subscribe the message table.

final mySubscription = supabase
  .from('message_table')
  .on(SupabaseEventTypes.all, (payload) {
    // Handle realtime payload
  })
  .subscribe();

if user1 and user2 are chatting, they will get all messages from this table.

So, how to filter this data with specified conversationId in supabase to stop receive the other message of other users and for reduce of bandwidth ?

And Is this database viable ?

2 Answers

You can add eq filter on your realtime listeners like this:

final mySubscription = supabase
  .from('message_table:conversationId=eq.conv12')
  .on(SupabaseEventTypes.all, (payload) {
    // Handle realtime payload
  })
  .subscribe();

You can read more about this feature on the official Supabase documentation here!

Finds all rows whose column satisfies the filter.

var conversationId = yourvalue ;
final mySubscription = supabase
  .from('message_table')
  .select('message, conversationId, sender , receiverId')
  .eq('conversationId', conversationId) // Correct
  .on(SupabaseEventTypes.all, (payload) {
    // Handle realtime payload
  })
  .subscribe();

Related