Websocket vs SSE 在 React/Node 上实现实时好友邀请系统
我想实现一个系统,允许用户互相添加为朋友并在他们之间共享数据。我已经完成了身份验证,目前正在研究实时执行此操作的方法。我的这个项目纯粹是一次学习经历,因此我正在寻找多种方法来执行此任务以增长我的知识。
我在之前的项目中拥有使用 Websockets 的经验,并且它很容易使用。 Websockets 似乎是解决我的问题的最佳解决方案,因为它允许用户通过开放套接字发送和接收邀请。然而,我也了解到,缺点是长时间打开的套接字连接可能会导致性能负担(?)因为我仅在发送/接收邀请时才发送/接收信息,所以 websocket 可能会被过度利用以实现简单的功能。
同时,我想了解新技术,我发现服务器发送事件的性能负担会较小(?)使用 SSE 会非常有效,因为每当用户发送时,它只向客户端/服务器发送 HTTP 请求邀请。
如果我上面输入的内容有误,请纠正我,因为这是我通过在线阅读收集的内容。所以现在我很难理解 SSE 是否比 websocket 对于我的项目更好。如果还有其他技术也请告诉我!谢谢
I would like to implement a system that allows users to add each other as friends and share data between them. I have gotten the authentication done and currently researching ways to do this real time. This project of mine is purely a learning experience so I am looking for many ways to perform this task to grow my knowledge.
I have experience using Websockets on a previous project and it was easy to use. Websockets seems like the best solution to my problem as it allows the user to send and receive invites through the open socket. However I have also learnt that the downside would be a long open socket connection that might be potentially performance taxing(?) Since I'm only sending/receiving information only when an invite is sent/received, websockets might be overutilized for a simple function.
At the same time I would like to learn about new technologies and I found out about Server Sent Events that would be less performance heavy(?) Using SSE would be much efficient as it only sends HTTP requests to the clients/server whenever the user send the invite.
Please correct me if I'm wrong for what I typed out above as this is what I gathered through my reading online. So now I'm having a hard time understanding whether SSE is better than websocket for my project. If there are other technologies please do let me know too! Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要发送朋友邀请,您只需发送API请求即可。 Websocket用于实时通信。从react.js获取电子邮件并将电子邮件发送到
Node.js侧的服务器,编写一个控制器来控制此请求:
to send a friend invitation, you just send an API request. WebSocket is used for real time communication. From react.js, get the email and send the email to the server
On node.js side, write a controller to control this request:
最好的建议是在这种情况下始终使用 websocket,因为您的项目可能会增长并需要一些使用 websocket 更好的功能,
但是您还有另一种选择,其中之一是 Firebase,是的,FIREBASE!
您可以使用 firebase 制作一个很好的反应式应用程序,因为它的观察者会实时更新数据,就像 websocket 一样。
但这里有一些缺点和优点。
Websocket:可以使您的项目可扩展,更完整,您可以在任何上下文中使用它,但是:很难实现,需要更多时间来学习和理解。
Firebase,实施起来简单快捷,您可以在 20 分钟内进行聊天,并且肯定会帮助您解决问题,有 Firestore 和 Reatime 数据库..甚至 Firestore 都是实时更新的..但是:Firebase 的成本为一个大项目可能很昂贵,我认为这对于一个大项目来说不是一个好的选择。
就是这样......对我来说进行实时数据应用程序的更好选择。
多一点关于。 Firebase 与 Websocket
The best advise would be always to use websocket in this context, cuz your project can grow and need some feature that would be better using websocket
But you got another options, one of the is Firebase, Yes, FIREBASE!
You can do a nice reactive application with firebase, becouse the its observers update data in realtime, just like the websockets do.
But here go some cons and pros.
Websocket: Can make your project escalable, its more complete, you can use it in any context, BUT: is hard to implement and takes more time to be learned and understood.
Firebase, Easy and fast to implement, you can do a chat in 20 minuts, and surelly would help you with your problem, There is Firestore and Reatime database.. even the firestore updates in realtime.. BUT: Firebase costs in a big project can be expensive, i dont think is a good option for a big project.
Thats it.. the better options to do a real time data application to me.
A little bit more about. Firebase vs Websocket
https://ably.com/compare/firebase-vs-socketio