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
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.
This may be sort of a newb-ish question. I know you can do this kind of thing in Node.js pretty easily, but I don't know what it's called and haven't had much luck with Google.
Basically, I am trying to build a simple tic-tac-toe server with Ruby on Rails. Players connect to each other, and moves are recorded and results processed live. If it was just having the user send messages to the server, that would be easily done with AJAX. However, I want to have the client wait and listen for the server to point out that the other player has made a move, and then automatically respond to that. I could do this by pinging the server with AJAX constantly, but there must be a better way. I feel like I'm missing some big technology that I haven't found yet just because I'm not entirely sure how to describe it or what it would be called.
Would I want to have the client connect directly to the server and maintain a live connection? If so, how would I do that? If not, what is the better way to do this? How do online games and stock tickers and streaming services provide their content to the client, and what tools does Rails give me to do something similar?
Check eventmachine in ruby
some links
https://github.com/eventmachine/eventmachine/wiki
http://20bits.com/article/an-eventmachine-tutorial
http://rubysource.com/introduction-to-event-machine
There is a faye and private_pub gem which makes things simpler .
There are railcasts available.
http://railscasts.com/episodes/260-messaging-with-faye
http://railscasts.com/episodes/316-private-pub
I recommend trying private_pub which is built on top on faye which uses eventmachine which can solve most of your questions
the pub/sub model helps to the client to subscribe to its channel
, so you can push updated to the channel which will be eventually passed to the client.
Your question and my answers
Q. Would I want to have the client connect directly to the server and maintain a live connection? If so, how would I do that?
A. You can use private_pub or faye to establish long connections and push data
also Check our later for pusher.com they provide services on commercial basis.
Q.How do online games and stock tickers and streaming services provide their content to the client, and what tools does Rails give me to do something similar?
A. AFAIK everyone uses some sort of push technology. pub/sub model. ruby has some gems available for such requirements, faye private_pub some of them..
For a commercial solution, check out Pusher, it does pretty much exactly what you want:
http://pusher.com/
For open source solutions, check out Faye (a pub/sub messaging server):
http://faye.jcoglan.com/
and some awesome railscasts explaining it:
http://railscasts.com/episodes/260-messaging-with-faye
http://railscasts.com/episodes/316-private-pub
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.
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