您可以使用React,Node JS和Mongoose创建聊天应用程序吗?是否需要socket.io?
我正在使用React和Node JS创建一个简单的聊天应用程序。当使用套接字在服务器和客户端之间建立连接时,我想知道使用此软件包的重点。由于React在状态更改时重新呈现页面,因此我可以从数据库中获取消息并更改状态,从而允许使用无缝的聊天应用程序。这样的聊天应用程序是否会有任何问题,通过从数据库中获取数据将显示聊天?添加新消息将再次更新数据库,并获取数据。因此,该页面将随着状态的变化而更新,因为它已重新渲染。那么在这样的应用程序中对套接字有什么需求?
I am creating a simple chat app using react and node js. When using socket to establish a connection between server and client, I was wondering the point of using this package. Since react re-renders the page upon state change, I can fetch messages from my database and change the state, allowing for a presumably seamless chat application. Will there be any problems with such a chat application, where the chats would be displayed by fetching data from the database? Adding a new message would again update the database and the data would be fetched. So, the page would update with the new chat being displayed as it has been re-rendered due to change of state. So what is the need for socket in such an application ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在没有socket.io的情况下构建所描述的内容。 socket.io简单地允许使用Websocket和其他一些技术实时传递消息,因此您不必长时间进行长时间的启动。
以不同的方式说,当您从数据库中获取数据时,您将进行React执行API调用,并从Node.js API端点获取新消息。此API呼叫将需要以很短的间隔进行,以使其感觉就像是实时出现的消息。
这是编程中的许多情况之一,您可以自己完全写一些东西,但是有一些工业等级的解决方案可以帮助您加快发展的速度。
You could build what you describe without socket.io. Socket.io simply allows the messages to be delivered in real-time using websockets and a few other technologies so you don't have to build long-polling.
Said in a different way, when you fetch data from the database, you'll have react perform an API call and fetch new messages from a node.js API endpoint. This API call will need to be performed on a very short interval to make it feel like messages are showing up in real time.
This is one of many situations in programming where you could totally write something yourself, but there are industrial grade solutions that will help speed up your development.