discussing the best approach to add notifications on tables in a chat app.
So I have a channel with messages and users that I would like to implement notifications on,
what I did so far is create a last read messages table to estimate the last message the viewer saw,
from that point, what would make more sense to create notifications on the web, to run a table trigger and create a condition that when user x is sending messages and user y is receiving them, and in this case user y is not viewing the channel, we push a notification then? and when user y is viewing we turn that condition off so no notifications are being sent? does anyone have a better way to implement this?
6 Replies
Thanks for posting in <#1088161997662724167>.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.
- Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
- Use search.convex.dev to search Docs, Stack, and Discord all at once.
- Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI.
- Avoid tagging staff unless specifically instructed.
Thank you!
Would it work to create a
usersInChannels
table and keeping live track of which users are currently in which channels? schema could simply be userId: v.id("users"), channelId: v.id("channels")
and you could either insert and delete rows based on when a user enters/exits a channel, or also add isActive: v.boolean()
to the schema and just update that field (that way you would also get a history of which channels a given user has ever been in). this approach could also give you a nice way to track live the number of active users in a given channel.Yeah, I already have a
membersInChannel
table, this would work, but I need it to work for notifications nowAh okay, I thought you were considering how to keep track of activity status. Then yeah, a trigger that runs on new messages would be a simple and nice way to check and potentially dispatch a notification.
You could put that logic into your
sendMessage
mutation as well - but that pretty much amounts to the same thing since triggers are basically just wrappers around mutations as far as I've understood?You are correct, when you add a trigger, you need to wrap the mutaion with the trigger to see all the changes :))
you've given me an idea, with adding the
isActive
to the members table, I remember convex has a usePresence
hook that could help with that!
but now thinking about I would do this on client first 😅Ah yeah, I also read that stack post a while back, you remind me! https://stack.convex.dev/presence-with-convex this probably has the answers you're looking for!
Implementing Presence with Convex
Some patterns for incorporating presence into a web app leveraging Convex, and sharing some tips & utilities I built along the way.