4

I have a question. I was using faye, for updating part of page for all clients at this page. But I am woundering. How can I make updating part of the page only for specific users. In my case, I want to notify user about new message. But I dont know how to do this. I only know how to notify all people on certain page. For example , in my post model I have create js

<% broadcast "/posts/new" do %>
$('#posts_list').prepend('<%= escape_javascript(render(@post)) %>');
$("#posts_list  li:first").hide().fadeIn("slow");
<% end %> 

and broadcast function is

def broadcast(channel, &block)
        message = {:channel => channel, :data => capture(&block)}
        uri = URI.parse("http://localhost:9292/faye")
        Net::HTTP.post_form(uri, :message => message.to_json)
end

also i had a faye client and code

$(function() {
  var faye = new Faye.Client('http://localhost:9292/faye');
  faye.subscribe("/posts/new", function(data){
    eval(data);
  });
});

(all this was done according to the railscast.)

In what way I need to think? Thanks in advance!

Paulo Casaretto
  • 967
  • 10
  • 33
Pavel
  • 3,900
  • 6
  • 32
  • 41

1 Answers1

3

I found the way to solve this problem. Gem private pub(https://github.com/ryanb/private_pub) help to do this. Each user have in layout

<%= subscribe_to "/messages/#{current_user.id}" %>

and when somebody sends message. In action.js( in my case - create.js) I did this

    <%  publish_to "/messages/#{@message.receiver.id}" do %>
    ...some js code, in my case jquery notification plugin usage
    <% end %>

and this works great for me.

Pavel
  • 3,900
  • 6
  • 32
  • 41
  • thank a lot this helped me with my question http://stackoverflow.com/questions/27139950/private-messages-with-faye-and-rails/27142335#27142335 – Alain Goldman Nov 26 '14 at 06:01