Search code examples
ruby-on-railsrubyruby-on-rails-3faye

How notify only specific users on a page using Faye?


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!


Solution

  • 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.