ryanb / private_pub

Handle pub/sub messaging through private channels in Rails using Faye.
MIT License
863 stars 226 forks source link

Differentiate between sender and recipient #127

Open willc0de4food opened 8 years ago

willc0de4food commented 8 years ago

I just got private chat working with private pub, but am having trouble differentiating between the sender and recipient of a message. When the message is published to the recipient, the sender class is applied to the message (the same problem that this user is having: http://stackoverflow.com/questions/35989728/differentiate-users-with-private-pub )

Is there a solution to this problem that I missed?

Thanks

Sashkan commented 8 years ago

Hey :)

Well, I found a clean solution to this problem: Somewhere in the wrapper, add an input including your current_user id

<input type="hidden" value="<%= current_user.id %>" name="sender-id" id="senderid">

Then, in your js publish, retrieve it, and voila :)

<% publish_to whatever_you_want  do %>
sender = $('#senderid').val();
  if (sender == "<%= current_user.id %>") {
// do your stuff
  }
<% end %>