ryanb / private_pub

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

Publishing from models, background workers, etc. #15

Closed mrrooijen closed 12 years ago

mrrooijen commented 13 years ago

Hey,

is there any way you could publish from within a model or a background worker? I've tried including the modules inside models but it'll still throw an undefined method exception for the capture method since that isn't available either, and I have no idea how to get it there either. I understand that the model might not be the most elegant location to publish messages from but I have some processes that run in the background and I would like to keep the users on the front-end updated about the "status" of these processes by publishing live updates to them.

Any idea how I could accomplish this?

Thanks!

BinaryMuse commented 13 years ago

Michael,

Starting in bdf306b9a10200a6811f6a1c9d29bdd4282e32da, you can publish any object that responds to to_json--see example. I don't believe that the gem version of private_pub has this commit yet, so you'd have to work from master until then.

mrrooijen commented 13 years ago

That is awesome. I'll give it a shot in the next hour or 2-3 and see how it goes. Thanks!

ryanb commented 13 years ago

@meskyanichi, Currently publish_to is only available to the views. I plan to make it available in all controllers, but that will not solve your problem here. I should probably make a PrivatePub.publish_to method which works similarly. The only problem with that is the ActiveSupport dependency with to_json. I'll do some experimenting.

mrrooijen commented 13 years ago

@ryanb Yeah I noticed. I actually (using the latest master branch commit) did a include PrivatePub::ViewHelpers from within a model and controller, both work fine if you use the JSON format.

Though, as you said, it's probably a nicer idea to go with PrivatePub.publish_to since it'll be available from anywhere without needing to include modules.

Really loving the combination of Faye and PrivatePub. It's really sweet, I can find so many use cases for this. So, thanks for your work!

inspire22 commented 13 years ago

Any luck with this? Jug.publish is how juggernaut 2 does it, which seemed to work really slick. I'm not a huge fan of "mysterious" view helpers in general. Often if it's just a simple string too, a js template is overkill vs just Ppub.pubilsh('myroom',@params['msg'])

ryanb commented 13 years ago

@inspire22 I haven't had a chance to work on this yet, but it should be a fairly easy addition. Just need to move some of the code around.