SlideShare a Scribd company logo
1 of 55
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 make
an API for the iPhone / iPad or
Architecture
Architecture
Architecture
Architecture


   Rails
application
Architecture

Database
               Rails
            application
3rd party
  APIs
Architecture

Database
               Rails
            application
3rd party
  APIs                    JSON
Architecture

Database
               Rails
            application
3rd party
  APIs                    JSON
                          REST
Architecture

Database
               Rails
            application
3rd party
  APIs                       JSON
                             REST
                          over HTTP(s)
Architecture

Database
               Rails
            application
3rd 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!
Authentication




Example 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
JSONRequest




Your 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 lightweight
ORM 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 it




You 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
                 Notification
application
                  Services
Push Notification
Push Notification
Push Notification
Push Notification




08f9e3f8 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 Notification




This saves the message to your database
Push Notification




Every 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

More Related Content

What's hot

Combining R With Java For Data Analysis (Devoxx UK 2015 Session)
Combining R With Java For Data Analysis (Devoxx UK 2015 Session)Combining R With Java For Data Analysis (Devoxx UK 2015 Session)
Combining R With Java For Data Analysis (Devoxx UK 2015 Session)Ryan Cuprak
 
Containerless in the Cloud with AWS Lambda
Containerless in the Cloud with AWS LambdaContainerless in the Cloud with AWS Lambda
Containerless in the Cloud with AWS LambdaRyan Cuprak
 
React Native Firebase Realtime Database + Authentication
React Native Firebase Realtime Database + AuthenticationReact Native Firebase Realtime Database + Authentication
React Native Firebase Realtime Database + AuthenticationKobkrit Viriyayudhakorn
 
Cross-Platform Desktop Apps with Electron (JSConf UY)
Cross-Platform Desktop Apps with Electron (JSConf UY)Cross-Platform Desktop Apps with Electron (JSConf UY)
Cross-Platform Desktop Apps with Electron (JSConf UY)David Neal
 
ADF Mobile: 10 Things you don't get from the developers guide
ADF Mobile: 10 Things you don't get from the developers guideADF Mobile: 10 Things you don't get from the developers guide
ADF Mobile: 10 Things you don't get from the developers guideLuc Bors
 
Riding the Edge with Ember.js
Riding the Edge with Ember.jsRiding the Edge with Ember.js
Riding the Edge with Ember.jsaortbals
 
Creating an Effective Mobile API
Creating an Effective Mobile API Creating an Effective Mobile API
Creating an Effective Mobile API Nick DeNardis
 
Building Awesome APIs in Grails
Building Awesome APIs in GrailsBuilding Awesome APIs in Grails
Building Awesome APIs in Grailsclatimer
 
Open Source Mobile Backend on Cassandra
Open Source Mobile Backend on CassandraOpen Source Mobile Backend on Cassandra
Open Source Mobile Backend on CassandraEd Anuff
 
Usergrid Overview
Usergrid OverviewUsergrid Overview
Usergrid Overviewusergrid
 
Oracle MAF real life OOW.pptx
Oracle MAF real life OOW.pptxOracle MAF real life OOW.pptx
Oracle MAF real life OOW.pptxLuc Bors
 
What is App Engine? O
What is App Engine? OWhat is App Engine? O
What is App Engine? Oikailan
 
Rapid API Development with LoopBack/StrongLoop
Rapid API Development with LoopBack/StrongLoopRapid API Development with LoopBack/StrongLoop
Rapid API Development with LoopBack/StrongLoopRaymond Camden
 
Lambdaless and AWS CDK
Lambdaless and AWS CDKLambdaless and AWS CDK
Lambdaless and AWS CDKMooYeol Lee
 
Xamarin.Forms Bootcamp
Xamarin.Forms BootcampXamarin.Forms Bootcamp
Xamarin.Forms BootcampMike Melusky
 
Cross Platform Mobile Apps with the Ionic Framework
Cross Platform Mobile Apps with the Ionic FrameworkCross Platform Mobile Apps with the Ionic Framework
Cross Platform Mobile Apps with the Ionic FrameworkTroy Miles
 
 Active Storage - Modern File Storage? 
 Active Storage - Modern File Storage?  Active Storage - Modern File Storage? 
 Active Storage - Modern File Storage? Michael Yagudaev
 
Building a REST Service in minutes with Spring Boot
Building a REST Service in minutes with Spring BootBuilding a REST Service in minutes with Spring Boot
Building a REST Service in minutes with Spring BootOmri Spector
 
Getting started with rails active storage wae
Getting started with rails active storage waeGetting started with rails active storage wae
Getting started with rails active storage waeBishal Khanal
 

What's hot (20)

Combining R With Java For Data Analysis (Devoxx UK 2015 Session)
Combining R With Java For Data Analysis (Devoxx UK 2015 Session)Combining R With Java For Data Analysis (Devoxx UK 2015 Session)
Combining R With Java For Data Analysis (Devoxx UK 2015 Session)
 
Containerless in the Cloud with AWS Lambda
Containerless in the Cloud with AWS LambdaContainerless in the Cloud with AWS Lambda
Containerless in the Cloud with AWS Lambda
 
React Native Firebase Realtime Database + Authentication
React Native Firebase Realtime Database + AuthenticationReact Native Firebase Realtime Database + Authentication
React Native Firebase Realtime Database + Authentication
 
Cross-Platform Desktop Apps with Electron (JSConf UY)
Cross-Platform Desktop Apps with Electron (JSConf UY)Cross-Platform Desktop Apps with Electron (JSConf UY)
Cross-Platform Desktop Apps with Electron (JSConf UY)
 
ADF Mobile: 10 Things you don't get from the developers guide
ADF Mobile: 10 Things you don't get from the developers guideADF Mobile: 10 Things you don't get from the developers guide
ADF Mobile: 10 Things you don't get from the developers guide
 
Riding the Edge with Ember.js
Riding the Edge with Ember.jsRiding the Edge with Ember.js
Riding the Edge with Ember.js
 
Creating an Effective Mobile API
Creating an Effective Mobile API Creating an Effective Mobile API
Creating an Effective Mobile API
 
Building Awesome APIs in Grails
Building Awesome APIs in GrailsBuilding Awesome APIs in Grails
Building Awesome APIs in Grails
 
Docker and java
Docker and javaDocker and java
Docker and java
 
Open Source Mobile Backend on Cassandra
Open Source Mobile Backend on CassandraOpen Source Mobile Backend on Cassandra
Open Source Mobile Backend on Cassandra
 
Usergrid Overview
Usergrid OverviewUsergrid Overview
Usergrid Overview
 
Oracle MAF real life OOW.pptx
Oracle MAF real life OOW.pptxOracle MAF real life OOW.pptx
Oracle MAF real life OOW.pptx
 
What is App Engine? O
What is App Engine? OWhat is App Engine? O
What is App Engine? O
 
Rapid API Development with LoopBack/StrongLoop
Rapid API Development with LoopBack/StrongLoopRapid API Development with LoopBack/StrongLoop
Rapid API Development with LoopBack/StrongLoop
 
Lambdaless and AWS CDK
Lambdaless and AWS CDKLambdaless and AWS CDK
Lambdaless and AWS CDK
 
Xamarin.Forms Bootcamp
Xamarin.Forms BootcampXamarin.Forms Bootcamp
Xamarin.Forms Bootcamp
 
Cross Platform Mobile Apps with the Ionic Framework
Cross Platform Mobile Apps with the Ionic FrameworkCross Platform Mobile Apps with the Ionic Framework
Cross Platform Mobile Apps with the Ionic Framework
 
 Active Storage - Modern File Storage? 
 Active Storage - Modern File Storage?  Active Storage - Modern File Storage? 
 Active Storage - Modern File Storage? 
 
Building a REST Service in minutes with Spring Boot
Building a REST Service in minutes with Spring BootBuilding a REST Service in minutes with Spring Boot
Building a REST Service in minutes with Spring Boot
 
Getting started with rails active storage wae
Getting started with rails active storage waeGetting started with rails active storage wae
Getting started with rails active storage wae
 

Viewers also liked

Iphone client-server app with Rails backend (v3)
Iphone client-server app with Rails backend (v3)Iphone client-server app with Rails backend (v3)
Iphone client-server app with Rails backend (v3)Sujee Maniyam
 
Chief Data Officer: 6 Ideas for Data Innovation
Chief Data Officer: 6 Ideas for Data Innovation Chief Data Officer: 6 Ideas for Data Innovation
Chief Data Officer: 6 Ideas for Data Innovation Craig Milroy
 
The Chief Data Officer and the Organizational Journey
The Chief Data Officer and the Organizational JourneyThe Chief Data Officer and the Organizational Journey
The Chief Data Officer and the Organizational JourneyCraig Milroy
 
Chief Data Officer: Top Ten Learnings...
Chief Data Officer: Top Ten Learnings...Chief Data Officer: Top Ten Learnings...
Chief Data Officer: Top Ten Learnings...Craig Milroy
 
А.Левенчук -- смычка кортекса и экзокортекса
А.Левенчук -- смычка кортекса и экзокортексаА.Левенчук -- смычка кортекса и экзокортекса
А.Левенчук -- смычка кортекса и экзокортексаAnatoly Levenchuk
 
Alignment: Office of the Chief Data Officer & BCBS 239
Alignment: Office of the Chief Data Officer & BCBS 239Alignment: Office of the Chief Data Officer & BCBS 239
Alignment: Office of the Chief Data Officer & BCBS 239Craig Milroy
 
Chief Data Officer: Evolution to the Chief Analytics Officer and Data Science
Chief Data Officer: Evolution to the Chief Analytics Officer and Data ScienceChief Data Officer: Evolution to the Chief Analytics Officer and Data Science
Chief Data Officer: Evolution to the Chief Analytics Officer and Data ScienceCraig Milroy
 
Трекшн карта и проблемное интервью
Трекшн карта и проблемное интервьюТрекшн карта и проблемное интервью
Трекшн карта и проблемное интервьюEugene Kalinin
 
Talking to your CEO about the Chief Data Officer Role
Talking to your CEO about the Chief Data Officer Role Talking to your CEO about the Chief Data Officer Role
Talking to your CEO about the Chief Data Officer Role Craig Milroy
 
Chief Data Officer: Overcoming Data Silos for True Business Value
Chief Data Officer: Overcoming Data Silos for True Business ValueChief Data Officer: Overcoming Data Silos for True Business Value
Chief Data Officer: Overcoming Data Silos for True Business ValueCraig Milroy
 
Chief Data Officer: DataOps - Transformation of the Business Data Environment
Chief Data Officer: DataOps - Transformation of the Business Data EnvironmentChief Data Officer: DataOps - Transformation of the Business Data Environment
Chief Data Officer: DataOps - Transformation of the Business Data EnvironmentCraig Milroy
 
Data strategy in a Big Data world
Data strategy in a Big Data worldData strategy in a Big Data world
Data strategy in a Big Data worldCraig Milroy
 
Chief Data Architect or Chief Data Officer: Connecting the Enterprise Data Ec...
Chief Data Architect or Chief Data Officer: Connecting the Enterprise Data Ec...Chief Data Architect or Chief Data Officer: Connecting the Enterprise Data Ec...
Chief Data Architect or Chief Data Officer: Connecting the Enterprise Data Ec...Craig Milroy
 
Overview of IBM Watson Services via Blue Mix
Overview of IBM Watson Services via Blue Mix Overview of IBM Watson Services via Blue Mix
Overview of IBM Watson Services via Blue Mix Craig Milroy
 
Overview of Microsoft Azure AI Services
Overview of Microsoft Azure AI ServicesOverview of Microsoft Azure AI Services
Overview of Microsoft Azure AI ServicesCraig Milroy
 
История IT-Agency
История IT-AgencyИстория IT-Agency
История IT-AgencyIT-Agency
 
Psdot 19 four factor password authentication
Psdot 19 four factor password authenticationPsdot 19 four factor password authentication
Psdot 19 four factor password authenticationZTech Proje
 
Kerberos Authentication Process In Windows
Kerberos Authentication Process In WindowsKerberos Authentication Process In Windows
Kerberos Authentication Process In Windowsniteshitimpulse
 
E authentication template 050212
E authentication template 050212E authentication template 050212
E authentication template 050212GovCloud Network
 

Viewers also liked (20)

Iphone client-server app with Rails backend (v3)
Iphone client-server app with Rails backend (v3)Iphone client-server app with Rails backend (v3)
Iphone client-server app with Rails backend (v3)
 
Chief Data Officer: 6 Ideas for Data Innovation
Chief Data Officer: 6 Ideas for Data Innovation Chief Data Officer: 6 Ideas for Data Innovation
Chief Data Officer: 6 Ideas for Data Innovation
 
The Chief Data Officer and the Organizational Journey
The Chief Data Officer and the Organizational JourneyThe Chief Data Officer and the Organizational Journey
The Chief Data Officer and the Organizational Journey
 
Chief Data Officer: Top Ten Learnings...
Chief Data Officer: Top Ten Learnings...Chief Data Officer: Top Ten Learnings...
Chief Data Officer: Top Ten Learnings...
 
А.Левенчук -- смычка кортекса и экзокортекса
А.Левенчук -- смычка кортекса и экзокортексаА.Левенчук -- смычка кортекса и экзокортекса
А.Левенчук -- смычка кортекса и экзокортекса
 
Alignment: Office of the Chief Data Officer & BCBS 239
Alignment: Office of the Chief Data Officer & BCBS 239Alignment: Office of the Chief Data Officer & BCBS 239
Alignment: Office of the Chief Data Officer & BCBS 239
 
Chief Data Officer: Evolution to the Chief Analytics Officer and Data Science
Chief Data Officer: Evolution to the Chief Analytics Officer and Data ScienceChief Data Officer: Evolution to the Chief Analytics Officer and Data Science
Chief Data Officer: Evolution to the Chief Analytics Officer and Data Science
 
Трекшн карта и проблемное интервью
Трекшн карта и проблемное интервьюТрекшн карта и проблемное интервью
Трекшн карта и проблемное интервью
 
Talking to your CEO about the Chief Data Officer Role
Talking to your CEO about the Chief Data Officer Role Talking to your CEO about the Chief Data Officer Role
Talking to your CEO about the Chief Data Officer Role
 
Chief Data Officer: Overcoming Data Silos for True Business Value
Chief Data Officer: Overcoming Data Silos for True Business ValueChief Data Officer: Overcoming Data Silos for True Business Value
Chief Data Officer: Overcoming Data Silos for True Business Value
 
Chief Data Officer: DataOps - Transformation of the Business Data Environment
Chief Data Officer: DataOps - Transformation of the Business Data EnvironmentChief Data Officer: DataOps - Transformation of the Business Data Environment
Chief Data Officer: DataOps - Transformation of the Business Data Environment
 
Data strategy in a Big Data world
Data strategy in a Big Data worldData strategy in a Big Data world
Data strategy in a Big Data world
 
Chief Data Architect or Chief Data Officer: Connecting the Enterprise Data Ec...
Chief Data Architect or Chief Data Officer: Connecting the Enterprise Data Ec...Chief Data Architect or Chief Data Officer: Connecting the Enterprise Data Ec...
Chief Data Architect or Chief Data Officer: Connecting the Enterprise Data Ec...
 
Overview of IBM Watson Services via Blue Mix
Overview of IBM Watson Services via Blue Mix Overview of IBM Watson Services via Blue Mix
Overview of IBM Watson Services via Blue Mix
 
Overview of Microsoft Azure AI Services
Overview of Microsoft Azure AI ServicesOverview of Microsoft Azure AI Services
Overview of Microsoft Azure AI Services
 
История IT-Agency
История IT-AgencyИстория IT-Agency
История IT-Agency
 
Psdot 19 four factor password authentication
Psdot 19 four factor password authenticationPsdot 19 four factor password authentication
Psdot 19 four factor password authentication
 
Kerberos Authentication Process In Windows
Kerberos Authentication Process In WindowsKerberos Authentication Process In Windows
Kerberos Authentication Process In Windows
 
SCWCD : Secure web
SCWCD : Secure webSCWCD : Secure web
SCWCD : Secure web
 
E authentication template 050212
E authentication template 050212E authentication template 050212
E authentication template 050212
 

Similar to Rails as iOS Application Backend

Iot meets Serverless
Iot meets ServerlessIot meets Serverless
Iot meets ServerlessNarendran R
 
System insight without Interference
System insight without InterferenceSystem insight without Interference
System insight without InterferenceTony Tam
 
Hdc09 I Phone Dev Connecting To Web
Hdc09   I Phone Dev Connecting To WebHdc09   I Phone Dev Connecting To Web
Hdc09 I Phone Dev Connecting To WebAndy Peters
 
Introduction to Realm Mobile Platform
Introduction to Realm Mobile PlatformIntroduction to Realm Mobile Platform
Introduction to Realm Mobile PlatformChristian Melchior
 
Laying the Foundation for Ionic Platform Insights on Spark
Laying the Foundation for Ionic Platform Insights on SparkLaying the Foundation for Ionic Platform Insights on Spark
Laying the Foundation for Ionic Platform Insights on SparkIonic Security
 
Design & Deploy a data-driven Web API in 2 hours
Design & Deploy a data-driven Web API in 2 hoursDesign & Deploy a data-driven Web API in 2 hours
Design & Deploy a data-driven Web API in 2 hoursRestlet
 
Social Photos - My presentation at Microsoft Tech Day
Social Photos - My presentation at Microsoft Tech DaySocial Photos - My presentation at Microsoft Tech Day
Social Photos - My presentation at Microsoft Tech DayTechMaster Vietnam
 
Build a Website on AWS for Your First 10 Million Users
Build a Website on AWS for Your First 10 Million UsersBuild a Website on AWS for Your First 10 Million Users
Build a Website on AWS for Your First 10 Million UsersAmazon Web Services
 
(MBL314) Build World-Class Cloud-Connected Products: Sonos
(MBL314) Build World-Class Cloud-Connected Products: Sonos(MBL314) Build World-Class Cloud-Connected Products: Sonos
(MBL314) Build World-Class Cloud-Connected Products: SonosAmazon Web Services
 
Protecting Your APIs Against Attack & Hijack
Protecting Your APIs Against Attack & Hijack Protecting Your APIs Against Attack & Hijack
Protecting Your APIs Against Attack & Hijack CA API Management
 
KKBOX WWDC17 Security - Antony
KKBOX WWDC17 Security - AntonyKKBOX WWDC17 Security - Antony
KKBOX WWDC17 Security - AntonyLiyao Chen
 
Build an App on AWS for Your First 10 Million Users
Build an App on AWS for Your First 10 Million UsersBuild an App on AWS for Your First 10 Million Users
Build an App on AWS for Your First 10 Million UsersAmazon Web Services
 
Ibm_interconnect_restapi_workshop
Ibm_interconnect_restapi_workshopIbm_interconnect_restapi_workshop
Ibm_interconnect_restapi_workshopShubhra Kar
 
Build a Website on AWS for Your First 10 Million Users
Build a Website on AWS for Your First 10 Million UsersBuild a Website on AWS for Your First 10 Million Users
Build a Website on AWS for Your First 10 Million UsersAmazon Web Services
 
Journey Towards Scaling Your Application to 10 million users
Journey Towards Scaling Your Application to 10 million usersJourney Towards Scaling Your Application to 10 million users
Journey Towards Scaling Your Application to 10 million usersAmazon Web Services
 
Mobile application development; Complete steps
Mobile application development; Complete stepsMobile application development; Complete steps
Mobile application development; Complete steps Abin Baby
 
Building APIs in an easy way using API Platform
Building APIs in an easy way using API PlatformBuilding APIs in an easy way using API Platform
Building APIs in an easy way using API PlatformAntonio Peric-Mazar
 

Similar to Rails as iOS Application Backend (20)

Iot meets Serverless
Iot meets ServerlessIot meets Serverless
Iot meets Serverless
 
System insight without Interference
System insight without InterferenceSystem insight without Interference
System insight without Interference
 
Hdc09 I Phone Dev Connecting To Web
Hdc09   I Phone Dev Connecting To WebHdc09   I Phone Dev Connecting To Web
Hdc09 I Phone Dev Connecting To Web
 
Architecture at PBS
Architecture at PBSArchitecture at PBS
Architecture at PBS
 
Introduction to Realm Mobile Platform
Introduction to Realm Mobile PlatformIntroduction to Realm Mobile Platform
Introduction to Realm Mobile Platform
 
Laying the Foundation for Ionic Platform Insights on Spark
Laying the Foundation for Ionic Platform Insights on SparkLaying the Foundation for Ionic Platform Insights on Spark
Laying the Foundation for Ionic Platform Insights on Spark
 
Design & Deploy a data-driven Web API in 2 hours
Design & Deploy a data-driven Web API in 2 hoursDesign & Deploy a data-driven Web API in 2 hours
Design & Deploy a data-driven Web API in 2 hours
 
Social Photos - My presentation at Microsoft Tech Day
Social Photos - My presentation at Microsoft Tech DaySocial Photos - My presentation at Microsoft Tech Day
Social Photos - My presentation at Microsoft Tech Day
 
Revue des annonces WWDC2015
Revue des annonces WWDC2015Revue des annonces WWDC2015
Revue des annonces WWDC2015
 
Build a Website on AWS for Your First 10 Million Users
Build a Website on AWS for Your First 10 Million UsersBuild a Website on AWS for Your First 10 Million Users
Build a Website on AWS for Your First 10 Million Users
 
(MBL314) Build World-Class Cloud-Connected Products: Sonos
(MBL314) Build World-Class Cloud-Connected Products: Sonos(MBL314) Build World-Class Cloud-Connected Products: Sonos
(MBL314) Build World-Class Cloud-Connected Products: Sonos
 
Protecting Your APIs Against Attack & Hijack
Protecting Your APIs Against Attack & Hijack Protecting Your APIs Against Attack & Hijack
Protecting Your APIs Against Attack & Hijack
 
KKBOX WWDC17 Security - Antony
KKBOX WWDC17 Security - AntonyKKBOX WWDC17 Security - Antony
KKBOX WWDC17 Security - Antony
 
Build an App on AWS for Your First 10 Million Users
Build an App on AWS for Your First 10 Million UsersBuild an App on AWS for Your First 10 Million Users
Build an App on AWS for Your First 10 Million Users
 
Ibm_interconnect_restapi_workshop
Ibm_interconnect_restapi_workshopIbm_interconnect_restapi_workshop
Ibm_interconnect_restapi_workshop
 
Build a Website on AWS for Your First 10 Million Users
Build a Website on AWS for Your First 10 Million UsersBuild a Website on AWS for Your First 10 Million Users
Build a Website on AWS for Your First 10 Million Users
 
Journey Towards Scaling Your Application to 10 million users
Journey Towards Scaling Your Application to 10 million usersJourney Towards Scaling Your Application to 10 million users
Journey Towards Scaling Your Application to 10 million users
 
Varun-CV-J
Varun-CV-JVarun-CV-J
Varun-CV-J
 
Mobile application development; Complete steps
Mobile application development; Complete stepsMobile application development; Complete steps
Mobile application development; Complete steps
 
Building APIs in an easy way using API Platform
Building APIs in an easy way using API PlatformBuilding APIs in an easy way using API Platform
Building APIs in an easy way using API Platform
 

Rails as iOS Application Backend

  • 1. Rails as iOS Application Backend Shanghai On Rails – Saturday 21st, 2011
  • 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. As a Rails Developer, why do I care about mobile?
  • 7. You will probably have to make an API for the iPhone / iPad or
  • 11. Architecture Rails application
  • 12. Architecture Database Rails application 3rd party APIs
  • 13. Architecture Database Rails application 3rd party APIs JSON
  • 14. Architecture Database Rails application 3rd party APIs JSON REST
  • 15. Architecture Database Rails application 3rd party APIs JSON REST over HTTP(s)
  • 16. Architecture Database Rails application 3rd party APIs Apple Push Notification Services
  • 17. Challenges • Authentication and Persistence • Data format and optimization • Push notifications scheduling
  • 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. 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. 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. 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!
  • 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. JSONRequest ➊ Initiate HTTP request to /inventory ➋ Handle networking, cookies, redirections and errors ➌ Parse HTTP Response as JSON
  • 25. JSONRequest Your delegate receives one of those two calls
  • 26. JSONRequest • Youget a NSDictionary object easy to manipulate, you can view it has a the equivalent of a Ruby hash {status: “ok”}
  • 28. We created a lightweight ORM for the iOS platform
  • 32. Models - How to use it
  • 33. Models - How to use it /counters.json
  • 34. Models - How to use it /counters.json
  • 35. Models - How to use it /counters.json /counters.json?name=shield
  • 36. Models - How to use it You directly manipulate objects, not JSON objects
  • 38. https://github.com/maxime/iorm Application code JSONRequest NSURLConnection iOS Network Socket
  • 39. https://github.com/maxime/iorm Application code ORM JSONRequest NSURLConnection iOS Network Socket
  • 40. Tips • Why not using the built-in .to_json ?
  • 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. 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
  • 44. Push Notification Apple Push Rails Notification application Services
  • 48. Push Notification 08f9e3f8 36b6e364 f4e80310 fe3068ef dba588d2 1b19102c 0e652002 6ca9a785
  • 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. Push Notification • Rails plugin: apn_on_rails • http://www.github.com/PRX/apn_on_rails • Rails3 branch
  • 51. Push Notification This saves the message to your database
  • 52. Push Notification Every ten minutes.. or every minute..
  • 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!
  • 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

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n