简体   繁体   English

ReactJS消息服务

[英]ReactJS Messaging Service

Background: 背景:

I am building a reactJS application using AWS cognito, dynamo and S3. 我正在使用AWS cognito,dynamo和S3构建reactJS应用程序。 The application is based in the recruitment sector where employers and employees can post and view jobs respectively. 该应用程序基于招聘部门,雇主和雇员可以在该部门分别发布和查看职位。 When an employee applies for a job the employer can view the employees profile and decided whether or not to message them. 员工申请工作时,雇主可以查看员工资料并决定是否向其发送消息。 Employees and employers converse via an on-site messaging service. 员工和雇主通过现场消息服务进行交谈。



The Question: 问题:

What is the best method to facilitate user chat? 促进用户聊天的最佳方法是什么?

ie what is a nice & efficient way to store messages and notify users when they have a new message. 即,什么是存储消息并在用户收到新消息时通知用户的好方法。

Our current approach is to have a setTimeout() on the site and check for new messages but this will be very inefficient so i'm looking for some guidance. 我们当前的方法是在网站上设置setTimeout()并检查是否有新消息,但这效率很低,因此我正在寻找一些指导。

I would like to stay inside the amazon infrastructure as much as possible but I am open to all suggestions. 我想尽可能地留在亚马逊基础设施中,但是我愿意接受所有建议。

I'm currently building something similar for a startup I'm working at. 我目前正在为正在工作的初创公司构建类似的东西。 Our React app is served by node.js server, while the API backend is provided by a django API with drf. 我们的React应用程序由node.js服务器提供服务,而API后端则由带有drf的django API提供。 As in your user chat case, we need to handle some real time data arriving in the frontend. 与您的用户聊天情况一样,我们需要处理一些到达前端的实时数据。

Our approach 我们的方法

The solution may be split up into inter server and server-browser realtime communication: 该解决方案可以分为服务器间和服务器浏览器实时通信:

We use redis (aws elasticache to be exact) as a publish/ subscribe message queue to push incoming data from the API backend to the nodejs server. 我们使用redis(确切地说是aws elasticache)作为发布/订阅消息队列,将传入的数据从API后端推送到nodejs服务器。 Specifically, whenever an instance of the model in question is created due to an HTTP POST call (ie in your case a message, which is send to the server), we publish JSON serialized information on a channel specific to the actors of concern. 具体而言,无论何时由于HTTP POST调用而创建了所讨论模型的实例(即,在您的情况下,该消息已发送到服务器),我们都会在特定于关注参与者的通道上发布JSON序列化信息。

On the node.js servers, we subscribe to channels of interest and receive information from the backend in real-time. 在node.js服务器上,我们订阅感兴趣的频道并实时从后端接收信息。 We then use socket.io to provide a websocket connection to the frontend, which may be easily integrated with React. 然后,我们使用socket.io提供到前端的websocket连接,该前端可以轻松地与React集成。

Limitations of this approach 这种方法的局限性

You cannot simply server your React app as a static website from S3 and have to rely on a node x React approach. 您不能简单地将自己的React应用程序作为S3的静态网站进行服务器,而必须依靠Node x React方法。 react-boilerplat (by Max Stoiber I think) is a great way to start. react-boilerplat (我认为是Max Stoiber)是一个很好的开始。

What's more, you can also use websockets end to end. 此外,您还可以端对端使用websocket。 We use this approach as our data source isn't a browser but a constrained device. 我们使用这种方法,因为我们的数据源不是浏览器,而是受限制的设备。

Hope that helps! 希望有帮助!

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

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