Communication between a Rails web app and a Mac app
I have a Rails web app that is used to gather data from an iOS device running an app I have developed.
The Rails web server will be handling communication with the Apple Push Notification service, so the iOS app will be sending it the required Device Token and some other application specific data.
The Mac app will be communicating with the Rails app to push a message to an iOS device. Both the Mac app and the Rails web app will be running on the same Mac server, under the same user account.
The communication between the the iPhone and Mac apps and the Rails web app will be done using the RESTful web services exposed by the web app.
Once the iOS app has sent its data to the Rails web service, I need that data to be sent to the Mac App that will be running on the same server as the Rails web app, so that it know which messages need to be pushed and where they should be pushed to.
What should I be looking for if I want to communicate between a Rails web app and a Mac app on the same box?
I have looked into polling the web app from the Mac app via a RESTful service, but I don't think this is the most elegant or efficient solution. Is there some way that I can invoke some action from the Mac app from Ruby?
Edit:
I've just read ab开发者_Python百科out NSDistributedNotifications
and the the Distributed Notification Center. This sounds like what I'm looking for.
Is it possible to use RubyCocoa from the Rails app to send a distributed notification and have the Mac app listen for this notification?
I think you can make your cocoa app implement a server which listen to a local port like localhost:28888 , and while your rails application receiving data from your iOS app, you can just sent it to localhost:28888
Maybe not the best solution, but if the Rails app can write the notification data to a lightweight local database or just plain text files in a specific local directory, and let your Mac app to read the data in a timely fashion, data have been read can be deleted or moved to another table/directory. This could be a temp solution, the ideal solution should be a single Mac web app as Rob mentioned.
I do not have any personal experience with this on OS X, so take it with a grain of salt.
If your app's main objects are KVC/KVO compliant, you can expose its properties to AppleScript, and use Ruby's scripting bridge to change values of the Mac app's properties.
Then, internally in the app you can listen to those properties and act accordingly.
If the app is developed as Document-based, then much of the KVC/KVO compliance and exposure to AppleScript will be automatic. See this: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_implement/SAppsImplement.html#//apple_ref/doc/uid/20000037-BBCJEEEC
On the other hand, if the app is developed as a single-window app, you will have to implement the scripting support yourself.
Probably it is not the best idea to relay on 'they are both on the same server with the same user' because sometimes in the future you might have to scale it, and then you will have to rewrite big chunks of code.
If you continue to use restful apis you can move everything wherever you want.
there are a lot of ways to do this kind of IPC tho for example you can have: socket - socket(2), fifo file - mkfifo(3), shared memory - shmget(2) very simple solution is to have the mac app listen on udp socket and wait for commands
here is an example for udp echo:
http://developer.apple.com/library/mac/#samplecode/UDPEcho/Listings/UDPEcho_m.html#//apple_ref/doc/uid/DTS40009660-UDPEcho_m-DontLinkElementID_5
even more simple is mkfifo:
$ mkfifo /tmp/app_communication_fifo
$ echo 'rails app message' > /tmp/app_communication_fifo & #this will block until somebody reads it
[1] 34415
$ cat /tmp/app_communication_fifo
rails app message
[1]+ Done
but i would recommend udp server or just stick with the restful api, because you can scale it as much as you want (hopefully when you have millions of users you can create more rails front ends and all of them can send messages to the mac server)
I don't think the NSDistributedNotifications implementation will help you. May be the underlying concept is appliable here. You need to implement a REST-Server on the iPhone in order to retrieve data. I'm not aware of a ready made API way of doing so.
Please correct me if I'm right!
精彩评论