创建一个API和一个Rails应用程序之间

创建一个API和一个Rails应用程序之间

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

问题描述

我有一个视频转码API和消耗它的CMS程序。

工作流程如下:


  • CMS发送 GET 要求向API获得的列表视频文件的跨code和它得到JSON格式的答案。

  • CMS用户选择哪个文件是跨codeD和点击的跨code按钮。

  • 作为链接在用户点击时, POST 请求转到API和API创建必要的进度内部ES和响应与JSON格式的进展的初始状态。

  • 在API创建进度上课后,我让 GET AJAX请求API询问当前进程状态定期。因为这是一个要求,我展现的转码过程中用户当前状态(如百分比..)。

  • 此外,我有我应告知CMS有关当前状态的一些其他资源。

在这个时候,我在5秒内做出符合API调用Ajax一次。

这里的问题:

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

有没有一种方法来创建一个API和一个Rails应用程序之间全双工通信?

我试过和推送通知,但好像他们只是对前端的沟通?这是真的吗?

我听说过的WebSocket ,但我无法找到一个很好的资源,充分了解它。

什么是Rails社区做什么,我问了批准的方式?

感谢您


解决方案

要澄清:你需要的不仅仅是服务器的更多 - >客户沟通,因为你需要告知用户/浏览器以及其他部件在CMS有关进度

注:不知道这个API写了什么技术,所以我会认为红宝石

在此基础上可能是一个很好的解决

。两个web浏览器和CMS服务器可以是菲客户可以订阅从API更新。该API可以更新发布到一个基于正在跨$ C $光盘中的视频文件,适当命名的通道。

浏览器和LT; - > API王菲服务器之间的通信将超过WebSocket的或备用交通工具。
CMS< - > API王菲服务器通讯科可以通过相同的传输类型,也可以使用一个引擎,如Redis的


  • CMS发送GET请求的API来获取一个列表视频文件的跨code和它得到JSON格式的答案。

  • CMS用户选择哪个文件是跨codeD和点击的跨code按钮。

  • 作为链接的用户点击,一个POST请求转到API和API创建必要的内部进展和JSON格式的的进展的初始状态响应和最新进展
  • 在API创建进展后,客户端订阅更新通道。由于进步改变了API将发布更新进度的通道,客户端将收到的更新。

  • 的CMS莫名其妙地需要知道的进展过于通道。它可以通过具有它总是预订一个通道做到这一点。然后,API可以发布该通道上所有的转码信息例如视频转码。当它看到一个新的转码已经开始就可以订阅特定的视频转码更新的通道。

这可能是双向通信不是100%必要。这似乎是API需要推更新到CMS和给客户端。如果是这样的话可能是一个选项​​。如果你不希望CMS具有对API的持久连接宁愿有更新通过HTTP推到它,你可以添加的以API的支持;在发展它使一个HTTP请求到CMS通知它的进度。

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

Workflow is as follows:

  • 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.

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

Here is the question:

Is this the best way to do it all?

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

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

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

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

Thank you

解决方案

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.

Note: not sure what technology the API is written in so I'll assume Ruby

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.

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 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.

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-24 16:47