Note that there are some explanatory texts on larger screens.

plurals
  1. POCall Socket.io from Rail Active:Record callback or observer
    primarykey
    data
    text
    <p>The good folks at LayerVault posted a easy way to add 'real-time' features on existing Ruby application without resorting to client side MCV or rewrite the entire app in Node.js, a while back. Their post show that you can update to all clients whenever your rails <strong>Models</strong> are updated [Reference 1] through delay_job &amp; Active:Record Observer:</p> <pre><code> class FileObserver &lt; ActiveRecord::Observer observer :lv_file def after_commit(record) record.delay.report_updated end end </code></pre> <p>and </p> <pre><code> class LVFile &lt; ActiveRecord::Base def report_updated Messenger.publish_message('file_updated', "file/#{self.id}") end end </code></pre> <p>[Reference 1] <code>http://layervault.tumblr.com/post/31462727280/rails-in-realtime-part-2</code></p> <p>Connect to socket.io server from client side javascript is easy. however I'm at lost how to implement <code>Messenger.publish_message('file_updated', "file/#{self.id}")</code> on rails server side. In the post they do mentioned that </p> <blockquote> <p>The report_updated method makes a class method call to a <strong>separate Messenger class</strong> (our Socket.IO interface on the web app side) and reports that a file has changed to the appropriate room. </p> </blockquote> <p>But I'm still not very sure how to implement <code>Messager</code> class in Rails so that Active:Record Observer will be able to connect to my socket.io server and send updates. </p> <p><strong>[PS]</strong> A conversation on HN indicates that they utilized <a href="https://github.com/lyondhill/socket.io-ruby-client" rel="nofollow">this gem</a></p> <p>The gem's wiki even included a brief demo:</p> <pre><code> require 'SocketIO' client = SocketIO.connect("http://localhost", sync: true) do before_start do on_message {|message| puts message} on_disconnect {puts "I GOT A DISCONNECT"} end after_start do emit("loadLogs", "/var/www/rails_app/log/production.log") end end puts "socket still running" loop do sleep 10 puts 'zzz' end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload