I'm using private_pub as a gem for implementing a notification system much like the one Facebook provides. For that, I am subscribing to a channel in a View and publishing from various places (different Controllers).
What's happening is that sometimes I get a lot of publishes for a single subscription. Do you have any idea about why this could happen?
My first suspicion was that I might be calling my notify method too many times (in some kind of loop or something, but I can see that it is only being called once, so I guess the problem must be somewhere in between the pub/sub layer, and most likely because of something I am doing wrong when notifying the View.
Next I present some snippets of my implementation.
In the _header.html.erb partial of my website, I subscribe to the user's notification channel, like so:
For instance in my friendships_controller, when adding a friend I generate some html to present the notification and publish the jquery with the prepended notification, like so:
html_text = render_to_string(:partial => 'notifications/notification', :locals => { notification: notification }, :formats => [:html]).squish jquery = "$('#notifications_" + user.id.to_s + "').prepend('#{html_text}');" PrivatePub.publish_to("/notifications/" + user.id.to_s, jquery)
Sorry for the long post, I hope some of you can help me. Thanks in advance.
Btw, I am running Rails 4.0.0 with Ruby 2.0.0p247.
I think we have the same problem: sometimes I get multiple notification of the same message...for example in the chat I receive multiple identical messages, but if I reload the page and the chat, the message is only one!
Well in my case the problem is that I use AJAX for loading pages (and also to load the chat) so basically I always have the same page and loading twice the chat, that is load the chat, close the chat and load it again, it results in having two subscriptions to the same channel, thus two identical messages!
At present time there isn't any 'unsubscribe' method, so the only way two avoid that is to be sure to make the subscription only once per page load...
hope to be useful