I am working on a feature where the admin creates a course and the user gets notified. I am thinking of using MongoDB change stream
but I need sockets to send the changed data to the frontend.
Now the scenario: Admin creates a course, change stream notifies and the socket.io emits that to the frontend.
My question is, if we are using socket.io to send the data to the frontend then why do we need MongoDB change stream? What we can do is simply emit the event when admin success in creating a course.
I have gone through multiple articles and they are doing the same- calling an API to create and update the document, a change stream to watch, and socket.io emit the event.
If we can send the data to the frontend once the course is created then why do we need MongoDB Change Stream?
CodePudding user response:
You server side code may want to send this event to multiple destinations and/or trigger additional processes.For security reasons you need this event to be controlled by the server side. Of course you could always rely on the client to emit an event that the db update was successful, but I would still check that on the server side. Mongodb streams would allow you to listen on those events and take action without relying on the client.