本文介绍了如何在 API 和 Rails 应用程序之间创建全双工通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视频转码 API 和一个使用它的 CMS 应用程序.

I have a video transcoding API and a CMS application which consumes it.

工作流程如下:

  • CMS 向 API 发送 GET 请求以获取要转码的列表视频文件,并以 JSON 格式获取答案.
  • CMS 用户选择要转码的文件并点击转码"按钮.按钮.
  • 当用户点击链接时,POST 请求会发送到 API,API 会在内部创建必要的 Progress 并以 JSON 格式的进度的初始状态进行响应.
  • 在 API 上创建 Progresses 后,我定期向 API 发出 GET ajax 请求,询问当前进程状态.因为我需要向用户显示转码过程的当前状态(例如百分比......).
  • 此外,我还有一些其他资源,我应该将当前状态告知 CMS.
  • CMS sends a GET request to to the API to get a of list video files to transcode and it gets the answer in JSON format.
  • CMS user selects which file to be transcoded and clicks on the "Transcode" button.
  • As user click on the link, a POST request goes to API and API creates necessary Progresses internally and responds with initial status of the progresses in JSON format.
  • After creating Progresses at the API, I make GET ajax requests to API asking about the current process status periodically. Because it's a requirement for me to show user current status of the transcoding processes (like percentage..).
  • Additionally, I have some other resources that I should inform CMS about the current status.

此时,我在 5 秒内对 API 进行一次 ajax 调用.

At this time, I'm making an ajax call to API once in 5 seconds.

这是做到这一切的最佳方式吗?

Is this the best way to do it all?

有没有办法在 API 和 Rails 应用之间创建全双工通信?

Is there a way to create full-duplex communication between an API and a Rails app?

我尝试了 pusher 和推送通知,但似乎它们仅用于前端通信?这是真的吗?

I tried pusher and push notifications but seems like they're only for front-end communication? Is this true?

我听说过 websocket 但我找不到很好的资源来完全理解它.

I had heard about websocket but I couldn't find a good resource to fully understand it.

Rails 社区批准的执行我要求的方式是什么?

What is the approved way of Rails community to do what I ask for?

谢谢

推荐答案

澄清:您需要的不仅仅是服务器 -> 客户端通信,因为您需要通知用户/浏览器以及 CMS 中的其他组件有关进度.

To clarify: you need more than just server -> client communication because you need to inform the user/browser and also other components in the CMS about progress.

注意:不确定 API 是用什么技术编写的,所以我假设是 Ruby

基于此 Faye 可能是一个非常好的解决方案.Web 浏览器和 CMS 服务器都可以是 Faye 客户端,并且可以从 API 订阅更新.API 可以将更新发布到根据正在转码的视频文件适当命名的频道.

Based on this Faye may be a very good solution. Both the web browser and the CMS server can be Faye clients and can subscribe to updates from the API. The API can publish updates to a channel appropriately named based on the video file that is being transcoded.

浏览器 API Faye 服务器通信将通过 WebSocket 或回退传输.CMS <-> API Faye 服务器通信可以使用相同的传输类型,也可以使用诸如 redis 之类的引擎.

Browser <-> API Faye server communication will be over WebSocket or fallback transport.CMS <-> API Faye server comms can be over the same transport types or they can use an Engine such as redis.

  • CMS 向 API 发送 GET 请求以获取要转码的列表视频文件,并以 JSON 格式获取答案.
  • CMS 用户选择要转码的文件并点击转码"按钮.
  • 当用户点击链接时,POST 请求会发送到 API,API 会在内部创建必要的进度,并以 JSON 格式的进度的初始状态进行响应以及进度更新的渠道.
  • 在 API 上创建进度后,客户端订阅更新频道.随着进度变化,API 会将更新进度发布到频道,客户端将收到更新.
  • CMS 以某种方式也需要知道进度通道.它可以通过拥有一个它始终订阅的频道来做到这一点.然后 API 可以发布该频道上的所有转码信息,例如视频转码.当它看到新的转码开始时,它可以订阅特定视频转码更新的频道.
  • CMS sends a GET request to to the API to get a of list video files to transcode and it gets the answer in JSON format.
  • CMS user selects which file to be transcoded and clicks on the "Transcode" button.
  • As user click on the link, a POST request goes to API and API creates necessary Progresses internally and responds with initial status of the progresses in JSON format and the channel for progress updates.
  • After creating Progresses at the API, the client subscribes to the updates channel. As progress changes the API will publish the update progress to the channel and the client will receive the updates.
  • The CMS somehow needs to know the progress channel too. It could do this by having an channel that it always subscribes to. The API can then publish all transcoding information on that channel e.g. video-transcoding. When it sees a new transcoding has started it can subscribe to the channel for specific video transcoding updates.

双向通信可能不是 100% 必要的.似乎 API 需要将更新推送到 CMS 和客户端.如果是这种情况,EventSource/Server-Sent Events 可能是选项.如果您不希望 CMS 与 API 建立持久连接,而是希望通过 HTTP 将更新推送到它,您可以添加 WebHook 支持 API;在进展时,它向 CMS 发出 HTTP 请求以通知其进展.

It may be that bi-directional communication isn't 100% necessary. It seems like the API needs to push updates to the CMS and to the client. If that's the case EventSource/Server-Sent Events may be an option. And if you don't want the CMS to have a persistent connection to the API and would rather have updates pushed to it via HTTP you could add WebHook support to the API; on progression it makes an HTTP request to the CMS to inform it of progress.

这篇关于如何在 API 和 Rails 应用程序之间创建全双工通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 14:04
查看更多