Swift on the Server

Swift on the Server

Talk about Swift on the Server for Google Dev Fest Düsseldorf 2016

Fea6d57cccac4021b6c8acbfaa468965?s=128

Benedikt Terhechte

October 23, 2016
Tweet

Transcript

  1. DÜSSELDORF

  2. SWIFT? ON THE SERVER?

  3. ABOUT ME Benedikt Terhechte Indie Developer macOS & iOS @terhechte

    appventure.me
  4. SERVER DEVELOPMENT ➤ Worked many years as a backend developer

    ➤ Kept an active interest in server development afterwards ➤ Developed in / for ➤ Google App Engine ➤ Python + Django ➤ Scala + Scalatra ➤ Clojure ➤ Go + Revel ➤ Lua + Openresty ➤ PHP + Symfony
  5. SWIFT?

  6. None
  7. HISTORY ➤ Original Release (closed source) June 2014 ➤ Open

    Source (Apache 2.0) since December 2015 ➤ Current Version 3.0.1 ➤ New language for Apple’s complete ecosystem ➤ Spearheaded by Chris Lattner (LLVM, Clang)
  8. TARGETS ➤ Officially Fully Supported ➤ iOS, macOS, watchOS, tvOS

    ➤ Ubuntu 14.04, Ubuntu 15.10, Ubuntu 16.04 ➤ Unofficially Fully Supported ➤ Debian, Arch Linux, Fedora, CentOS ➤ FreeBSD 11.0 ➤ Partial Support ➤ Android ➤ Windows
  9. SWIFT, THE LANGUAGE

  10. MODERN FEATURES ➤ Value Types (Structs, Strings, Numbers, Arrays, Dictionaries)

    ➤ Reference Types (Classes) ➤ Optionals & Syntactic Sugar ➤ Strong Types & Generics ➤ Protocols (“Interfaces”) & Protocol Extensions (“Traits”) ➤ Composition & Inheritance ➤ Closures / Functions are types ➤ Pattern Matching ➤ Tuples
  11. MODERN FEATURES ➤ Automatic Retain Counting (No GC) ➤ Safety

    by default ➤ Algebraic Data Types (Enums with associated data) ➤ Full Unicode Support ➤ Error handling via Non-Unwinding Exceptions ➤ Functional Programming Constructs ➤ Comes with a REPL (Commandline) & Playgrounds (Xcode) ➤ Static & Dynamic Dispatch
  12. INTRODUCTION

  13. INTRODUCTION

  14. INTRODUCTION

  15. INTRODUCTION

  16. INTRODUCTION

  17. INTRODUCTION

  18. None
  19. None
  20. OPTIONALS

  21. GENERICS

  22. GENERICS

  23. PROTOCOL ORIENTED PROGRAMMING

  24. PROTOCOL ORIENTED PROGRAMMING

  25. PROTOCOL ORIENTED PROGRAMMING

  26. PROTOCOL ORIENTED PROGRAMMING

  27. ENUMS & PATTERNS

  28. ENUMS & PATTERNS

  29. ERROR HANDLING

  30. ERROR HANDLING

  31. FUTURE FEATURES (I.E. ON AGENDA FOR SWIFT 4 OR 5)

    ➤ Coroutines / CSP ➤ Optional Strong Memory Lifetimes like Rust ➤ Extended Generics Support (Existentials) ➤ Performance Improvements ➤ Reflection & Dynamic Features
  32. SWIFT ON THE SERVER? WHY? There are already tons of

    solutions
  33. GO

  34. GO THREE REASONS

  35. “There’s a real desire among developers for less fractionalization in

    programming -Kyle Jessup Founder, Perfect Framework
  36. CURRENT FULL STACK SETUP ➤ Ruby on the Server ➤

    Javascript in the Frontend ➤ SQL in the Database ➤ CSS & HTML in the Frontend ➤ Java for the Android solution ➤ Swift for the iOS Solution ➤ Thats 7 Languages! ➤ New devices, new paradigms (VR), more languages ➤ Should stride for less 1.
  37. CODE SHARING ➤ Builds upon the first reason ➤ Share

    code among different targets ➤ Saves time ➤ Less Bugs 2.
  38. SWIFT ➤ Swift is a great language ➤ Faster than

    Ruby or Python ➤ Consumes less memory ➤ Strong Types ➤ Modern language features 3.
  39. SWIFT WEB FRAMEWORKS

  40. PERFECT ➤ 8600 GitHub Stars ➤ "Perfect is a complete

    and powerful toolbox, framework, and application server for Linux, iOS, and macOS (OS X)."
  41. VAPOR ➤ 7000 Github Stars ➤ "A pure Swift &

    modular Web Framework that works on iOS, macOS, and Ubuntu." ➤ Very modular ➤ Beautiful API
  42. KITURA ➤ 5000 Github Stars ➤ "Kitura is a web

    framework and web server that is created for web services written in Swift." ➤ Backed by IBM ➤ IBM is investing heavily into Kitura ➤ Kitura's developers also contribute to Swift in order to improve Linux support
  43. MANY MORE ➤ Swifton: https://github.com/necolt/Swifton (1994 Stars) ➤ Zewo: https://github.com/Zewo/Zewo

    (1215 Stars) ➤ Blackfish: https://github.com/elliottminns/blackfish (924 Stars) ➤ Slimane: https://github.com/noppoMan/Slimane (61 Stars) ➤ Tailor: https://github.com/brownleej/tailor (55 Stars) ➤ Kunugi: https://github.com/novi/Kunugi (36 Stars) ➤ Quark: https://github.com/QuarkX/Quark (31 Stars)
  44. FIRST VERDICT ➤ The major frameworks have around 20.000 GitHub

    Stars together ➤ Ruby on Rails has 31.000 ➤ Huge interest (already) in Swift Server development
  45. WEB FRAMEWORK FEATURES

  46. ➤ All Swift frameworks support different features ➤ Depending on

    your use case, this may help you decide on a framework ➤ Most frameworks are still kinda in development or 1.0 ➤ Many features are only partially implemented
  47. ➤ Take with grain of salt ➤ Status quo may

    already have changed between my research and this talk
  48. USER AUTHENTICATION ➤ Log a user in, keep a user

    logged in, log him out, access a user ➤ Via email / password or i.e. Google / Twitter / Facebook (OAuth) ➤ Perfect, Vapor
  49. RAW DATABASE SUPPORT Feature MySQL PostgreSQL MongoDB Redis RethinkDB Cassandra

    CouchDB Perfect Vapor Kitura Zewo
  50. ORM ➤ Spares you from writing custom SQL ➤ Vapor

  51. ROUTING ➤ Interpret visitor input URLs ➤ Parse them ➤

    Retrieve parameters ➤ /users/23/posts/144/ comments/12 ➤ user_id: 23, post_id: 144, comment_id: 12 ➤ Perfect, Vapor, Kitura, Zewo
  52. CRUD ➤ Create, Read, Update, Delete ➤ Inherit from a

    class to automatically gain CRUD ➤ Auto-generate routes ➤ /users/:id/delete ➤ Vapor
  53. SESSIONS ➤ Sessions allow associating stateful information with individual visitors

    ➤ Perfect, Vapor, Kitura, Zewo
  54. LOGIC-LESS TEMPLATES ➤ Syntax for search & replace placeholders in

    HTML Files ➤ Templates without the notion of if / else conditions ➤ “Mustache” ➤ Perfect, Vapor, Kitura, Zewo <h1>{{header}}</h1> {{#empty}} <p>The list is empty.</p> {{/empty}}
  55. TURING TEMPLATES ➤ Conditionals ➤ Loops ➤ Vapor #if(entering) {

    Hello, #(friend-name)! } ##else() { Goodbye, #(friend-name)! } #loop(friends, "friend") { <li><b>#(friend.name)</b></li> }
  56. MIDDLEWARE ➤ Custom code which can be injected to run

    before and after each request / response cycle ➤ Examples: ➤ Authentication ➤ Security ➤ Caching ➤ Perfect, Vapor, Kitura, Zewo
  57. LOCALISATION ➤ drop.localization[lang, "welcome", "title"], ➤ Vapor "welcome": { "title":

    "Welcome!", "body": "A web framework" } "welcome": { "title": "¡Bienvenidos!", "body": "Un framework web" }
  58. WEB SOCKETS ➤ “Full-Duplex communication channels over single TCP connection”

    ➤ Real-time data transfer from / to server ➤ Perfect, Vapor, Zewo
  59. DEPLOYMENT Method Heroku Docker AWS Apache Nginx Blue mix DigitalOcean

    Perfect Vapor Kitura Zewo
  60. MISC ➤ File Uploads ➤ JSON Encoding / Decoding ➤

    File Uploads ➤ Perfect, Vapor, Kitura, Zewo
  61. MISSING FEATURES ➤ Database Migrations ➤ Scaffolding ➤ Asset Pipeline

    / Build System ➤ Admin Tool ➤ Sitemaps ➤ Hot Reloading of Code ➤ Reloading of HTML / CSS / JS
  62. PERFORMANCE

  63. BENCHMARKS ARE DIFFICULT ➤ There’re Swift Web Framework benchmark, but

    they were performed on macOS. The system and performance differs from Linux. Swift is better optimised for macOS ➤ Others only compare limited frameworks or lack the knowledge to implement the benchmark test correctly in the comparison languages ➤ One side will always be unhappy about the comparison
  64. TECH EMPOWER BENCHMARK ➤ It compares ~40 frameworks. ➤ It

    has very strict rules ➤ Optimized & tested heavily by lots of users ➤ www.techempower.com/ benchmarks
  65. STATE ➤ I’ve implemented the Techempower Plaintext benchmarks for Perfect,

    Kitura, and Vapor ➤ I’ve then compared Swift Web Frameworks to the competition as tested by Techempower ➤ Keep in mind ➤ Swift for Linux is new. ➤ The frameworks are all in development ➤ I’ve tested this on other hardware than Techempower ($5 digital ocean droplet vs. 40 Core 32GB Ram Monster) ➤ I ran less tests (only plaintext)
  66. THE SWIFT FRAMEWORKS 0 70000 140000 210000 280000 Swift: Vapor

    Swift: Kitura Swift: Perfect Plaintext
  67. SWIFT VS OTHER “MODERN” LANGUAGES Ruby: rails-unicorn Rust: iron Groovy:

    grails Javascript: express Javascript: nodejs Clojure: http-kit Elixir: Cowboy Swift: Vapor Swift: Kitura Scala: Play2 Swift: Perfect Go: falcore Go: gin Go: Raw 0 250000 500000 750000 1000000 Plaintext
  68. SWIFT VS THE FASTEST Clojure: http-kit Elixir: Cowboy Swift: Vapor

    Swift: Kitura php5 Java: Play2 Scala: Play2 Swift: Perfect Python: bottle Lua: lapis Go: falcore Python: falcon ur/web Go: gin Go: Raw Lua: openresty Java: servlet Java: vertx Java: netty 0 1250000 2500000 3750000 5000000 Plaintext
  69. PHP laravel Erlang: chicagoboss Ruby: rails-unicorn PHP: hhvm Ruby: sinatra-trinidad

    Java: ninja-standalone Rust: iron Python: flask Groovy: grails Javascript: express Javascript: nodejs Clojure: http-kit Elixir: Cowboy Swift: Vapor Swift: Kitura php5 Java: Play2 Scala: Play2 Swift: Perfect 0 75000 150000 225000 300000 Plaintext SWIFT VS COMMON SOLUTIONS
  70. Vapor (currently) offers the most features Some code examples of

    common features with Vapor
  71. HELLO WORLD

  72. TYPE-SAFE ROUTING Type-safe routing. This will only be called if

    the `number` parameter is indeed an integer
  73. RESPONSE CONFORMANCE PROTOCOL

  74. RESPONSE CONFORMANCE PROTOCOL

  75. RESPONSE CONFORMANCE PROTOCOL

  76. DEFAULT ARGUMENTS

  77. VALIDATION

  78. MIDDLEWARE

  79. MIDDLEWARE

  80. MIDDLEWARE

  81. AUTHENTICATION

  82. AUTHENTICATION

  83. AUTHENTICATION

  84. AUTHENTICATION

  85. CHUNKED DATA

  86. WEB SOCKETS

  87. OUTLOOK

  88. SHARE ALL THE CODE??

  89. SHARE ALL THE CODE?? NO

  90. HOWEVER

  91. SWIFT ON ANDROID ➤ 2 Facebook People and several other

    open source contributors ➤ Slowly evolving ➤ Uses JNI ➤ Alternative to C++ ➤ No auto-generated wrapper code yet ➤ Painful to use ➤ Give it another year
  92. SWIFT ON THE SERVER ➤ Early, but exciting ➤ Up

    until Swift 3 GM, Swift changed a lot ➤ Frameworks had to adopt to these changes ➤ Frameworks are still changing. Not stable yet. ➤ Outdated examples, docs, and tutorials
  93. SECURITY ➤ Don’t use it for anything vaguely important

  94. SHOULD YOU CHOOSE SWIFT? ➤ It depends ➤ On how

    much frontend code you have ➤ On how polyglot your team is ➤ On how much shared code you expect ➤ Whether the future direction of Swift aligns with business
  95. DOCUMENTATION ➤ Documentation is lacking for all frameworks. ➤ Possible

    approaches: ➤ Scout GitHub for Sample repos (though oftentimes they're outdated) ➤ Read unit tests ➤ Read Pull Requests for features. Oftentimes, they include basic usage instructions ➤ Read the source
  96. SOME ALTERNATIVES ➤ Kotlin (targets JVM & Javascript) ➤ Scala

    (targets JVM, Mobile / Native & Javascript) ➤ Javascript + Node + React / React Native ➤ C# (targets CLR, Mobile Devices & Desktops) ➤ C++ 17 (targets Server, Mobile & Emscripten / Javascript) ➤ Rust (targets Server, Mobile & Emscripten / Javascript) ➤ OCaml (targets Server, Mobile & Javascript) ➤ Java + bugVM (RoboVM) (targets Mobile, native macOS / Ubuntu, JVM, Javascript)
  97. HIRUNDO - SWIFT DISCUSSION & MORE STYLEMAC.COM/HIRUNDO

  98. THANKS! ➤ Will post links to Slides & Code on

    Twitter: @terhechte