Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift on the Server

Swift on the Server

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

Benedikt Terhechte

October 23, 2016
Tweet

More Decks by Benedikt Terhechte

Other Decks in Programming

Transcript

  1. 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
  2. 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)
  3. 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
  4. 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
  5. 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
  6. 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
  7. GO

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

    programming -Kyle Jessup Founder, Perfect Framework
  9. 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.
  10. CODE SHARING ➤ Builds upon the first reason ➤ Share

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

    Ruby or Python ➤ Consumes less memory ➤ Strong Types ➤ Modern language features 3.
  12. PERFECT ➤ 8600 GitHub Stars ➤ "Perfect is a complete

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

    modular Web Framework that works on iOS, macOS, and Ubuntu." ➤ Very modular ➤ Beautiful API
  14. 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
  15. 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)
  16. 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
  17. ➤ 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
  18. ➤ Take with grain of salt ➤ Status quo may

    already have changed between my research and this talk
  19. 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
  20. 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
  21. CRUD ➤ Create, Read, Update, Delete ➤ Inherit from a

    class to automatically gain CRUD ➤ Auto-generate routes ➤ /users/:id/delete ➤ Vapor
  22. 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}}
  23. TURING TEMPLATES ➤ Conditionals ➤ Loops ➤ Vapor #if(entering) {

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

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

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

    ➤ Real-time data transfer from / to server ➤ Perfect, Vapor, Zewo
  27. MISC ➤ File Uploads ➤ JSON Encoding / Decoding ➤

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

    / Build System ➤ Admin Tool ➤ Sitemaps ➤ Hot Reloading of Code ➤ Reloading of HTML / CSS / JS
  29. 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
  30. TECH EMPOWER BENCHMARK ➤ It compares ~40 frameworks. ➤ It

    has very strict rules ➤ Optimized & tested heavily by lots of users ➤ www.techempower.com/ benchmarks
  31. 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)
  32. 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
  33. 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
  34. 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
  35. TYPE-SAFE ROUTING Type-safe routing. This will only be called if

    the `number` parameter is indeed an integer
  36. 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
  37. 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
  38. 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
  39. 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
  40. 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)