简体   繁体   English

两个用户客户端之间通过 REST API 与 PHP 后端、Z45EDC1B1和Node3507D9D2A03DD 进行实时通信

[英]Real-time communication between two user-clients via REST API with PHP backend, WebSocket and Node JS

Asking for architectural advice on how to design the system with public API for user clients communication.寻求有关如何设计具有公共 API 的系统的架构建议,以供用户客户通信。

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:工作流程如下:

  1. Client #1 connects to the server and creates the resource客户端 #1 连接到服务器并创建资源
  2. Client #2 connects to the server and accesses the resource客户端 #2 连接到服务器并访问资源
  3. Client #1 changes the resource客户端 #1 更改资源
  4. Client #2 changes the resource客户端 #2 更改资源
  5. Repeat steps 3 and 4 until done.重复步骤34 ,直到完成。

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建议的系统架构

  • Node WebSocket server (socket.io to receive/return events)节点 WebSocket 服务器(socket.io 接收/返回事件)
  • PHP backend with REST API (access/change the resource , fire events to WebSocket) PHP 后端与 REST API(访问/更改资源,将事件触发到 WebSocket)
  • Node JS application with public API for the end-user client (response delay functionality until the event received)用于最终用户客户端的具有公共 API 的节点 JS 应用程序(响应延迟功能,直到收到事件)

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:一些注意事项:

  1. Avoid Sleep at all costs.不惜一切代价避免睡眠。
  2. Your use case tends to lend itself to a pub/sub micro-services pattern.您的用例倾向于使用 pub/sub 微服务模式。
  3. As you need to preserve message processing order you need to have a common queue.由于您需要保留消息处理顺序,因此您需要有一个公共队列。 Each of your REST API nodes act as a pub/sub publisher onto a distributed message queue system (RabbitMQ, Kafka, etc. type of tech).您的每个 REST API 节点都充当分布式消息队列系统(RabbitMQ、Kafka 等技术类型)的发布/订阅发布者。 So for high throughput you now have a farm of machines handing the enqueue.因此,对于高吞吐量,您现在有一个处理队列的机器农场。 They return immediately with a 201 Accepted, but need a way to mark the message with some kind of client identifier so you can route update messages back over web socket (if you aren't going to poll for status updates by resource id).它们立即返回 201 Accepted,但需要一种方法来使用某种客户端标识符标记消息,以便您可以通过 web 套接字将更新消息路由回(如果您不打算按资源 id 轮询状态更新)。
  4. You need subscribers to this queue to do the actual processing.您需要此队列的订阅者来执行实际处理。 Same thing, have these as separate applications and now you can scale out the dequeue and processing.同样的,将它们作为单独的应用程序,现在您可以扩展出队和处理。 However, the tech you choose for the pub/sub bus needs to be able to invalidate subsequent messages for that resource, and for each one of the invalidated messages provide feedback to your application so that it can send the required message over web socket.但是,您为 pub/sub 总线选择的技术需要能够使该资源的后续消息无效,并且对于每个无效消息都向您的应用程序提供反馈,以便它可以通过 web 套接字发送所需的消息。

Hope this helps.希望这可以帮助。

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

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