Thank you for this video, very interesting way of autoloading new notifications.
However, polling sounds a bit "old school". I read a lot about Active Live and websockets with the upcoming release of rails 5. Wouldn't it be interesting for this case to share new notifications automagically to all users concerned by them as soon as they are created ? Or maybe it's too heavy ?
Polling works just fine in a lot of cases unless there's an absolute _need_ to have realtime. It's rare for notifications to need to be truly instant. The other beauty of polling is that you can just write regular old Rails and JS code which you're already familiar with scaling. Probably 99% of the time, polling will do the trick just fine.
However, that's something we'll be talking about in the future. WebSockets are a whole lot more complicated to setup, manage, and maintain, so it will be a topic for a separate series (one that's coming soon!).
Or, shorter, using the alias "j":
<li class="nav-item btn-group" data-behavior="notifications" data-notifications="<%=j render template: "notifications/index", formats: [:json] %>">
Otherwise, a quote character in the generated JSON can break your HTML.
Why have you decided to use CoffeeScript here? To me it seems really hard to justify when features are incompatible with ECMA 6. This makes me a little nervous about relying on these lessons.
If I were you, I wouldn't be looking forward to dealing with conflicts with keywords like `for..in`, `class`, and even `super`. But to each their own.
No particular reason. I have been using CoffeeScript before ECMA 6 and am familiar with it so it's easy to use. Once nice thing is it's easily translatable to JS as well for anyone not familiar with it.
And yes, ECMA 6 (and 7) solve pretty much everything there. I'll have to do an episode showing how to use ECMA 6 with Rails as well.
Chris, is using
setInterval good for our Rails app performance?
It will add extra requests, but they are pretty simple, plus you can enable caching around the JSON response to make it fast.
Great as always. Something i noticed is when
setInterval is fired and if the drop down menu is open, it will make disappear its content. How could we prevent that ?
Thank you for the video, Chris! I implemented the notification feature by watching the previous tutorial and it works great! But it's tedious to test in the browser because I have to be logged in as two different users. I tried writing Jasmine test and capybara test but I'm a bit stuck. It'd be awesome if you could cover testing for this feature, too. Thanks for the tutorials!!
You can use this approach still the exact same way with ActionCable if you want to preload some data in the view before the Websocket connection gets initialized.
For the most part, with websocket stuff, I imagine in most cases you will want to just display a loading spinner instead while the connection gets setup. They'll happily work together though with no problem. The only difference is that you change the mechanism to update the data from AJAX to Websockets.
If I'm using active model serializer, the way to do this on the view is to use `@notifications.to_json` on the render?
Join 20,000+ developers who get early access to new screencasts, articles, guides, updates, and more.