Rails as iOS Application Backend    Shanghai On Rails – Saturday 21st, 2011
iOS Applications• Which   website doesn’t have an iPhone application ?• iPhone application add a mobility aspect to your w...
As a Rails Developer,why do I care about mobile?
Mobile websites
Mobile websites
Mobile websites
You will probably have to makean API for the iPhone / iPad or
Architecture
Architecture
Architecture
Architecture   Railsapplication
ArchitectureDatabase               Rails            application3rd party  APIs
ArchitectureDatabase               Rails            application3rd party  APIs                    JSON
ArchitectureDatabase               Rails            application3rd party  APIs                    JSON                    ...
ArchitectureDatabase               Rails            application3rd party  APIs                       JSON                 ...
ArchitectureDatabase               Rails            application3rd party  APIs                          Apple Push        ...
Challenges• Authentication   and Persistence• Data   format and optimization• Push   notifications scheduling
Authentication• Havethe iPhone behaves as a web browser.• Sending cookies with every request• Persisting   cookies on the ...
Authentication• Havethe iPhone behaves as a web browser.• Sending cookies with          authlogic every request           ...
Authentication• Havethe iPhone behaves as a web browser.• Sending cookies with          authlogic every request           ...
Authentication• Havethe iPhone behaves as a web browser.• Sending cookies with          authlogic every request           ...
AuthenticationExample using inherit_resources and authlogic
iOS side                           Application code• Not   as easy.• NSURLConnection   is       JSONRequest low level     ...
JSONRequest       ➊ Initiate HTTP request to /inventory➋ Handle networking, cookies, redirections and errors         ➌ Par...
JSONRequestYour delegate receives one of those two calls
JSONRequest• Youget a NSDictionary object easy to manipulate, you can view it has a the equivalent of a Ruby hash         ...
What about models?
We created a lightweightORM for the iOS platform
Models - Rails
Models - iOS
Models - iOS
Models - How to use it
Models - How to use it      /counters.json
Models - How to use it      /counters.json
Models - How to use it         /counters.json   /counters.json?name=shield
Models - How to use itYou directly manipulate objects, not JSON objects
Back to Rails
https://github.com/maxime/iorm           Application code             JSONRequest          NSURLConnection          iOS Ne...
https://github.com/maxime/iorm           Application code                ORM             JSONRequest          NSURLConnect...
Tips• Why   not using the built-in .to_json ?
Built-in to_json                                                2k bytes> puts Counter.all.to_json[{"counter":{"level_id":...
Write your own .to_json                                                 1k byte> puts Counter.all.map(&:to_json).to_json[{...
Push Notification
Push Notification                 Apple Push   Rails                 Notificationapplication                  Services
Push Notification
Push Notification
Push Notification
Push Notification08f9e3f8 36b6e364 f4e80310 fe3068ef dba588d2         1b19102c 0e652002 6ca9a785
Push Notification• Push   Notification Token identifies one device, not one user• Send it to your Rails backend and save it...
Push Notification• Rails   plugin: apn_on_rails• http://www.github.com/PRX/apn_on_rails• Rails3   branch
Push NotificationThis saves the message to your database
Push NotificationEvery ten minutes.. or every minute..
Push Notification• Rake   task triggered by crontab• Resque   scheduler• Ifyou want to schedule a Rake task every minute or...
Resque Scheduler
Thank you• maxime@ekohe.com• Thanksto maverick@ekohe.com for his contribution on the JSONRequest / Model ORM• Thanks      ...
Upcoming SlideShare
Loading in...5
×

Rails as iOS Application Backend

27,713

Published on

Slide from my talk at Shanghai on Rails on Saturday 21st, May 2011.

4 Comments
81 Likes
Statistics
Notes
No Downloads
Views
Total Views
27,713
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
438
Comments
4
Likes
81
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • For some sites, the mobile provides instant information: BBC, NyTimes, Twitter\nFor some others, they are just an extension on the platform: Google, Youtube\n\nInstagram, Foursquare very integrated to the mobile \n\n
  • For some sites, the mobile provides instant information: BBC, NyTimes, Twitter\nFor some others, they are just an extension on the platform: Google, Youtube\n\nInstagram, Foursquare very integrated to the mobile \n\n
  • For some sites, the mobile provides instant information: BBC, NyTimes, Twitter\nFor some others, they are just an extension on the platform: Google, Youtube\n\nInstagram, Foursquare very integrated to the mobile \n\n
  • For some sites, the mobile provides instant information: BBC, NyTimes, Twitter\nFor some others, they are just an extension on the platform: Google, Youtube\n\nInstagram, Foursquare very integrated to the mobile \n\n
  • For some sites, the mobile provides instant information: BBC, NyTimes, Twitter\nFor some others, they are just an extension on the platform: Google, Youtube\n\nInstagram, Foursquare very integrated to the mobile \n\n
  • For some sites, the mobile provides instant information: BBC, NyTimes, Twitter\nFor some others, they are just an extension on the platform: Google, Youtube\n\nInstagram, Foursquare very integrated to the mobile \n\n
  • For some sites, the mobile provides instant information: BBC, NyTimes, Twitter\nFor some others, they are just an extension on the platform: Google, Youtube\n\nInstagram, Foursquare very integrated to the mobile \n\n
  • For some sites, the mobile provides instant information: BBC, NyTimes, Twitter\nFor some others, they are just an extension on the platform: Google, Youtube\n\nInstagram, Foursquare very integrated to the mobile \n\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Rails as iOS Application Backend

    1. 1. Rails as iOS Application Backend Shanghai On Rails – Saturday 21st, 2011
    2. 2. iOS Applications• Which website doesn’t have an iPhone application ?• iPhone application add a mobility aspect to your web application - GPS, on-the-go, etc.• Push notifications brings retention
    3. 3. As a Rails Developer,why do I care about mobile?
    4. 4. Mobile websites
    5. 5. Mobile websites
    6. 6. Mobile websites
    7. 7. You will probably have to makean API for the iPhone / iPad or
    8. 8. Architecture
    9. 9. Architecture
    10. 10. Architecture
    11. 11. Architecture Railsapplication
    12. 12. ArchitectureDatabase Rails application3rd party APIs
    13. 13. ArchitectureDatabase Rails application3rd party APIs JSON
    14. 14. ArchitectureDatabase Rails application3rd party APIs JSON REST
    15. 15. ArchitectureDatabase Rails application3rd party APIs JSON REST over HTTP(s)
    16. 16. ArchitectureDatabase Rails application3rd party APIs Apple Push Notification Services
    17. 17. Challenges• Authentication and Persistence• Data format and optimization• Push notifications scheduling
    18. 18. Authentication• Havethe iPhone behaves as a web browser.• Sending cookies with every request• Persisting cookies on the iPhone• Good news for your Railer: nothing new!
    19. 19. Authentication• Havethe iPhone behaves as a web browser.• Sending cookies with authlogic every request devise ....• Persisting cookies on the iPhone• Good news for your Railer: nothing new!
    20. 20. Authentication• Havethe iPhone behaves as a web browser.• Sending cookies with authlogic every request devise ....• Persisting cookies on the Cookies! iPhone• Good news for your Railer: nothing new!
    21. 21. Authentication• Havethe iPhone behaves as a web browser.• Sending cookies with authlogic every request devise ....• Persisting cookies on the Cookies! iPhone• Good news for your Railer: nothing new!
    22. 22. AuthenticationExample using inherit_resources and authlogic
    23. 23. iOS side Application code• Not as easy.• NSURLConnection is JSONRequest low level NSURLConnection• JSONRequest provides you a higher level iOS Network Socket interface
    24. 24. JSONRequest ➊ Initiate HTTP request to /inventory➋ Handle networking, cookies, redirections and errors ➌ Parse HTTP Response as JSON
    25. 25. JSONRequestYour delegate receives one of those two calls
    26. 26. JSONRequest• Youget a NSDictionary object easy to manipulate, you can view it has a the equivalent of a Ruby hash {status: “ok”}
    27. 27. What about models?
    28. 28. We created a lightweightORM for the iOS platform
    29. 29. Models - Rails
    30. 30. Models - iOS
    31. 31. Models - iOS
    32. 32. Models - How to use it
    33. 33. Models - How to use it /counters.json
    34. 34. Models - How to use it /counters.json
    35. 35. Models - How to use it /counters.json /counters.json?name=shield
    36. 36. Models - How to use itYou directly manipulate objects, not JSON objects
    37. 37. Back to Rails
    38. 38. https://github.com/maxime/iorm Application code JSONRequest NSURLConnection iOS Network Socket
    39. 39. https://github.com/maxime/iorm Application code ORM JSONRequest NSURLConnection iOS Network Socket
    40. 40. Tips• Why not using the built-in .to_json ?
    41. 41. Built-in to_json 2k bytes> puts Counter.all.to_json[{"counter":{"level_id":2,"name":"Mend","created_at":"2011-01-11T10:56:02Z","strength":11,"cost":2,"updated_at":"2011-04-28T18:10:28Z","id":12,"initial_stock":25,"inventory_icon_id":21}},{"counter":{"level_id":2,"name":"Radiate","created_at":"2011-01-11T10:57:19Z","strength":11,"cost":2,"updated_at":"2011-04-28T18:10:30Z","id":13,"initial_stock":25,"inventory_icon_id":10}},{"counter":{"level_id":4,"name":"Float","created_at":"2011-01-11T10:50:37Z","strength":31,"cost":4,"updated_at":"2011-04-28T18:10:33Z","id":10,"initial_stock":15,"inventory_icon_id":31}},{"counter":{"level_id":2,"name":"Shine","created_at":"2011-01-11T10:52:47Z","strength":31,"cost":4,"updated_at":"2011-04-28T18:10:35Z","id":11,"initial_stock":25,"inventory_icon_id":12}},{"counter":{"level_id":6,"name":"Pure","created_at":"2011-01-11T10:50:18Z","strength":51,"cost":6,"updated_at":"2011-04-28T18:10:38Z","id":9,"initial_stock":10,"inventory_icon_id":30}},{"counter":{"level_id":8,"name":"Bounce","created_at":"2010-12-20T19:39:05Z","strength":51,"cost":6,"updated_at":"2011-04-28T18:10:41Z","id":1,"initial_stock":5,"inventory_icon_id":8}},{"counter":{"level_id":9,"name":"Reflect","created_at":"2011-01-11T08:56:10Z","strength":71,"cost":8,"updated_at":"2011-04-28T18:10:44Z","id":6,"initial_stock":10,"inventory_icon_id":9}},{"counter":{"level_id":11,"name":"Detect","created_at":"2011-01-11T08:57:06Z","strength":81,"cost":10,"updated_at":"2011-04-28T18:10:48Z","id":7,"initial_stock":10,"inventory_icon_id":26}},{"counter":{"level_id":10,"name":"Healer","created_at":"2011-01-11T10:49:49Z","strength":81,"cost":10,"updated_at":"2011-04-28T18:10:52Z","id":8,"initial_stock":5,"inventory_icon_id":34}},{"counter":{"level_id":11,"name":"Counter","created_at":"2011-01-11T10:58:24Z","strength":101,"cost":25,"updated_at":"2011-04-28T18:10:55Z","id":14,"initial_stock":10,"inventory_icon_id":28}},{"counter":{"level_id":11,"name":"Return","created_at":"2011-01-11T08:54:57Z","strength":151,"cost":50,"updated_at":"2011-04-28T18:10:57Z","id":5,"initial_stock":5,"inventory_icon_id":20}}]
    42. 42. Write your own .to_json 1k byte> puts Counter.all.map(&:to_json).to_json[{"name":"Mend","strength":11,"id":12,"icon":"/system/pictures/21/large/Glass-2.png?1299820062"},{"name":"Radiate","strength":11,"id":13,"icon":"/system/pictures/10/large/potion-1.png?1299786958"},{"name":"Float","strength":31,"id":10,"icon":"/system/pictures/31/large/Untitled-1.png?1299820281"},{"name":"Shine","strength":31,"id":11,"icon":"/system/pictures/12/large/point-3.png?1299787038"},{"name":"Pure","strength":51,"id":9,"icon":"/system/pictures/30/large/Umbrella-3.png?1299820262"},{"name":"Bounce","strength":51,"id":1,"icon":"/system/pictures/8/large/shield-2.png?1299786772"},{"name":"Reflect","strength":71,"id":6,"icon":"/system/pictures/9/large/shield-3.png?1299786787"},{"name":"Detect","strength":81,"id":7,"icon":"/system/pictures/26/large/Shield-1.png?1299820172"},{"name":"Healer","strength":81,"id":8,"icon":"/system/pictures/34/large/Vest-3.png?1299820340"},{"name":"Counter","strength":101,"id":14,"icon":"/system/pictures/28/large/Shield-3.png?1299820222"},{"name":"Return","strength":151,"id":5,"icon":"/system/pictures/20/large/Glass-1.png?1299820042"}] By writing your own to_json method, you not only optimize the size of the body but also you can be much more specific and prepare your own data
    43. 43. Push Notification
    44. 44. Push Notification Apple Push Rails Notificationapplication Services
    45. 45. Push Notification
    46. 46. Push Notification
    47. 47. Push Notification
    48. 48. Push Notification08f9e3f8 36b6e364 f4e80310 fe3068ef dba588d2 1b19102c 0e652002 6ca9a785
    49. 49. Push Notification• Push Notification Token identifies one device, not one user• Send it to your Rails backend and save it to your data storage
    50. 50. Push Notification• Rails plugin: apn_on_rails• http://www.github.com/PRX/apn_on_rails• Rails3 branch
    51. 51. Push NotificationThis saves the message to your database
    52. 52. Push NotificationEvery ten minutes.. or every minute..
    53. 53. Push Notification• Rake task triggered by crontab• Resque scheduler• Ifyou want to schedule a Rake task every minute or even every 5 minutes, you will consume a lot of resources on your server to boot up the Rails environment.• Be careful about 256 bytes size limit!
    54. 54. Resque Scheduler
    55. 55. Thank you• maxime@ekohe.com• Thanksto maverick@ekohe.com for his contribution on the JSONRequest / Model ORM• Thanks to jimmy@ekohe.com for writing an awesome backend for Breeze Living
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×