Rails api PUSH chat message - ruby-on-rails

I am developing a Rails application that will be exposed by API only. The application has a real time chat system which involves users submitting data to the server (via API calls), but what I want to push this data to other clients.
what is the best way to send data/message to other client?

Even knowing that your question will be closed, because at least you need to put some code (what you have now) I recently came across a blog post that will guide you in the right direction.
What you need is the new Rails 5 (still in beta) and the new component called ActionCable. Here you have the blog post describing how to use it. Also you have the DHH ActionCable examples.
Under the wood it uses websockets for full-duplex communications, ensuring your users will get the notifications.
Since you want an API based solution, you may need more work to get it with your frontend framework.

Related

Connecting Firebase in Rails

I have a Rails app from which I want to connect to Firebase and communicate with Rails. Basically I'll have the Rails app monitor Firebase for any changes and dynamically CRUD those changes in my views. I did a fair bit of searching but there's seems to be a lack of community resources on how to achieve this. What would be the best way to do this?
The solution I found was to use firebase-rails gem, it's description reads as follows:
Ruby wrapper for the Firebase REST API.
Changes are sent to all subscribed clients automatically, so you can update your clients in realtime from the backend.

How to dynamically update client in rails when data in database changes?

I would like the data on a page to update as soon as it changes in database.
Is there a way I can trigger some event on server when data changes and send it to the client?
For example a user might see the number of friend requests he has and that number should update in realtime if some other user adds that user as a friend.
This is a brand new feature in Rails 4 (Server sent events). There is a Rails Cast on this topic.
If i m not wrong, then you are trying to write a Rails application in which a server can push data to multiple clients in real time.
Their are many guides that involve real-time push, and Rails 3.
Juggernaut
You can try juggernaut to do what you want.
The github repository : https://github.com/maccman/juggernaut Example of application with juggernaut : https://github.com/maccman/holla
Faye
Check out Faye: http://faye.jcoglan.com/ - I hear really good things about it.
if you're looking for a hosted solution, i've used Pusher http://pusher.com/ in the past, and loved it. i converted a site that used ajax polling over to pusher in about 30 minutes.
Ajax Push Engine - Complete Comet solution
You could use APE (Ajax Push Engine) Rails plugin.
APE is a full-featured OpenSource solution designed for Ajax Push. It includes a comet server and a Javascript Framework. APE allows to implement any kind of real-time data streaming to a web browser, without having to install anything on the client-side.
I hope this helps you.
Thanks

What WebSockets system should I use for this?

I'm building a Rails app where I need a real time commenting system. I'm going to use WebSockets, but I'm new to them and I'm kinda lost. I tried em-websockets and websocket-rails, but neither worked well with what I have to do. I also though of a Node.JS and Socket.io app, but I don't know how to start with that.
What I want to do is send a WebSocket message when a new comment is made on a post, on the create action of my CommentsController. I'll send a message containing the comment content and creator and the post ID.
Thanks in advance! :D
Sorry, but I dont think so. Be careful with WebSockets. It is fundamental concept that provides a very powerful mechanism.
Websockets is good for super, absolutely real-time applications like online games. For commenting system (even realtime) you dont need them, the AJAX is more then enought for this.
You could use a realtime hosted service if you don't want to deal with your own realtime infrastructure, fallbacks for older browsers, scaling complications etc.
I recently wrote a post on Smashing Mag on building a realtime commenting system. It uses PHP and Pusher (who I work for) but the separation between client and server should meant that you could use any backend technology/service. It also demonstrates how to progressively enhance your app.
The most commonly used self-hosted ruby technologies for realtime communication does seem to be Faye, as #Alfred suggested.
Just using websockets as the only available transport is not a good idea, because websockets are not yet supported in every browser. Luckily for example Faye does support multiple transports so that it will work in every browser. I also found this interesting video in the past explaining how you could use Faye in conjunction with RoR from RailsCast.

Push data from rails app to clients

I'm working on a rails app that will primarily be exposed by an api to various mobile clients (iOS, android etc). The application involves users submitting data to the server (via api calls), but what I want to include is the ability to push this data down to other clients. The general concept is similar to a messaging app, where I submit a message to the server from me client and the receiver is pushed the message from the server.
The only method I know of at the moment is to constantly poll the server, but there must be better tech solutions than this. Any ideas?
I would look at using a websocket within the page to push the updates.
You could implement this using Faye, which falls back to long polling and other work-arounds for browsers without websocket support. Faye has a pure-ruby implementation, so you could probably work out access to your model layer.
Edit:
Also, this is a project that combines Faye with Rails. It is fairly new, but might do what you want. Faye-Rails
You should check out http://www.pusher.com
Pusher is a hosted API for quickly, easily and securely adding scalable realtime functionality to web and mobile apps.
If you need self-hosted solution, then you should check out slanger gem https://github.com/stevegraham/slanger which is server implementation for pusher client libraries. When you feel you need hosted solution, you just change URL's.
Slanger is an open source server implementation of the Pusher protocol written in Ruby. It is designed to scale horizontally across N nodes and to be agnostic as to which Slanger node a subscriber is connected to, i.e subscribers to the same channel are NOT required to be connected to the same Slanger node. Multiple Slanger nodes can sit behind a load balancer with no special configuration. In essence it was designed to be very easy to scale.
Ruby has it's own event-processing library, implemented like a gem:
https://github.com/eventmachine/eventmachine
Maybe it helps you
I prefer event machine over any other solution. It is somewhat more complicated that faye but you can write way more sophisticated code using event machine.
You might wanna check this peepcode screencast on event machine

Ruby on Rails 3 - Public live chat

I want to create a public live chat application using rails 3.
I found some example on rails 2. Any one can tell you a good example / tutorial to develop a live chat application using rails 3.
I encountered several roadblocks as I tried to implement a public and private chat system in my rails 3 app. I looked at faye, juggernaut, node.js and more. Eventually after trying several approaches I was able to implement a system that works great:
1) I started by following the video guide to faye messaging in Railscast 260 as mentioned by Devin M. I was able to quickly setup a rails app that persisted messages, and a chat server that pushed these new messages out to all the clients. The biggest problem was security. I had no control over access to the chat server.
2) This lead me to using the private pub gem by Ryan Bates in Railscast 316 - which helps to secure your faye server by verifying the client's signature. This worked for securing the server but I ran into issues trying to verify the actual user with my authentication system and adding 'who's online' functionality. I worked on a hack of private pub to pass in the user details when authenticating but could not get things to work smoothly.
3) In the end I decided to move the chat server to pusher - a hosted API for real-time apps. I followed this tutorial on how to create a real-time survey in rails to get a feel for how to set things up. Although not directly about setting up a chat system - this tutorial along with what I had already setup from the Railscasts above (and the easy-to-read pusher docs), allowed me to quickly setup a secure rails 3 chat app - complete with authentication, 'who's online', status messages and more. The best part is...I don't have to deal with managing the chat server.
Hope this helps someone going through the same process as me.
You can get the basics down with Railscast 260, I assume a background in Rails/Ruby already and some knowedge of jQuery/JavaScript. The screencast has a text version here and the source is here, it's also on GitHub.
I'd start by checking out Ilya Grigorik's em-synchony + examples and looking at the code for the Hector private chat server gem.

Resources