简体   繁体   English

MongoDB 更改 stream 与 socket.io

[英]MongoDB change stream vs socket.io

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.我正在考虑使用MongoDB change stream但我需要 sockets 将更改后的数据发送到前端。 Now the scenario: Admin creates a course, change stream notifies and the socket.io emits that to the frontend.现在的场景:管理员创建一个课程,更改 stream 通知并且 socket.io 将其发送到前端。 My question is, if we are using socket.io to send the data to the frontend then why do we need MongoDB change stream?我的问题是,如果我们使用 socket.io 将数据发送到前端,那么为什么我们需要 MongoDB 更改 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.我浏览了多篇文章,他们也在做同样的事情——调用 API 来创建和更新文档,更改 stream 以观察,并 socket.io 发出事件。

If we can send the data to the frontend once the course is created then why do we need MongoDB Change Stream?如果我们可以在创建课程后将数据发送到前端,那么为什么我们需要 MongoDB 更改 Stream?

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. Mongodb 流将允许您在不依赖客户端的情况下侦听这些事件并采取行动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM