简体   繁体   English

如何在Rails中动态有效地从数据库(通知)中提取信息

[英]How to dynamically and efficiently pull information from database (notifications) in Rails

I am working in a Rails application and below is the scenario requiring a solution. 我正在使用Rails应用程序,以下是需要解决方案的方案。

I'm doing some time consuming processes in the background using Sidekiq and saves the related information in the database. 我正在使用Sidekiq在后台执行一些耗时的过程,并将相关信息保存在数据库中。 Now when each of the process gets completed, we would like to show notifications in a separate area saying that the process has been completed . 现在,当每个过程完成时,我们想在一个单独的区域中显示notifications ,指出该process has been completed

So, the notifications area really need to pull things from the back-end (This notification area will be available in every page) and show it dynamically. 因此,通知区域确实需要从后端提取内容(此通知区域将在每个页面中可用)并动态显示。 So, I thought Ajax must be an option. 因此,我认为Ajax必须是一个选择。 But, I don't know how to trigger it for a particular area only. 但是,我不知道如何仅针对特定区域触发它。 Or is there any other option by which Client can fetch dynamic content from the server efficiently without creating much traffic. 或者,客户端是否可以通过其他任何方式有效地从服务器获取动态内容而不会产生大量流量。

I know it would be a broad topic to say about. 我知道这将是一个广泛的话题。 But any relevant info would be greatly appreciated. 但任何相关信息将不胜感激。 Thanks :) 谢谢 :)

You're looking at a perpetual connection (either using SSE's or Websockets), something Rails has started to look at with ActionController::Live 您正在查看永久连接(使用SSE或Websockets),Rails已开始使用ActionController :: Live进行查看。


Live 生活

You're looking for "live" connectivity: 您正在寻找“实时”连接:

"Live" functionality works by keeping a connection open between your app and the server. “实时”功能通过保持应用程序与服务器之间的连接打开来工作。 Rails is an HTTP request-based framework, meaning it only sends responses to requests. Rails是一个基于HTTP请求的框架,这意味着它仅发送对请求的响应。 The way to send live data is to keep the response open (using a perpetual connection), which allows you to send updated data to your page on its own timescale 发送实时数据的方法是保持响应打开(使用永久连接),这使您可以按自己的时间尺度将更新的数据发送到页面

The way to do this is to use a front-end method to keep the connection "live", and a back-end stack to serve the updates. 这样做的方法是使用前端方法来保持连接“活动”,并使用后端堆栈来提供更新。 The front-end will need either SSE's or a websocket , which you'll connect with use of JS 前端将需要SSEwebsocket ,您将使用JS来连接它们

The SEE's and websockets basically give you access to the server out of the scope of "normal" requests (they use text/event-stream content / mime type) SEE和Websocket基本上使您可以访问超出“正常”请求范围的服务器(它们使用text/event-stream内容/ mime类型)


Recommendation 建议

We use a service called pusher 我们使用一种称为pusher的服务

This basically creates a third-party websocket service, to which you can push updates. 这基本上会创建一个第三方Websocket服务,您可以向其推送更新。 Once the service receives the updates, it will send it to any channels which are connected to it. 服务收到更新后,会将其发送到与其连接的任何通道。 You can split the channels it broadcasts to using the pub/sub pattern 您可以使用pub / sub模式将广播的频道拆分为

I'd recommend using this service directly (they have a Rails gem ) (I'm not affiliated with them), as well as providing a super simple API 我建议直接使用此服务(它们有Rails gem )(我不隶属于它们),以及提供一个超级简单的API

Other than that, you should look at the ActionController::Live functionality of Rails 除此之外,您应该查看Rails的ActionController :: Live功能

The answer suggested in the comment by @h0lyalg0rithm is an option to go. @ h0lyalg0rithm在注释中建议的答案是可以选择的。

However, primitive options are. 但是,原始选项是。

  1. Use setinterval in javascript to perform a task every x seconds. 使用javascript中的setinterval每隔x秒执行一次任务。 Say polling. 说投票。

  2. Use jQuery or native ajax to poll for information to a controller/action via route and have the controller push data as JSON. 使用jQuery或本机ajax通过路由将信息轮询到控制器/动作,并使控制器将数据作为JSON推送。

  3. Use document.getElementById or jQuery to update data on the page. 使用document.getElementById或jQuery更新页面上的数据。

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

相关问题 如何从关联的模型中提取信息(Rails) - How to pull information from associated model (Rails) Ruby on Rails 3如何显示数据库中的信息 - Ruby on Rails 3 how to display information from the database 如何从rails中的远程服务器数据库中提取数据? - How to pull data from remote server database in rails? Rails-如何从数据库获取信息并将其放入javascript var - Rails - how to get information from database and put into javascript var 设置默认列以从Rails中的数据库中提取 - Set default columns to pull from the database in Rails 通过Rails将信息从数据库传递到javascript - Passing information from database into javascript via rails 从视图中隐藏数据库中的信息。 -滑轨 - Hiding information in the database from the view. - Rails 从DB中提取一些配置变量以在Rails环境中动态设置? - Pull some configuration variables from the DB to set dynamically in Rails environment? 如何从Rails中的双数据库中提取数据(PostgreSQL和SQL Server) - How do I pull data from a dual database in Rails (Postgresql and SQL Server) Ruby-on-Rails:如何从数据库表的有限子集中提取最新条目 - Ruby-on-Rails: How to pull out most recent entries from a limited subset of a database table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM