Rails as iOS Application Backend
Upcoming SlideShare
Loading in...5
×
 

Rails as iOS Application Backend

on

  • 24,507 views

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

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

Statistics

Views

Total Views
24,507
Views on SlideShare
24,424
Embed Views
83

Actions

Likes
68
Downloads
403
Comments
4

9 Embeds 83

https://twitter.com 46
http://goingon.in 17
http://redwatch.posterous.com 8
http://paper.li 3
http://seekr-artemis.heroku.com 3
http://a0.twimg.com 2
https://si0.twimg.com 2
https://twimg0-a.akamaihd.net 1
http://localhost 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \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 Rails as iOS Application Backend Presentation Transcript

  • 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 web application - GPS, on-the-go, etc.• Push notifications brings retention
  • 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 REST
  • ArchitectureDatabase Rails application3rd party APIs JSON REST over HTTP(s)
  • ArchitectureDatabase Rails application3rd party APIs Apple Push Notification Services
  • 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 iPhone• Good news for your Railer: nothing new!
  • 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!
  • 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!
  • 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!
  • AuthenticationExample using inherit_resources and authlogic
  • iOS side Application code• Not as easy.• NSURLConnection is JSONRequest low level NSURLConnection• JSONRequest provides you a higher level iOS Network Socket interface
  • JSONRequest ➊ Initiate HTTP request to /inventory➋ Handle networking, cookies, redirections and errors ➌ Parse HTTP Response as JSON
  • 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 {status: “ok”}
  • 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 Network Socket
  • https://github.com/maxime/iorm Application code ORM JSONRequest NSURLConnection iOS Network Socket
  • Tips• Why not using the built-in .to_json ?
  • 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}}]
  • 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
  • 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 to your data storage
  • 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 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!
  • Resque Scheduler
  • 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