A Solid Backend for a Solid Mobile Application

Preview:

DESCRIPTION

Nowadays many mobile applications are relying more and more on server-side applications. Setting up servers and deploying a server-side application can be hard and time-consuming, but it has not to be. Qt Cloud Services contains three different services that can be utilised to implement a robust and scalable server-side solution easily. Managed Application Runtime (MAR) is used to deploy and run applications on the cloud, Enginio Data Stroage (EDS) can be used to store data and Managed WebSocket (MWS) service makes it possible to send and receive WebSocket messages. By combining these three services it is possible to build and deploy a server-side solution without configuring any server machine. In this presentation an example Qt mobile application demonstrates how to implement a real-time Twitter stream provided by a server-side application that is running on MAR. The server-side application stores tweets to EDS and sync new tweets to the client via WebSocket protocol.

Citation preview

© 2013 The Qt Company

A Solid Back End for a Solid Qt Mobile Application

© 2014 The Qt Company

About Me

© 2014 The Qt Company

Lauri Nevala

• Over ten years of experience in creating web and mobile based applications

• Working in Qt Cloud Services team at The Qt Company

nevalau nevalla nevalla

© 2014 The Qt Company

Awesome mobile applications…

© 2014 The Qt Company

…and they all need back-end systems!

© 2014 The Qt Company

How do you implement your back end solution?

© 2014 The Qt Company

You have an idea

BUY AND SETUP HARDWARE

INSTALL OS

DEFINE ACCESSCONTROL

SETUP ANALYTICS

INSTALL SOFTWARE

BUILD

JUST START BUILDING

APP APP

X

© 2014 The Qt Company

PaaSPlatform as a Serv ice

© 2014 The Qt Company

What is PaaS?Applica'on*

Data*

Run'me*

Middleware*

OS*

Virtualiza'on*

Servers*

Storage*

Networking*

On#Premises#Yo

u*Manage*

Applica'on*

Data*

Run'me*

Middleware*

OS*

Virtualiza'on*

Servers*

Storage*

Networking*

IaaS$

You*Manage*

Vend

or*Provide

s*

Applica'on*

Data*

Run'me*

Middleware*

OS*

Virtualiza'on*

Servers*

Storage*

Networking*

PaaS$

You*Manage*

Vend

or*Provide

s*

Applica'on*

Data*

Run'me*

Middleware*

OS*

Virtualiza'on*

Servers*

Storage*

Networking*

SaaS#

Vend

or*Provide

s*

With PaaS, developer can focus just on building great apps. No need to worry about servers, operating system, loadbalancing, network or infrastructure.

© 2014 The Qt Company

Illustra(on  by  Rouge  Group  

PaaS

© 2014 The Qt Company

– Yefim Natis, Gartner

“We believe PaaS will enable organizations become more agile and responsive”

“Organizations who are late, don't adopt PaaS will fall behind”

© 2014 The Qt Company

Modern Application Development

1. Mobile First Approach - Factoring applications into many apps.

2. Cloud-Backed - Adopting more abstract and machine-independent application models for back ends in the form of PaaS.

3. Agile Teams - Rapid iterations, frequent releases, integration of users

4. Continuously Integrated and Delivered - No downtimes

5. DevOps-Enabled - Developers own deployments

6. User Experience-Centric - Focusing on the design and appearance of the application

7. Service Oriented Architecture - Use of micro services to compose application

8. Lightweight - Less complex software that is less time-consuming to install, learn and use

9. Socially Oriented - UX patterns from social networks (timelines, event streams etc), Data is updated using event-based push-oriented patterns. Integrated search functionality.

10.API Factored and Surfaced - Componentize and granularize back-end software in order to achieve composable and easy-to-evolve back-end platforms.

© 2014 The Qt Company

© 2014 The Qt Company

Managed Application Runtime (MAR)

Scalable, Multi-language, Multi-database,

Application Platform as a Service

© 2014 The Qt Company

Supported Frameworks

Supported frameworks by 3rd party build packs

Scala, Clojure, Play, Gradle, Grails, PHP, Go, Meteorite, Perl, Dart, Nginx, Apache, Jekyll

© 2014 The Qt Company

Built-In Services

© 2014 The Qt Company

Add Ons

Enginio Data Storage (EDS)

Managed WebSocket (MWS)

© 2014 The Qt Company

Enginio Data Storage (EDS)

Flexible and powerful cloud data storage

with built-in user and data access control

© 2014 The Qt Company

Managed WebSocket (MWS)

Fully managed service implementing a bi-

directional, real-time communication gateway

for WebSockets.

© 2014 The Qt Company

Developer Friendly Deployment

Deploy using Git – the most common VCS among developers

>  git  push  qtc  master

© 2014 The Qt Company

Scalable Architecture

Red

isM

ongo

DB

Mon

goD

B

Your Application Built-­‐InServices

Applica2on  Instances End Users

Configure

Deploy

Scale With Your Business

Git

Push

API

Red

isM

ySQ

L

Envi

ronm

ent V

aria

bles

Load

Bal

ance

r

myawesome.com

Scaling

© 2014 The Qt Company

Web Console

© 2014 The Qt Company

Command Line Tool

© 2014 The Qt Company

I Have an Idea

$$$

© 2014 The Qt Company

Mobile application that shows tweets in real

time

© 2014 The Qt Company

Back End

• DB where to store tweets

• Worker that retrieves/receives tweets from Twitter

• API that offers tweets to client app

• WebSocket server that push new tweets to client app

© 2014 The Qt Company

• Enginio Data Storage

+ Schemaless

+ REST API

Database

© 2014 The Qt Company

WebSocket Server• Managed WebSocket

+ Easy to start

+ REST API

© 2014 The Qt Company

Worker + API• Managed Application Runtime

+ PaaS

+ Just build the app

© 2014 The Qt Company

1. get initial tweets

2. new tweet

3. save tweet

4. send WebSocket message

© 2014 The Qt Company

Worker - Rubyrequire  'tweetstream'  require  'qtc-­‐sdk'  !TweetStream.configure  do  |config|      config.consumer_key              =  ENV['CONSUMER_KEY']      config.consumer_secret        =  ENV['CONSUMER_SECRET']      config.oauth_token                =  ENV['OAUTH_TOKEN']      config.oauth_token_secret  =  ENV['OAUTH_SECRET']      config.auth_method                =  :oauth  end

eds  =  Qtc::Eds::Client.new(ENV['EDS_BACKEND_ID'])  mws  =  Qtc::Mws::Client.new(ENV['MWS_GATEWAY_ID'],  {access_token:  ENV['MWS_SECURITY_TOKEN']})  tweets  =  eds.collection('tweets')

daemon  =  TweetStream::Daemon.new('tracker',  :multiple  =>  true,  :no_pidfiles  =>  true)  daemon.track('#QtDD14')  do  |status|      if  !status.text.start_with?('RT  @')          tweet  =  {              created:  status.created_at,              tweet_id:  status.id,              text:  status.text,              name:  status.user.name,              screen_name:  status.user.screen_name,              profile_img_url:  status.user.profile_image_uri,              hashtags:  status.hashtags.map{|h|  h.text.downcase  }          }          tweets.insert(tweet)          mws.send_message(tweet.to_json,  {sockets:  nil,  tags:  ['QtDD14']})      end  end

© 2014 The Qt Company

API - Ruby / Sinatrarequire  'sinatra'  require  'qtc-­‐sdk'  !set  :port,  ENV['PORT']

get  '/tweets/:hashtag'  do      eds  =  Qtc::Eds::Client.new(ENV['EDS_BACKEND_ID'])      tweets  =  eds.collection('tweets')      query  =  {          :q  =>  {          'hashtags'  =>  {'$in'  =>  [params[:hashtag]]              }          },          :limit  =>  50,          :sort  =>  [{"sortBy"  =>  "createdAt","direction"  =>  "desc"}]      }      content_type  :json,  'charset'  =>  'utf-­‐8'      result  =  tweets.find(query)      result.to_json  end

get  '/websocket_uri'  do      mws  =  Qtc::Mws::Client.new(ENV['MWS_GATEWAY_ID'],  {access_token:  ENV['MWS_SECURITY_TOKEN']})      socket  =  mws.create_socket(["QtDD14"])      content_type  :json,'charset'  =>  'utf-­‐8'      socket.to_json  end

© 2014 The Qt Company

Procfile

• When using most common frameworks, MAR can automatically detect the executable.

• You can explicitly declare what can be executed in a Procfile.

• Each line declares a process type - a command that can be executed from your built application

worker:  ruby  ./worker.rb  run  web:  rackup  -­‐p  $PORT  -­‐E  production

© 2014 The Qt Company

> git push qtc master

© 2014 The Qt Company

lanevala@it-­‐l-­‐m0015  ~/projects/qtdd-­‐twitter-­‐stream[master*]$  git  push  qtc  master  Counting  objects:  9,  done.  Delta  compression  using  up  to  8  threads.  Compressing  objects:  100%  (8/8),  done.  Writing  objects:  100%  (9/9),  1.46  KiB  |  0  bytes/s,  done.  Total  9  (delta  6),  reused  0  (delta  0)  -­‐-­‐-­‐-­‐-­‐>  Ruby  app  detected  -­‐-­‐-­‐-­‐-­‐>  Compiling  Ruby/Rack  -­‐-­‐-­‐-­‐-­‐>  Using  Ruby  version:  ruby-­‐2.1.2  -­‐-­‐-­‐-­‐-­‐>  Installing  dependencies  using  1.6.3                Running:  bundle  install  -­‐-­‐without  development:test  -­‐-­‐path  vendor/bundle  -­‐-­‐binstubs  vendor/bundle/bin  -­‐j4  -­‐-­‐deployment                Fetching  gem  metadata  from  https://rubygems.org/........                Using  eventmachine  1.0.3                Using  json  1.8.1                Using  thread_safe  0.3.4                Using  multi_json  1.10.1                Using  naught  1.0.0                Using  rack  1.5.2                Using  bundler  1.6.3                Using  em-­‐socksify  0.3.0                Using  http  0.6.1                Using  faraday  0.9.0                Using  em-­‐twitter  0.3.3                Installing  httpclient  2.5.2                Installing  inifile  3.0.0                Using  memoizable  0.4.2                Using  puma  2.9.1                Using  rails_12factor  0.0.3                Using  em-­‐http-­‐request  1.1.2                Using  twitter  5.11.0                Using  sinatra  1.4.5                Using  tweetstream  2.6.1                Installing  faye-­‐websocket  0.7.5                Using  qtc-­‐sdk  0.0.5  from  https://github.com/nevalla/qtc-­‐sdk-­‐ruby.git  (at  master)                Your  bundle  is  complete!                Gems  in  the  groups  development  and  test  were  not  installed.                It  was  installed  into  ./vendor/bundle                Bundle  completed  (9.04s)                Cleaning  up  the  bundler  cache.                Removing  httpclient  (2.4.0)                Removing  qtc-­‐sdk-­‐ruby  (8e7a9e3858e8)                  -­‐-­‐-­‐-­‐-­‐>  Discovering  process  types                Procfile  declares  types  -­‐>  worker,  web                Default  process  types  for  Ruby  -­‐>  rake,  console,  web  -­‐-­‐-­‐-­‐-­‐>  Compiled  slug  size  is  17M  -­‐-­‐-­‐-­‐-­‐>  Deploying  app                Uploading  app  container  ..  done.                mar-­‐eu-­‐1-­‐13viqrf8  deployed  to  http://mar-­‐eu-­‐1-­‐13viqrf8.qtcloudapp.com  To  git@git-­‐mar-­‐eu-­‐1.qtc.io:mar-­‐eu-­‐1-­‐13viqrf8        36e9190..b237ec8    master  -­‐>  master  lanevala@it-­‐l-­‐m0015  ~/projects/qtdd-­‐twitter-­‐stream[master*]$

© 2014 The Qt Company

Demo

© 2014 The Qt Company

Buildpacks

• When you deploy your Application, MAR slug compiler uses buildpack to prepares your code for execution.

• Each programming language and framework support is implemented as buildpack.

• Buildpack script will build the runtime environment on top of the Stack Image, a mini Linux distribution, and install everything that is needed.

© 2014 The Qt Company

• Stack Image is a mini Linux distribution which defines the core operating system and runtime environment. MAR Stack Images are based on following Linux distributions:

• Lucid based on Ubuntu 10.04 LTS Lucid Lynx

• Trusty based on Ubuntu 14.04 LTS Trusty Tahr will be available soon!

© 2014 The Qt Company

How to scale the application?

• MAR application processes can be scaled up or down dynamically.

• A typical web application has at least web process type declared in Procfile.

• You can set wanted concurrency level for process by issuing scale command using qtc-cli:

© 2014 The Qt Company

$ qtc-cli mar scale web=2

$ qtc-cli mar scale worker=4

$ qtc-cli mar scale worker=0

© 2014 The Qt Company

Configuration Variables

• When developing server-side applications, it is very often required to have a configuration for application that is varying between environments. For example staging, production, testing, etc.

• Configuration to some external systems like database addresses, credentials or other environment variables might be required.

© 2014 The Qt Company

• This configuration data is not included in your application code and can be changed independently.

• The configuration is exposed to a running application via environment variables.

• You can manage environment variables using the qtc-cli command line tool:

• qtc-cli mar envs:set SECRET_CODE=donotsharethis

© 2014 The Qt Company

Ports

Applicationinstances Load Balancer Client

ApplicationGET your-mar-address/index.html:80GET /index.html:47435

GET /index.html:47487

© 2014 The Qt Company

Logging

• Logs are a stream of time-ordered events aggregated from the output streams of all your application instance processes.

• MAR logger routes log streams from all these sources into single stream.

• Anything written to standard out (stdout) or standard error (stderr) is captured into application instance logs.

© 2014 The Qt Company

• Qt/C++: qDebug() << "Log me to stdout”;

• QML & Node.js: console.log("Log me to stdout”);

• Ruby: puts "Log me to stdout”

!

• $ qtc-cli mar logs

© 2014 The Qt Company

Qt Backend

• Qt support is activated when application has one or more qmake project files in the root folder.

• If you set QMAKE_ARGS environment variable it will be passed to qmake command

• You must specify web process manually in Procfile:

• web: ./hello-world/server --port $PORT

© 2014 The Qt Company

Supported Qt Versions• New applications will default to the latest stable version of Qt (currently

5.3.2).

• You can specify any supported Qt runtime version to be used to run your application. This functionality is enabled when your project has .qt_runtime file.

• Available Qt runtimes:

• qt-5.3.0

• qt-5.3.1

• qt-5.3.2

• qt-5.4.0-beta

© 2014 The Qt Company

© 2014 The Qt Company

Further Actions

• Visit qtcloudservices.com

• Sign-up

• Use it

• Give us feedback

• Qt forum

• info@qtcloudservices.com

© 2014 The Qt Company

Thank you!

http://www.qtcloudservices.com

Recommended