✅ 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
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•6d ago
Message Not Public
Sign In & Join Server To View
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.
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•6d ago
Message Not Public
Sign In & Join Server To View
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.
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•6d ago
Message Not Public
Sign In & Join Server To View
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•6d ago
Message Not Public
Sign In & Join Server To View
Fair enough. Never worked with those
Unknown User•6d ago
Message Not Public
Sign In & Join Server To View
Or Hangfire
Unknown User•6d ago
Message Not Public
Sign In & Join Server To View
Channel would be better for this situation though
Unknown User•6d ago
Message Not Public
Sign In & Join Server To View
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•6d ago
Message Not Public
Sign In & Join Server To View
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•6d ago
Message Not Public
Sign In & Join Server To View
alright then, I can scratch that
Unknown User•6d ago
Message Not Public
Sign In & Join Server To View
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•6d ago
Message Not Public
Sign In & Join Server To View
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•6d ago
Message Not Public
Sign In & Join Server To View
see
$channel
Unknown User•6d ago
Message Not Public
Sign In & Join Server To View
Opinion: first article looks better as an introduction than the devblog one, but but should still be read in that order
https://github.com/tkp1n/ndportmann.com/blob/master/posts/2019-01-03--system-threading-channels/index.md
https://web.archive.org/web/20221129052633/http://ndportmann.com/system-threading-channels/
https://devblogs.microsoft.com/dotnet/an-introduction-to-system-threading-channels
https://www.stevejgordon.co.uk/an-introduction-to-system-threading-channels
Video:
https://docs.microsoft.com/en-us/shows/on-net/working-with-channels-in-net
Unknown User•6d ago
Message Not Public
Sign In & Join Server To View
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
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•6d ago
Message Not Public
Sign In & Join Server To View
you dont have to no
its just a normal http call
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•6d ago
Message Not Public
Sign In & Join Server To View
you dont need to have a "list" of them anywhere
Unknown User•6d ago
Message Not Public
Sign In & Join Server To View
yes, it keeps looping
Unknown User•6d ago
Message Not Public
Sign In & Join Server To View
no?
Unknown User•6d ago
Message Not Public
Sign In & Join Server To View
or just invert the responsibility
with an event
Unknown User•6d ago
Message Not Public
Sign In & Join Server To View
:nso_ame_shrug:
theres many ways of doing it that doesnt require storing them in a list somewhere
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
I will try to report back here with anything I find worth mentioning and/or face
Unknown User•6d ago
Message Not Public
Sign In & Join Server To View
Yes, it was on the server
Unknown User•6d ago
Message Not Public
Sign In & Join Server To View
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•6d ago
Message Not Public
Sign In & Join Server To View
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•6d ago
Message Not Public
Sign In & Join Server To View
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•5d ago
Message Not Public
Sign In & Join Server To View