-
-
Notifications
You must be signed in to change notification settings - Fork 78
Query subscriptions #221
Description
Feature request
Is your feature request related to a problem? Please describe.
Queries and subscriptions need to be mixed for most real-time applications. However, their implementations are very different and return different results, requiring the application manually mix the two sets of data to keep them in sync.
For example, you might have a chat app that does an initial query for the latest 50 messages in the current chat room.
const { data } = supabase
.from('messages')
.select('id, name, body, created_at')
.match({ room_id: 'abc-123' })
.limit(50)
messages.value = dataBut in order to make the chat real-time, you need to set up a subscription that can only listen for inserts on the table. None of the querying operations can be used, only a rudimentary filter with different syntax. Likewise, the payload that is returned is simply the entire row with no control over which fields are sent.
supabase
.channel('public:messages')
.on(
'postgres_changes',
{
event: 'INSERT',
schema: 'public',
table: 'messages',
filter: 'room_id=eq.abc-123'
},
(payload) => {
messages.value.push(payload.new)
}
)
.subscribe()This gets even more complicated if you want to allow messages to be edited, since that requires subscribing to the UPDATE event, then manually finding the message in the local chat history and updating it.
Describe the solution you'd like
Extend the query syntax to allow subscribing to a query.
supabase
.from('messages')
.select('id, name, body, created_at')
.match({ room_id: 'abc-123' })
.subscribe((payload) => {
messages.value = payload.new
}))The idea being that unless a change to the table affects the query, don't send a payload.
It would be useful if both a full payload and a sparse payload could be chosen. Full payloads are good for things like real-time chat where you need old messages to be truncated, so replacing the entire immediate chat history can be beneficial. A sparse payload is useful for surgical changes to a local dataset, when the data is potentially too large to resend each time there is a change.
Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
Additional context
This feature request is similar to subscriptions in GraphQL which use the same syntax as normal queries. It would be really great if subscriptions could be added to the GraphQL API as well.