ryanb / private_pub

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

publish_to from controller (documentation) #31

Open rubytastic opened 12 years ago

rubytastic commented 12 years ago

Publish_to from a controller doesn't seem to work (in the readme it states this is possible although the docs are not that clear on further details ) Or I misinterpreted them.

IndexController#Index PrivatePub.publish_to("/notification", "alert('blablabalabl');")

And in my /app/views/index.erb <%= subscribe_to "/notification" %>

My understanding was this should publish to the /notifications and push the alert out, but that isn't the case. It only seems to work on a create action wich is JS enabled.

@ryanb can you clarify how this works? thx for the great gem!

pokonski commented 12 years ago

Maybe it's because the controller action is executed BEFORE the subscribe_to in the view. Therefore you are pushing notification before actually subscribing the user to a channel.

rubytastic commented 12 years ago

Could be could you share basic example maybe on how you implemented? For starters i look to code a notification channel and send data to that channel from anywhere in my app. The instant messaging thingy could wait. I tried to add the publish to channel in my view in header and footer to no avail. The controller would be executed first maybe

rubytastic commented 12 years ago

Still haven't resolved this, anyone an idea what might be wrong? Ive reviewed the original docs and the rails cast once more and checked that everything is fine still I'm unable to publish_to from inside a controller

zlu commented 12 years ago

You can publish from controller, model, or anywhere that is Ruby :) It looks you are following the "Alternative Usage" section of the README. Instead of publishing a javascript string, first try with a simple string such as PrivatePub.publish_to("/notification", "blah blah") Now in your view, besides calling subscribe_to (which only generate script tag that enables access to interested channel), you will also need code like this: PrivatePub.subscribe("/notifications", function(data) { alert(data); }); In this example, data is "blah blah".

ajbraus commented 10 years ago

I tried your example @zlu to no avail. What is a better solution to this?