publish_to from controller (documentation) #31

Open
rubytastic opened this Issue Jan 29, 2012 · 5 comments

Projects

None yet

4 participants

@rubytastic

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
pokonski commented Feb 5, 2012

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

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

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
zlu commented Feb 16, 2012

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
ajbraus commented Mar 29, 2014

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment