Private messages with Faye and Rails

可紊 提交于 2019-12-05 02:06:43

问题


I'm using the faye gem menitioned in the railscast allowing apps to push messages. Problem is it pushes messages to all chat clients that are open. I need them to be private. It's possible to get private messaging with faye but it's url based. For example all messages will be sent to site.com/foo. But, in my model the chat doesn't have a specific url. Because chat is just a collection of messages sent to you by that user.

So if you're logged in as adam site.com/messages/eve would allow you to talk to eve but for eve it's the reverse. site.com/messages/adam. So URL specific private chat is seemingly out of the question.

Any pointers?

messages_controller.rb ( triggering ajax )

def create
  @message = Message.new(message_params)
  @message.save
end 

create.js.erb ( calling a broadcast method )

<% broadcast params[:message][:username] do %>
  $(".text-wrap").append("<%= current_user.name %> <%= @message.body %></div>");
<% end %>

broadcast method ( posting to the faye server )

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

application.js ( faye server subscription to /messages/* )

$(function(){
    var faye = new Faye.Client('http://localhost:9292/faye');
    var subscription = faye.subscribe('/messages/*', function(message) {
        eval(message);
    });
});

回答1:


I solved it! The problem was that I had the subscribe function in application.js meaning it would run the subscribe javascript on each page. Instead what I did is at the bottom of the chat page view i subscribed to /messages/eve/adam. This is how I did that:

<script>  
  $(function(){
    var faye = new Faye.Client('http://localhost:9292/faye');
    var subscription = faye.subscribe('/messages/#{current_user.username}/#{@user.username}', function(message) {
      eval(message);
    });
  });
</script>

Then in the broadcast method I made sure to send the information to only the correct place.

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


来源:https://stackoverflow.com/questions/27139950/private-messages-with-faye-and-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!