How to notify only certain users on a page using Faye?

I have a question. I used faye to update part of the page for all clients on this page. But I am embarrassed. How to update part of the page only for certain users. In my case, I want to notify the user of a new message. But I do not know how to do this. I know how to notify all people on a particular page. For example, in my message model, I created js

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

and broadcast function

 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 

I also 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 railscast.)

How do I need to think? Thanks in advance!

+4
source share
1 answer

I found a way to solve this problem. Gem private pub (https://github.com/ryanb/private_pub) Help do this. Each user has a layout.

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

and when someone sends a 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 it works great for me.

+3
source

Source: https://habr.com/ru/post/1398128/


All Articles