1

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

scaryguy
  • 7,720
  • 3
  • 36
  • 52

2 Answers2

2

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):

You may want to install curb (CUrl RuBy) here: https://github.com/taf2/curb

I could write some code if you wanted?

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • Thank you very much for your answer Rich! You've truly understood what I want to gather. But I don't understand what's the point with 'push' logic if I'll just send a GET or POST request from API to my main application? I think I misunderstood something? Shouldn't Push be used for the purpose I mentioned? I can just use `Typheous` to send an HTTP request to my main app? – scaryguy Feb 22 '14 at 11:22
  • The pusher notification is meant for two way communication (you have to be *subscribed* to a channel to receive updates). There's no way to "subscribe" a rails app to another app's updates -- you have to initiate an HTTP request of some sort. I just recommended CURL (because it's the only thing I've had experience with). If you wanted to use `Typheous`, it's totally up to you!! – Richard Peck Feb 22 '14 at 11:34
  • So can we summarize like this? "In order to send a notification from an API to an app, the best/most convenient way is sending a regular HTTP request from API to app" So what is the use case of Websocket then? How to gain full-duplex communication between the API and the app? :\ – scaryguy Feb 22 '14 at 11:42
  • No you can't just summarise like that. I still need to learn more about API's, but I don't believe an API is meant to initiate any requests, just responds. If that's the case, your system's design of using an API to "send" requests is not correct. Here's a great resource on API's: http://en.wikipedia.org/wiki/Representational_State_Transfer – Richard Peck Feb 22 '14 at 11:51
  • If you're looking for a "full duplex" connection between API & app, I would recommend asking a new question (I need to learn more about it!). My gut tells me it's TOTALLY possible, but will need to be deeply integrated into your app, with an authenticated API – Richard Peck Feb 22 '14 at 11:54
  • There's another tutorial here: http://www.pubnub.com/http-rest-push-api/ -- this is very similar to the Pusher question -- about subscribing to a particular "Channel", which the server will update – Richard Peck Feb 22 '14 at 11:54
  • 1
    Well, I think you're right. I need to ask another question. I'm gonna do it. Stay tuned to upvote :D – scaryguy Feb 22 '14 at 12:25
  • Let me know the link - I'll come and see if I can contribute! – Richard Peck Feb 22 '14 at 12:27
  • Here it is: http://stackoverflow.com/questions/21954483/how-to-create-a-full-duplex-communication-between-an-api-and-an-app-in-rails – scaryguy Feb 22 '14 at 12:42
  • Hey Rick, I had asked the same question to Pusher support and I got the answer I was looking for. I think I'd better to sign my own answer as accepted answer since people may look at this thread and may not notice my answer. Sorry for that and I'm up voting your answer as a present from me :) – scaryguy Feb 25 '14 at 11:41
1

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 .

scaryguy
  • 7,720
  • 3
  • 36
  • 52