Take the 2-minute tour ×

I'm currently working on a rails app. 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).

To handle the websocket communication, the websocket-rails gem is used (https://github.com/websocket-rails/websocket-rails).

The websocket communication itself works fine. But I don't know how to trigger the websocket controller method on a model change, without breaking the mvc principles.

Is there a clean way to solve this problem?

Additional information:

Currently the daemon adds a temperature object every 20 seconds. 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


The daemon uses different external sources (I don't think details matter that much) and creates new objects:

newTemp = Measurement.new
# set values ...
share|improve this question
How is data collected from the daemon? How often? What does a temperature object look like? –  gg_s Dec 25 '13 at 17:16
The temperature is stored in a simple model (ActiveRecord). I added the detailed information to the original post. –  StateOfTheArt89 Dec 25 '13 at 17:28
add comment

2 Answers

Daemon should post new temperature objects through a controller interface, just like a regular web request. Accessing objects directly (not via controller) breaks the MVC pattern.

My suggestion is to implement trigger_value:float and triggered?:boolean attributes in Temperature.

  • (Controller, before save) Pass a trigger value along with temperature daemon data to a new Temperature object.

  • (Model, during save) Calculate whether temperature value exceeds trigger value; set triggered? boolean accordingly.

  • (Controller, after save) After create, read the triggered? boolean; publish message to client if necessary.

share|improve this answer
I dont think you understood the question. He's trying to trigger the websocket event from outside its controller. I'm with the same doubt. If you manage to make this work, let me know. –  Hamdan Jun 25 at 4:11
add comment

I think you need to use this, as docs says:


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

check it out: Documentation

share|improve this answer
add comment

Your Answer


