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.
Related
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.
Recently I've been looking for a solution to implement real-time updating web pages, for example, Twitter-like news feed or real-time chat. I've discovered some ways, as Pusher service, faye, and quite a lot of ruby gems, like private_pub or sync.
The problem is that this solutions don't seem to be a completely right way to follow. Pusher is rather expensive, and in fact I would not prefer to use other servie in my project. Faye seems insecure, and it is quite hard to implement security for it. Private_pub does the right thing, but last commit on github was in 2013 and in fact it is quite outdated.
All in all, ways that I have discovered do not seem to be professional-grade solutions for Rails startups. I have come up to the question whether I should completely switch to NodeJS or other technologies, or I can integrate NodeJS app inside a Rails one?
To sum up, is there such solution for Rails framework, or switch to another technologies is inevitable?
It may not help you right now, but at RailsConf last month DHH announced that Rails 5 will add support for websockets via a new library called ActionCable.
https://www.youtube.com/watch?v=oMlX9i9Icno
MessageBus might be a good fit. It's currently used in Discourse to implement live updates.
I'm also not sure what your security concerns about Faye are exactly. You should have no issues if everything is operating over HTTPS with proper CORS settings.
As for a mixed Node/Rails solution, you could push some list (e.g. post and list of those to be notified) on an update in the Rails app to a Redis instance. A Node app subscribed to Redis could then notify clients to make a request back to the Rails server for the latest updates.
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
I am new to the web socket framework with Rails and generally I feel I should get my hands to start working with it. I have used Faye for my private message publishing and it works nicely. So I want to ask if there is a nice tutorial out there that can show me how to use web sockets with rails. Thank you.
Node.js + socket.io is probably your easiest option right now. Faye is set up to use Node.js, so you may have already have Node.js running alongside your rails app (depending on how you're using Faye). You'll just want to start using socket.io. The socket.io repo includes some very useful examples - I sugget looking at chat.
You can communicate between your rails app and Node.js server via http. Node.js lets you easily make an http server. You may also want to take a look at request.
I am wondering how to integrate node.js on a rails app (for learning purpose).
Based on Michael Hartl tutorial (http://railstutorial.org/) I realized a basic twitter clone with rails and want to get user microposts in real-time without the use of comet or juggernaut. (the application is hosted on heroku)
For the moment, I only see example with node.js frameworks (http://howtonode.org/grasshopper-shoutbox) but nothing merged with a ruby on rails app.
I would be very thankful if someone knows a good tutorial or give me some points to start in order to accomplish this.
Thanks!
As Shripad said, I'd consider trying to build your app with Node by itself. Geddy will feel familiar (getting started anyway) if you have experience with Rails. Note: I do not have experience on a real world app with Geddy, but it is the best Rails-like framework I've seen so far. For persistence you can use SQLite, PostgreSQL or CouchDB, just like you would with Rails. I thought about how to communicate between a Rails app and Node without any intermediary. In our work project we're using Redis as an intermediary between Rails and Node. Rails publishes messages to Redis, Node pulls messages from Redis. I could not find a good way or example projects to avoid the middle communication layer on a personal project, so I went with the same setup. The good news is Node Redis modules are written and once you get everything installed, it is easy to test out pushing messages back and forth.
If you are looking at creating real-time apps then go with node.js (high concurrency) alone. You really cannot integrate node.js into a rails app. You can however have a node server setup on another port with an api and websockets configured and then have your rails app communicate with that server. It is PITA to do that kind of setup. You rather build the entire web app in node itself. However, if you want anything rails specific that does not use juggernaut then i would suggest http://www.pusherapp.com.
Its extremely easy to setup server push using Pusher.
It already did. Not really NodeJs but a framework built on top if it. Yada, yada, yada... check this out: https://github.com/1602/express-on-railway
**Run node along side your rails server**
If you want to intergrate your Rails app with Node you could use the node-rails gem
Node Rails will enable you to run a Node server along side your Rails application and have the two share authentication NodeRails assumes you are using Devise for your authentication. Node-Rails uses [redis gem][2] , so you will need to have that installed.
Learn more about using npm packages on Rails.