#401 ActionController::Live pro
Server-sent events allow you to trigger events on the browser from a persistent connection to the server. This requires Rails stream data which can be done through ActionController::Live, a feature upcoming in Rails 4.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
Frist!
On a serious note I was surprised you didn't mention your own gem 'private_pub'. (For which you have some pull-requests. One is actually quite good.
Anyway back on topic. I am currently running faye/private_pub in production and it has been doing relatively well. That said, my loads are pathetic. At most we have 50 users simultaneously using the system. Sometimes faye causes the browser to slow down ridiculously, at least on Chrome on Mac. Also messages get lost sometimes but I am not terribly worried about that. It's not critical data I am handling.
That said it just feels to me like the web has not been made for things like this. It feels like a dirty hackfest having to integrate 4 different pieces of software to make one thing more or less run. And this shows when you look at the hacky nature of what rails has done with it. I am used to rails being nice to use (not so much anymore. Rails got bloated... 'rbenv exec bundle exec rails c' great. real nice to type right?) but that controller just looks... ugly.
So I hope there will be a 'definate' solution to this problem in the future but I don't hold my hopes up.
Controller looks ugly? Move stuff to separate class.
Command is too long? Make an alias.
With a .rbenv-version file and bundler binstubs I just type bin/rails c. Which you can alias.
I think you're right about calling this a hack-fest. I think Ryan was alluding to that in the end, but covered it because it's a new core rails feature. But you have to keep in mind that if you need these services, you're going to have to integrate them somehow. I have found that Faye can be pretty fast, depending on your client side JS and architecture.
+1
Stefano: use play with scala for websockets, or just use http://torquebox.org/features/
10x for the video!
Torquebox looks great! How easy is it to migrate? Is anyone using it in production?
Great topic, I waited for this a for a long time. Thanks!
One thing that I missing in this episode: Redis was described instead of PostgreSQL. Going to figure out how to do this with pgsql. Maybe this gem is applicable: https://github.com/ryandotsmith/queue_classic
Tried to implement this episode, it works!
Not such a big overhead adding Redis server.
Rails has to move in this direction.
If not it'll get obsolete in no time. With competition from meteor, backbone and all the other js frameworks, people start asking: "Why do I need this bloated Rails stuff?" The whole convention over configuration is a double-edged sword. Users shouldn't (and therefore don't) know about the trivial (but hard to solve on you own) questions.
For example Rails solves crsf with one line in the application_controller. Many other frameworks requires more configuration and because its hard to understand that leads to solutions like "- just disable crsf". In rails you don't have to think about the trivial stuff, just make your application.
The browsers to day is no longer dumb terminals. So, we have to make them work more trough javascript and JSON. Rails have to make better conventions for better handling JSON and open connections if we want to keep it relevant. If we keep on mainly sending rendered HTML down to a (stupid) web browser, rails soon be the old hog.
This episode shows that still it is to much configuration. The rails core team should make decisions. Code should be simple like this
+1
+1
This episode makes me a little sad.
It's almost easier to setup Faye to handle this.
Excellent presentation, but its an excellent presentation of a kludge. After so many, "yes, but... no... do this.. but then that won't work the way it used to." twists, it is apparent that the problem wasn't with the pedagogy, but rather the feature itself.
Rails isn't ready for this solution, and I share with you the hope that new versions down the pike are more promising.
It might be worth checking out: http://www.pubnub.com/
I love the "theme" song :)
I'm personally excited for this, simply because the complexity of web sockets or comet always bothers me, whereas SSEs seem fairly straightforward. Its unfortunate that there are so many gotchas, but one could hope that, if not in the 4.0 release, a 4.0.1 or even a 4.1 would fix them
I may be wrong, but I think the postgresql listen/notify will require polling, too. So that's not really an option for "instant push".
This gist of mine shows where you have to SELECT null in order to get the socket to send along any unprocessed notifications, at which point you can get all notifications available on the connection.
https://gist.github.com/3915671#file-reel_ws_pg_example-rb-L21
Also, Tony Arcieri's reel is an excellent alternative in many respects to using live.
Do SSEs work on heroku. I ask because was looking into websockets on rails and heroku, but they don't support them.
I'm trying this on heroku, but it seems the pub/sub doesn't work . Does anyone use this(ActionController::Live, puma, redis) on heroku. I want some advice.
+1
+1
Has anyone had any success getting this to work on Heroku? I have it set up, along with redis-cloud, and am able to get pub/sub working, but I need some advice on how to set up my pool size and puma threads...I very quickly reach the connection limit on the free tier of my redis-cloud account as well. Hoping someone has some experience with this.
https://github.com/acdesouza/rails-and-server-sent-events
Devise is almost ready to use with Rails (look at 'rails4' branch in its repository), but including
ActionController::Live
crashes the application.For the moment, solution is to move
events
action to separate controller.Does anyone know if it works with apache and passenger? I've been using it for a while.
How to use Puma by default: (by running just
rails s
insteadrails s puma
): http://stackoverflow.com/questions/14146700/how-to-change-the-default-rails-server-in-rails-3/14911994#14911994Wow, great episode. Wanted to know more about that new feature for a while. Thanks!
Your opinion on this is correct, it's just not worth the effort because all of the restrictions on concurrent connections. After trying many possibilities (ActionController::Live, Socket.io, Faye,... ) we decided to outsource the real-time component entirely to pusher.com - highly recommended. It takes literally minutes to set up and you don't have to worry about updating, uptime, compatibilities, scaling...
(disclaimer: not affiliated to Pusher in any way)
Tomaz, thank you very much, pusher is great service, not affiliated to them too :) just used it with a free account and I am pleased.
Just thought I'd mention that !! works in bash, but it doesn't auto-expand like that. I wasn't aware of it... hopefully I don't forget before I have occasion to use it.
Does anyone know why has rails not adopted EventMachine? When i google around, i am unable to find any good concrete examples of usage of EM with Rails3.2. Would we have any out of the box compatibility from Rails4 for EM?
Are there any good reasons why psubscribe wouldn't work in the latest Rails4.rc1?
The rest of the tutorial's working a treat, I just cannot for the life of me figure out psub..
Hi Ryan,
using the pubsub approach within the ActionController::Live we ended up with a lot of Threads that are never closed. I think Redis subscribe and psubscribe are blocking the created Thread and stream.close will never be called, will check this soon.
What we've noticed gets even worse with new visitor and each page reload we ended up with a new Thread. At the end we had many Threads sleeping consuming all available Threads for puma.
There is also an issue at github addressing this: https://github.com/rails/rails/issues/10989#issuecomment-21305888
First of all: Great episode again, Ryan - thanks!
We had the idea to implement a notification pushing mechanism based on the ActionController::Live functionalities. Basically everything works fine from the functional perspective.
But: Am I the only one who doesn't want to accept that you have to restart your development web server (Puma) every time you change something in your code?
For me this is highly uncomfortable. Does anybody else encounters the same problem? Maybe there's a solution for this somewhere out there that I can't get hold of right now?
Yes there is a way, I recommend only to use in development, it has some drawbacks.
Hey Ryan, have you had a chance to reasess your opinion of ActionController::Live since first releasing this cast?
Great Episode. I really enjoyed it.
The Redis subscription keeps blocking my req-res cycle and my browser seems to keep loading for ever. Does any one has this issue? Please help.
Following your suggestion (echoed by Aaron Patterson) that thin is a suitable (ie., non-buffering) server, and since my requirement is fairly simple, I'm trying to get Live to work on the generic thin server. But no matter what I do it appears to buffer its output, so the client sees nothing from my response stream until the whole thing is done. When I switch over to puma it works fine, so the problem must lie with thin, but I can't get it to stop buffering the response to save my life. Any ideas?
hi steve, did you ever find a solution or determine the root of this problem? i'm having the same issue running rails 4 and thin on OS X 10.6.8 in development...
Hi, when I create a new rails app and follow the tutorial, it is impossible to have actioncontroller live working properly. When I check what is going on in the browser, the "events" action is marked as pending.
When I use the app built by Ryan it is working properly. However, if I do "bundle update" the "events" action is not working.
Does someone have an idea what settings I have to change on a new rails app ?
I tried to implement application with ActionController::Live again, and had some troubles with multi-treading. I found it doesn't work. But then discovered, that it actually zsh's
repeat
command doesn't work appropriately. It sends commands one-by-one. Checked with zsh 5.0.0-2ubuntu1 version.Also, I tried to check by opening two different Chrome tabs. Same effect, Chrome waits while one request will finish, then sends another.
So multi-treading can be checked either by sending two curl requests from different console tabs, or by using two different browsers.
Is there in ActionController::Live some way to generate private secured channels?
Could anyone managed to make this work?
I need to implement live update in a new project. I updated the source code provided with this railscast to work with rails 4 but it doesn't.
Seems like the javascript isn't triggered when a message comes in
Warning! ActionController::Live works badly with Redis, because Redis' subscribe function hangs the process and the stream never closes. More information: https://github.com/rails/rails/issues/10989
In rails 4.2 you have to use
rescue ClientDisconnected instead of IOError
Love you!
I want to get response from this event method using rest client. It does not response. Is the Redis subscribe method create any problem for Restcleint ? I make question in here also http://stackoverflow.com/questions/28485446/rest-client-not-working-for-rails-server-sent-eventsse
+1
I have written an article on how to use Server Side Events in Rails 5