ryanb / private_pub

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

Multiple Publishes to view with single Subscription #84

Closed dnlserrano closed 11 years ago

dnlserrano commented 11 years ago

Hi, 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:

<%= subscribe_to '/notifications/' + @user.id.to_s %>

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_tostring(: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.

gregmolnar commented 11 years ago

Most likely you call that publish_to multiple times. What do you see in your log file? Is it called multiple times or just once?

dnlserrano commented 11 years ago

Thanks for your answer, @gregmolnar

In the Faye log my notifications only seem to get published once. The strange thing here is that sometimes it all goes right, and the behavior in the log seems to be the same in both situations (one message published, or multiple messages).

My message (to be published) goes through this "cycle" in the Faye log:

Received message via HTTP POST: Processing messages: Passing through incoming extensions: Handling message: Publishing message Queueing for client "": Passing through outgoing extensions:

gregmolnar commented 11 years ago

Than it must be something on the client side. It is very hard to say anything without seeing the code but I guess it is not a public repo.

dnlserrano commented 11 years ago

It is not a public repo, you're right. I have no clue about what I might be doing wrong on the client side, since almost all the logic remains in the server. In my application controller I have that code that does the publishing. On the client side I only place the subscribe_to html/embeded ruby tag.

Thanks anyway, @gregmolnar. I will get on this right after I finish some other stuff, and I will post the answer if I find a solution.

gregmolnar commented 11 years ago

From the code you posted all looks fine to me too. Let us know if you figure it out.

dnlserrano commented 11 years ago

Apparently I solved it. I was subscribing to my user notifications channel in the _header partial of my rails app. I moved the subscription to the _notifications partial and now everything is working properly. I still don't understand how the subscription in the header could be causing multiple occurences of a new notification (maybe it was something else that I did and do not remember?), but anyway, this seems to have solved it.