C
C#6d ago
Lamar

✅ How to notify clients when an db update occurs

Hello! I am trying to build a Voting-ish application. What I am trying to achieve is to send notifications to the connected clients once a db entity has been updated, but I am unsure what would be the best approach. A constraint I have set is that I am trying to use SSE to communicate with the clients. For now I thought of constructing some observable. In my mind it would work similar to this: - client sends a request to update the voting count - the database updates the entity - I send a notification that a changed occurred - in the SSE endpoint, I have a listener that waits for changes and once it receives a change it sends data to the clients which will tell the clients to send another request to fetch for the new updated votes However I am unsure if this approach is good or if it even works. Do you have any suggestions on how I can improve the approach?
56 Replies
greyfox
greyfox6d ago
You could have a trigger on the database table which triggers when a new record is inserted or updated. The trigger inserts a new record into an "Updates" table. You have a service that polls that periodically and if any new records have been inserted you do your notification. Once the notification is sent, you can set a "NotificationSent" bit field to true so it isn't used again. Has the benefit that the clients don't need to know anything about the updates thing. Alternatively you can use a message broker service like RabbitMQ or the like to handle all your requests, and which will queue the notifications for you. Could also use webhooks.
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
viceroypenguin
Oh hell no. Please don’t ever do this Web hooks are the best recommendation, and that can be managed in the server code, everywhere that you manage a particular entity. Or better yet, isolate managing that entity to a single handler, which then queues a background job that does the webhook notifications.
Lamar
LamarOP6d ago
I have used EF with Change Tracker + SaveChanges. I was thinking about sending some sort of event if the SaveChanges returns != 0 I mentioned SSE because I wanted to push the changes to the clients, which in my case would be both web based with a spa, and a mobile app
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
viceroypenguin
Interceptor is fine too. That’s at least in the C#. Would still recommend going back to a Hangfire job or something to do the actual work of notifications.
Lamar
LamarOP6d ago
I will have to look up webhook notifications, I don't know much about them. I also considered a message broker, but I do not know how to consume it from client apps
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
viceroypenguin
Also, if you’re talking about spas, then you’ll have to do connection management to know who’s active and who isn’t.
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
viceroypenguin
Fair enough. Never worked with those
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
viceroypenguin
Or Hangfire
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
viceroypenguin
Channel would be better for this situation though
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
Lamar
LamarOP6d ago
If I were to use RMQ, would it be OK to push the notifications from the Update service call, and then have a background service that listens to topic/queue
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
Lamar
LamarOP6d ago
but then I am unsure what to do once the message gets picked up by this service, how to do the fanout from the service
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
Lamar
LamarOP6d ago
alright then, I can scratch that
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
Lamar
LamarOP6d ago
I don't need that much for now, it's something I plan to do in my spare time, nothing serious Was thinking about only using 1 server
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
Lamar
LamarOP6d ago
Thank you for explaining both cases. I will try to keep it as simple as possible, for my use case I think 1st aproach will work fine, for now I don't see any filtering being needed. However this assumes I do distributed APIs
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX6d ago
see $channel
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
jen
jen6d ago
ive done something similar, sse + push an update when something occurs somewhere else, i used normal .net events, the class handling the sse connection has an event listener for it and an async loop
Lamar
LamarOP6d ago
Ook, I will have to go over both channels and events. Have not used any of those before Could you please explain what you meant when you said "lookup from the event to SSE" I don't think I got that I don't think I know what lookup means in this context, to be more precise
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
jen
jen6d ago
you dont have to no its just a normal http call
Lamar
LamarOP6d ago
It's somewhat working, all I did was to create an endpoint in which there's a loop that queries the db every x seconds and I send the data to the client I can see data being pushed on the client, so I assume it's working
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
jen
jen6d ago
you dont need to have a "list" of them anywhere
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
jen
jen6d ago
yes, it keeps looping
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
jen
jen6d ago
no?
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
jen
jen6d ago
or just invert the responsibility with an event
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
jen
jen6d ago
:nso_ame_shrug: theres many ways of doing it that doesnt require storing them in a list somewhere
FusedQyou
FusedQyou6d ago
I haven't read the convo but this is something we kind of try to get working at work. Whatever you do don't use database triggers. They suck
Lamar
LamarOP6d ago
I will try to report back here with anything I find worth mentioning and/or face
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
Lamar
LamarOP6d ago
Yes, it was on the server
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
Lamar
LamarOP6d ago
Yes yes You are right, that's why I said it's "somewhat working" I will try to replace that by sending a notification. But now I wonder if the server will push to all clients or if only one client gets the data I have only tested with 1 client open Now I get what you meant when you mentioned the list of SSE connections, altough pro jenxpert said it's not needed. I will test it out to see how it works
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
Lamar
LamarOP6d ago
No nothing like that. It's just a constraint I set myself because I wanted to learn more about Server Sent Events. In my mind it made sense because I only seem to care about communicating from server to client. I said I didn't need the bidirectional connection from WebSockets. But it seems it would be easier to just use that
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
Lamar
LamarOP6d ago
Thank you all by the way! I'm a bit torn now about which approach to choose, I didn't have the chance to go over the links you sent. I'll see once I go back to working on this
Unknown User
Unknown User5d ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?