[英]Real-time communication between two user-clients via REST API with PHP backend, WebSocket and Node JS
I'm working on the project where two clients must be able to communicate at real-time (or close to that) with each other in the most simple way as possible.我正在做一个项目,其中两个客户必须能够以最简单的方式彼此实时(或接近)通信。 Let's introduce the resource which has to be accessed by two separate clients.
让我们介绍必须由两个单独的客户端访问的资源。 The workflow is the following:
工作流程如下:
The client cannot act until opposing client has not acted - request order must be preserved.在对方客户未采取行动之前,客户无法采取行动 - 必须保留请求顺序。
Clients should be able to access the resource via REST API ( GET
, POST
, PUT
, DELETE
).客户端应该能够通过 REST API (
GET
, POST
, PUT
, DELETE
) 访问资源。 Each client must wait until the opposing client performs an action.每个客户端必须等到对方客户端执行操作。 Time for the client to respond and perform an action is about 1-2 seconds (can slightly differ).
客户端响应和执行操作的时间约为 1-2 秒(可能略有不同)。
Please note that system should be able to handle a high load of concurrent requests (multiple clients communicating at the same time).请注意,系统应该能够处理高负载的并发请求(多个客户端同时通信)。
The global goal of the application is to provide an API where clients programmed in multiple different languages could communicate at real-time without any polling implementation on the user-client side.
该应用程序的全球目标是提供一个 API,其中以多种不同语言编程的客户端可以实时通信,而无需在用户客户端进行任何轮询实现。 User clients must be as simple as possible.
用户客户端必须尽可能简单。
Pseudo user-client example伪用户-客户端示例
response = init();
while (response->pending) {
response = get();
}
while (response->action_required) {
response = act();
if (response->error || response->timeout) {
response = get();
}
}
function init() {
// POST resource.example.com
}
function act() {
// PUT resource.example.com
}
function get() {
// GET resource.example.com
}
The problem statement
问题陈述
Since each client must wait until opposing client to act there is a need to introduce the sleep()
function in the code which will delay the response until the resource will be affected/changed by the opposing client.由于每个客户端必须等到对方客户端采取行动,因此需要在代码中引入
sleep()
function,这将延迟响应,直到资源受到对方客户端的影响/更改。
The request polling must be omitted from the user-client and implemented in server side.请求轮询必须从用户客户端省略并在服务器端实现。
Current thoughts and proposal
目前的想法和建议
The initial thought was to implement only the PHP backend and perform response delay inside the API function, however, this implementation seems to cause severe performance issues, so I'm thinking about a more sophisticated solution.最初的想法是只实现 PHP 后端并在 API function 内部执行响应延迟,但是,这种实现似乎会导致严重的性能问题,所以我的解决方案更复杂。 Or maybe I am wrong and response delay can successfully be implemented with
sleep()
inside the PHP backend?或者也许我错了,可以在 PHP 后端使用
sleep()
成功实现响应延迟?
Proposed system architecture建议的系统架构
Please note that PHP backend cannot be replaced in this architecture, however, WebSocket and Node JS application are flexible units for the implementation.请注意 PHP 后端无法在此架构中替换,但是 WebSocket 和 Node JS 应用程序是实现的灵活单元。
Would be this kind of architecture implementable without severe server performance issues?这种架构可以在没有严重的服务器性能问题的情况下实现吗? Is there a better, more feasible way to design this kind of system?
有没有更好、更可行的方法来设计这种系统? Is Node JS application able to handle multiple concurrent requests with response delay or any other kind of web application (Python/Ruby/...) would serve better?
Node JS 应用程序是否能够处理具有响应延迟的多个并发请求,或者任何其他类型的 web 应用程序(Python/Ruby/...)会更好吗? Is socket a must-have for this system in order to achieve somewhat real-time behaviour?
为了实现某种实时行为,套接字是否是该系统的必备工具?
Please, share any ideas/insights/suggestions/... what could help to design this system in a sophisticated and well-performing manner.请分享任何想法/见解/建议/...什么可以帮助以复杂且性能良好的方式设计该系统。
Thank you in advance!先感谢您!
Some notes:一些注意事项:
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.