简体   繁体   English

是否可以在后端使用“推送”服务?

[英]Is it possible to use 'push' services at the back-end?

I'm using pusher gem to manipulate my front-end from an external API. 我正在使用推送器 gem从外部API操纵我的前端。 It works fine, no problem with that. 它工作正常,没问题。

But the thing I wonder is if there is a possibility to use push notifications at the back-end of my application? 但我想知道的是,是否有可能在我的应用程序的后端使用推送通知? I spent a serious amount of time investigating this but couldn't find something useful. 我花了很多时间来调查这个,但找不到有用的东西。

Let me summarize: 让我总结一下:

I have an application and another API application which is tightly interacting with other. 我有一个应用程序和另一个与其他应用程序紧密交互的API应用程序。 Sometimes I want to use my API to send notification to my main application and I want to be able to manipulate data at the back-end of my main application regarding the data received from API side. 有时我想使用我的API向我的主应用程序发送通知,我希望能够在主应用程序的后端处理有关从API端接收的数据的数据。 These are things like 'an action was completed/started/succeed' etc... 这些是“行动已完成/开始/成功”等等......

I understand that 'pusher' receives push notifications by JavaScript at the front-end. 我知道'推送器'会在前端通过JavaScript接收推送通知。 But I believe that there must be a way to use those notifications at the back-end as well. 但我相信必须有一种方法可以在后端使用这些通知。

If there is another way (maybe Faye? Websocket) to do that I'd love to learn what it is. 如果有另一种方式(也许是Faye?Websocket)这样做,我很想知道它是什么。 Any clue would be appreciated. 任何线索将不胜感激。

Is it something doable? 这是可行的吗?

Thank you 谢谢

Pusher is a backend system too (to "push" updates to channels) Pusher也是一个后端系统 (用于“推送”对频道的更新)


Endpoints 端点

I think you may be interested in endpoints 我想你可能对端点很感兴趣

From what I can gather, it seems you're looking to trigger the transfer of data to an endpoint once an action occurs in your API? 从我可以收集的信息来看,一旦API中出现动作,您似乎希望触发数据传输到端点? For example: 例如:

  • User signs up on "API" app 用户注册“API”应用
  • API app sends "notification" to main app API应用程序向主应用程序发送“通知”
  • Main app increases user count by 1 主应用程序将用户数增加1

The way I can see this working is by either using ajax , or sending a curl request to your main app's endpoint (set in routes), triggering the action: 我可以看到这个工作的方式是使用ajax ,或者向主应用程序的端点发送curl请求(在路由中设置),触发操作:

#main_app/config/routes.rb
post "endpoint", to: "application#endpoint"

#main_app/controllers/application_controller.rb
def endpoint
    @count = Option.increment!(:user_count)
end

This will allow you to manipulate your data in the backend of your "main" app 这将允许您在“主”应用程序的后端操纵您的数据


API API

The tricky, non-conventional part comes when you want to send the data from your API app to your Main app (this is where you got the "pusher" idea from) 当您想要将API应用程序中的数据发送到主应用程序时,就会出现棘手的非传统部分(这是您从中获得“推动”想法的地方)

I would personally look at sending a standard HTTP request to the Main app endpoint, probably with Curl (if from the backend): 我个人会看到向主应用程序端点发送标准HTTP请求,可能是使用Curl(如果来自后端):

You may want to install curb (CUrl RuBy) here: https://github.com/taf2/curb 你可能想在这里安装curb (CUrl RuBy): https//github.com/taf2/curb

I could write some code if you wanted? 如果你愿意,我可以写一些代码吗?

I had asked the same question to the Pusher's support team and I got the exact answer I was looking for. 我向Pusher的支持团队提出了同样的问题,我得到了我正在寻找的确切答案。

You can install a client library on your server ( http://pusher.com/docs/client_libraries ) if there is one for your server. 如果服务器上有客户端库,则可以在服务器上安装客户端库( http://pusher.com/docs/client_libraries )。 You can then subscribe to a client channel this way. 然后,您可以通过这种方式订阅客户端频道。

In my case, I use Ruby gem which can be reached from https://github.com/pusher/pusher-ruby-client . 在我的例子中,我使用Ruby gem,可以从https://github.com/pusher/pusher-ruby-client访问

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

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