I'm implementing Notifications in an Angular app that will get the data from a Rails API. I've been thinking about using the Angular Poller module in a polling technique, however I was wondering if there's a better way where you can push the data from the Rails API to Angular whenever there is an update?
-
There is - webSockets. Have a read here: fenixapps.com/blog/nodejs-angularjs-and-socket-io. Unlike polling, web sockets gives real live-data integration. However it needs much more work to be done. Currently Rails does not support sockets unfortunately, but it is to be supported in Rails 5 - and those are to be released very soon (fall 2015).BroiSatse– BroiSatse2015-09-25 09:01:38 +00:00Commented Sep 25, 2015 at 9:01
1 Answer
Socket.io is one way, if you want to make a node.js application that acts as a "lighthouse" and redirects requests from the rails application. But be careful with authentication, since if you do it that way, you will have to make it impossible for someone else to use your "lighthouse" in his application for the same purpose.
There is a pure rails way of doing that, by using websocket-rails gem:
https://github.com/websocket-rails/websocket-rails
If you manage to go through the instructions and get it running, you will able to do everything you need to send realtime notifications to the web interface in a pure rails way.
Important note: another issue is making it run on production with passenger and nginx or apache: https://github.com/websocket-rails/websocket-rails/issues/67 http://blog.joshsoftware.com/2013/05/28/websocket-over-nginx/ Here the server needs to be configured correctly to make reverse-proxying websocket requests possible.