简体   繁体   English

Rails vs. Websockets:在模型更改时触发控制器方法

[英]Rails vs. websockets: Triggering a controller method on model changes

I'm currently working on a rails app. 我目前正在使用Rails应用程序。 The app takes temperature data via a daemon (running in the background). 该应用程序通过守护程序获取温度数据(在后台运行)。 Every time a new temperature object is saved and the temperature exceeds a certain level, I want to display the new data to the user (asynchronous via websockets). 每次保存新的温度对象并且温度超过一定水平时,我都想向用户显示新数据(通过websocket异步)。

To handle the websocket communication, the websocket-rails gem is used ( https://github.com/websocket-rails/websocket-rails ). 为了处理websocket通信,使用了websocket-rails gem( https://github.com/websocket-rails/websocket-rails )。

The websocket communication itself works fine. Websocket通信本身运行良好。 But I don't know how to trigger the websocket controller method on a model change, without breaking the mvc principles. 但是我不知道如何在不改变mvc原理的情况下触发模型更改的websocket控制器方法。

Is there a clean way to solve this problem? 有没有解决此问题的干净方法?

Additional information: 附加信息:

Currently the daemon adds a temperature object every 20 seconds. 当前,守护程序每20秒添加一个温度对象。 But this value depends on settings and the count of measurement points. 但是该值取决于设置和测量点的数量。

temperature model: 温度模型:

# == Schema Information
#
# Table name: measurements
#
#  id          :integer          not null, primary key
#  value       :float
#  measured_at :datetime
#  created_at  :datetime
#  updated_at  :datetime
#

class Measurement < ActiveRecord::Base

end

The daemon uses different external sources (I don't think details matter that much) and creates new objects: 守护程序使用不同的外部源(我认为细节并不那么重要)并创建新对象:

newTemp = Measurement.new
# set values ...
newTemp.save

I think you need to use this, as docs says: 我认为您需要使用它,如文档所述:

Broadcast to the channel from anywhere inside your Rails application. 从Rails应用程序内的任何地方广播到频道。 An existing controller, a model, a background job, or a new WebsocketRails controller. 现有的控制器,模型,后台作业或新的WebsocketRails控制器。

latest_post = Post.latest
WebsocketRails[:posts].trigger 'new', latest_post

check it out: Documentation 签出: 文档

Daemon should post new temperature objects through a controller interface, just like a regular web request. 守护程序应该通过控制器界面发布新的温度对象,就像常规的Web请求一样。 Accessing objects directly (not via controller) breaks the MVC pattern. 直接访问对象(而不是通过控制器)会破坏MVC模式。

My suggestion is to implement trigger_value:float and triggered?:boolean attributes in Temperature . 我的建议是在Temperature实现trigger_value:floattriggered?:boolean属性。

  • (Controller, before save) Pass a trigger value along with temperature daemon data to a new Temperature object. (控制器,保存之前)将触发值以及温度守护程序数据传递到新的Temperature对象。

  • (Model, during save) Calculate whether temperature value exceeds trigger value; (模型,保存过程中)计算温度值是否超过触发值; set triggered? 设置triggered? boolean accordingly. 布尔值。

  • (Controller, after save) After create, read the triggered? (控制器,保存后)创建后,读取已triggered? boolean; 布尔值 publish message to client if necessary. 如有必要,将消息发布给客户端。

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

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