Rails Notification Without Page Refresh - ruby-on-rails

I created a simple private messaging app through rails, but I am trying to get it to simply show that new message has arrived without the person having to refresh the page.
I am looking at State Machine because I am thinking that it might be a part of building something like this. Can anyone push me in the right direction?
Thanks.

The simplest way to do that using javascript is to periodically send requests to the server using setTimeout + $.get().
Another a little bit more complicated way is to use gems like faye . There is nice railscast on this theme.

Take a look at any of the messaging protocols like:
Faye
Socket.IO
or hosted sollutions like
PubNub or Pusher

Related

Is it possible to use 'push' services at the back-end?

I'm using pusher gem to manipulate my front-end from an external API. It works fine, no problem with that.
But the thing I wonder is if there is a possibility to use push notifications at the back-end of my application? I spent a serious amount of time investigating this but couldn't find something useful.
Let me summarize:
I have an application and another API application which is tightly interacting with other. Sometimes I want to use my API to send notification to my main application and I want to be able to manipulate data at the back-end of my main application regarding the data received from API side. These are things like 'an action was completed/started/succeed' etc...
I understand that 'pusher' receives push notifications by JavaScript at the front-end. But I believe that there must be a way to use those notifications at the back-end as well.
If there is another way (maybe Faye? Websocket) to do that I'd love to learn what it is. Any clue would be appreciated.
Is it something doable?
Thank you
Pusher is a backend system too (to "push" updates to channels)
Endpoints
I think you may be interested in endpoints
From what I can gather, it seems you're looking to trigger the transfer of data to an endpoint once an action occurs in your API? For example:
User signs up on "API" app
API app sends "notification" to main app
Main app increases user count by 1
The way I can see this working is by either using ajax, or sending a curl request to your main app's endpoint (set in routes), triggering the action:
#main_app/config/routes.rb
post "endpoint", to: "application#endpoint"
#main_app/controllers/application_controller.rb
def endpoint
#count = Option.increment!(:user_count)
end
This will allow you to manipulate your data in the backend of your "main" app
API
The tricky, non-conventional part comes when you want to send the data from your API app to your Main app (this is where you got the "pusher" idea from)
I would personally look at sending a standard HTTP request to the Main app endpoint, probably with Curl (if from the backend):
Curl on Ruby on Rails
Rails curl syntax
You may want to install curb (CUrl RuBy) here: https://github.com/taf2/curb
I could write some code if you wanted?
I had asked the same question to the Pusher's support team and I got the exact answer I was looking for.
You can install a client library on your server
(http://pusher.com/docs/client_libraries) if there is one for your
server. You can then subscribe to a client channel this way.
In my case, I use Ruby gem which can be reached from https://github.com/pusher/pusher-ruby-client .

sendgrid_webapi event notification sendgrid

I'm having difficulty retrieving the event notifications for my SendGrid account and I'm not sure how to proceed.
I'm utilizing the sendgrid_webapi gem (https://github.com/kylejginavan/sendgrid_webapi) and I'm attempting to retrieve the event notifications for the client using the barebones implementation that the README.rdoc provides.
Very simple:
client = SendGridWebApi::Client.new(*user*, *password*)
notifications = client.event_notification.get()
return notifications
Each of the other calls (bounces, unsubscribes, etc.) work as expected, but for some reason I've been unable to retrieve anything other than the URL that I've specified within the SendGrid interface (I've enabled all events and the event notification app).
The documentation for the gem states that the options available can be found at http://sendgrid.com/docs/API_Reference/Webhooks/event.html but I've been unable to take that information and turn it into something tangible that I can use when making the call.
I feel like this is just something simple that I'm missing, but any help / guidance would be greatly appreciated.
Thanks in advance!
The API call you are trying to use is for configuring the Event Webhook, not for consuming events.
A webhook is a "push" API rather than one that you query for information. You need to setup a URL with a script that can handle HTTP posts from SendGrid in order to use the webhook.
Here is a gem called gridhook that can do what you need.

Can I use github-services hook to post my feeds to other services?

Github has developed github-services hook to push commits to other services like bugzilla, campfire, basecamp ..
Can one use the same github-services hook to push my application data to other services? If yes how may I integrate github-services to my Rails application.
Any Help ? Any suggestion ?
Update Can I integrate github-services hook source code as Sinatra application inside my Rails application ? How may I call other services(bugzilla, campfire, basecam, twitter) hooks from my application triggers ?
As example, When one user post something on other user's wall than message should be sent to the other services like bugzilla,campfire,basecamp, twitter ...
The Post-Receive Url is the simplest hook to perform such notification. It triggers a POST to a pre-configured Url whenever a pushis performed on the repository.
You could start with this Github.help page on testing web hooks to understand the format of what is being POSTed and how the service reacts. This is done thanks a very useful service: PostBin.
This help page gives a simple example of what one would have to implement on a Sinatra server to parse the POSTed JSON:
post '/' do
push = JSON.parse(params[:payload])
"I got some JSON: #{push.inspect}"
end
This gist goes a little further and show some really basic JSON data extraction.
If you want to go further, you can configure, through the GitHub API, some additional hooks to listen to more events (new issue, new fork, download, ...).
I think you are looking for an easy way to post your app's data to many other web services.
github-services is designed to take git commit information and push it to other services that accept that commit information... so if your app's data looks enough like github's payload, then those other services that work with github-services will work with your app.
But I suspect your app is not like github and your data is different than a git commit. In that case, you could make use of the code in 'services/' as examples of how to implement event handlers in your app. This one for Campfire uses the Tinder gem, for example: https://github.com/github/github-services/blob/master/services/campfire.rb
Then your WallPostsController#create could call a method that posts data in the format you choose to the various services. If you're going to post to many services, you may want to do it in an asynchronous job (DelayedJob, resque, etc.) because calls to many external services will take quite a while.

Ajaxmessaging rails gem - replacement?

I found this gem:
http://code.google.com/p/ajaxmessaging/
Because this project is inactive I need something more fresh to do the same stuff, in my case, notification messaging to the client.
There is something similar and better than this? (for Passenger)
You want to look at Pusher (http://pusher.com), Faye (http://faye.jcoglan.com, http://asciicasts.com/episodes/260-messaging-with-faye), or Juggernaut (https://github.com/maccman/juggernaut). Pusher is probably easiest to get started with, but its a commercial product after a certain level of usage.

Ajax Push Engine

are there anyone hear about APE (Ajax Push Engine) before ? I'm building Rails application and trying to create group chat with this APE realtime engine, the problem is how to make Rails communicate with APE Server ? Are there any tutorial or reference on working APE with Rails ?
As far as I know, Mike's answer is not entirely correct.
For clients all clients the receiving part, in all scenarions, this is 100% correct: The communication is done by Javascript.
Also, the javascript can (and in many cases will be the best choice) be the most reasonable choice to push the info to the server too. This is the case with open communications of many to many people like in chat rooms.
However the following documentation page states clearly that we have other choices:
"Using inlinepush module to push data
from your php/rails/python/...
application to APE"
http://www.ape-project.org/wiki/index.php/Tutorial:How_to_write_an_application_with_APE
There are many cases that this could and should be so. I can think of at least 2 scenarios:
Applications that only logged in users could post things to others. Your app would handle the login and inline push info to the ape server.
Applications that broadcast live information from one source to many clients (in this case only the admin could post and this configuration is done on the server side also.
Let me note that I'm not using APE yet, I'm researching it and if I find I've posted any misleading info here, I'll come back and correct myself.
The short answer is that "you don't" APE works with Javascript calls back to the APE server (typically proxied through Apache or whatever server you're using).
Conceptually, if you wanted to break out into different "rooms" or whatever, you'd setup defaults within the javascript from the rails settings.

Resources