javascript - Update Rails view with Faye from Database change (after_commit callback) on model -


i puzzled , first faye or pub/sub implementation please forgive me if basic questions. have not found answers anywhere else. appreciated.

how can call , update rails view page model callback (after_commit, after_save etc)? need javascript code view update run views/conferences/_show_current_participants.js.erb once after_commit callback fires.

i have setup , works have javascript execute , view update if use link_to tag return => true on view. thing not have user interaction , need push view updates page base solely on database changes (hence after_commit callback on model). not right tool/design pattern/approach?

i tried making http request on after_commit callback http://localhost:3000/conferences/conference_id/_show_current_participants_url did not trigger faye/the javascript executed on page view.

here is: _show_current_participants.js.erb

<% participant_broadcast "/conferences/#{@conference.id}" %>   alert('_show_current_participants.js.erb loaded');   $("#current_participants_js").html("<%= escape_javascript(render partial: 'conferences/show_current_participants.html', locals: { conference: @conference } ) %>"); <%end%> 

this works charm if set trigger on link_to tag remote => true:

<li><%= link_to "faye call", "#{@conference.id}/_show_current_participants", remote: true %></li> 

how can modify setup instead of having click on link faye , subsequent partial's javascript gets activated on after_commit model callback? wrong approach? how else can tell view update based on database change? appreciated. thank kind soul willing me find right direction.

versions:

faye 1.2.4 (not faye-rails)
rails 4.1.5
ruby 2.3.0
@ point more super pretty solution looking works.

you should subscribe messages javascript callback:

<script>    $(function() {        // create new client connect faye        var client = new faye.client('http://localhost:3000/conferences');         // subscribe public channel        var public_subscription = client.subscribe('/#{@conference.id}/_show_current_participants', function(data) {           $("#current_participants_js").html(data);         });    }); </script> 

my reference code taken , adjusted:

https://code.tutsplus.com/tutorials/how-to-use-faye-as-a-real-time-push-server-in-rails--net-22600


Comments