Slide 1

Slide 1 text

DEVELOPING APP-BACKENDS WITH SWIFT ON THE SERVER

Slide 2

Slide 2 text

ABOUT ME Benedikt Terhechte Hirundo SourceKittenDaemon CoreValue Indie Developer @terhechte appventure.me

Slide 3

Slide 3 text

SERVER DEVELOPMENT ➤ Worked many years as a backend developer ➤ Kept an active interest in server development afterwards ➤ Developed in ➤ Python + Django ➤ Scala + Scalatra ➤ Clojure ➤ Go + Revel ➤ Lua + Openresty ➤ PHP + Symfony

Slide 4

Slide 4 text

SWIFT ON THE SERVER? WHY? There are already tons of solutions

Slide 5

Slide 5 text

GO

Slide 6

Slide 6 text

GO THREE REASONS

Slide 7

Slide 7 text

“There’s a real desire among developers for less fractionalization in programming -Kyle Jessup Founder, Perfect Framework

Slide 8

Slide 8 text

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 strife for less 1.

Slide 9

Slide 9 text

CODE SHARING ➤ Builds upon the first reason ➤ Share code among different targets ➤ Saves time ➤ Less Bugs 2.

Slide 10

Slide 10 text

SWIFT ➤ Swift is a great language ➤ Faster than Ruby or Python ➤ Consumes less memory ➤ Strong Types ➤ Modern language features 3.

Slide 11

Slide 11 text

WEB FRAMEWORKS

Slide 12

Slide 12 text

WHAT’S A WEB FRAMEWORK ➤ Just like cocoa touch or cocoa ➤ Support the development of web applications and web APIs

Slide 13

Slide 13 text

DB Browser Request Server Framework Code REQUEST / RESPONSE FLOW CHART

Slide 14

Slide 14 text

DB Browser Request Server Framework Code REQUEST / RESPONSE FLOW CHART

Slide 15

Slide 15 text

SWIFT WEB FRAMEWORKS

Slide 16

Slide 16 text

PERFECT ➤ 8200 GitHub Stars ➤ "Perfect is a complete and powerful toolbox, framework, and application server for Linux, iOS, and macOS (OS X)."

Slide 17

Slide 17 text

VAPOR ➤ "A pure Swift & modular Web Framework that works on iOS, macOS, and Ubuntu." ➤ Very modular ➤ Beautiful API

Slide 18

Slide 18 text

KITURA ➤ "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

Slide 19

Slide 19 text

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)

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

WEB FRAMEWORK FEATURES

Slide 22

Slide 22 text

➤ 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

Slide 23

Slide 23 text

➤ Take with grain of salt ➤ Status quo may already have changed between my research and this talk

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

RAW DATABASE SUPPORT Feature MySQL PostgreSQL MongoDB Redis RethinkDB Cassandra CouchDB Filemaker Perfect Vapor Kitura Zewo

Slide 26

Slide 26 text

ORM ➤ Database Abstraction, like Core Data ➤ Spares you from writing custom SQL ➤ Vapor

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

CRUD ➤ Create, Read, Update, Delete ➤ Inherit from a class to automatically gain CRUD ➤ Auto-generate routes ➤ /users/:id/delete ➤ Vapor

Slide 29

Slide 29 text

SESSIONS ➤ Sessions allow associating stateful information with individual visitors ➤ Perfect, Vapor, Kitura, Zewo

Slide 30

Slide 30 text

LOGIC-LESS TEMPLATES ➤ Syntax for search & replace placeholders in HTML Files ➤ Templates without the notion of if / else conditions ➤ “Mustache” ➤ Perfect, Vapor, Kitura, Zewo

{{header}}

{{#empty}}

The list is empty.

{{/empty}}

Slide 31

Slide 31 text

TURING TEMPLATES ➤ Conditionals ➤ Loops ➤ Vapor #if(entering) { Hello, #(friend-name)! } ##else() { Goodbye, #(friend-name)! } #loop(friends, "friend") {
  • #(friend.name)
  • }

    Slide 32

    Slide 32 text

    MIDDLEWARE ➤ Custom code which can be injected to run before and after each request / response cycle ➤ Examples: ➤ Authentication ➤ Security ➤ Caching ➤ Perfect, Vapor, Kitura, Zewo

    Slide 33

    Slide 33 text

    LOCALISATION ➤ drop.localization[lang, "welcome", "title"], ➤ Vapor "welcome": { "title": "Welcome!", "body": "A web framework" } "welcome": { "title": "¡Bienvenidos!", "body": "Un framework web" }

    Slide 34

    Slide 34 text

    WEB SOCKETS ➤ “Full-Duplex communication channels over single TCP connection” ➤ Real-time data transfer from / to server ➤ Perfect, Vapor, Zewo

    Slide 35

    Slide 35 text

    DEPLOYMENT Method Heroku Docker AWS Apache Nginx Bluemix Perfect Vapor Kitura Zewo

    Slide 36

    Slide 36 text

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

    Slide 37

    Slide 37 text

    MISSING FEATURES ➤ Database Migrations ➤ Scaffolding ➤ Asset Pipeline / Build System ➤ Admin Tool ➤ Sitemaps ➤ Hot Reloading of Code ➤ Reloading of HTML / CSS / JS

    Slide 38

    Slide 38 text

    REMARKS ➤ All Frameworks use the Swift Package Manager ➤ Vapor, Kitura, and Zewo use OpenSwift for compatibility. This means that middleware, requests, responses, and other objects are based on the same protocols and can be interchanged.

    Slide 39

    Slide 39 text

    PERFORMANCE

    Slide 40

    Slide 40 text

    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

    Slide 41

    Slide 41 text

    TECH EMPOWER BENCHMARK ➤ It compares ~40 frameworks. ➤ It has very strict rules ➤ Optimized & tested heavily by lots of users ➤ www.techempower.com/ benchmarks

    Slide 42

    Slide 42 text

    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 droplets vs. AWS c3.large) ➤ I ran less tests (only plaintext)

    Slide 43

    Slide 43 text

    THE SWIFT FRAMEWORKS 0 70000 140000 210000 280000 Swift: Vapor Swift: Kitura Swift: Perfect Plaintext

    Slide 44

    Slide 44 text

    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

    Slide 45

    Slide 45 text

    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

    Slide 46

    Slide 46 text

    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

    Slide 47

    Slide 47 text

    Vapor (currently) offers the most features Some code examples of common features with Vapor

    Slide 48

    Slide 48 text

    HELLO WORLD

    Slide 49

    Slide 49 text

    TYPE-SAFE ROUTING Type-safe routing. This will only be called if the `number` parameter is indeed an integer

    Slide 50

    Slide 50 text

    RESPONSE CONFORMANCE PROTOCOL

    Slide 51

    Slide 51 text

    RESPONSE CONFORMANCE PROTOCOL

    Slide 52

    Slide 52 text

    RESPONSE CONFORMANCE PROTOCOL

    Slide 53

    Slide 53 text

    DEFAULT ARGUMENTS

    Slide 54

    Slide 54 text

    VALIDATION

    Slide 55

    Slide 55 text

    MIDDLEWARE

    Slide 56

    Slide 56 text

    MIDDLEWARE

    Slide 57

    Slide 57 text

    MIDDLEWARE

    Slide 58

    Slide 58 text

    AUTHENTICATION

    Slide 59

    Slide 59 text

    AUTHENTICATION

    Slide 60

    Slide 60 text

    AUTHENTICATION

    Slide 61

    Slide 61 text

    QUESTION GAME

    Slide 62

    Slide 62 text

    No content

    Slide 63

    Slide 63 text

    No content

    Slide 64

    Slide 64 text

    No content

    Slide 65

    Slide 65 text

    SHARING CODE: DEMO

    Slide 66

    Slide 66 text

    CAN WE USE THIS?

    Slide 67

    Slide 67 text

    GENERAL ➤ 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 ➤ Xcode's SourceKitD crashes far more often than during iOS development

    Slide 68

    Slide 68 text

    WHICH FRAMEWORK ➤ If you’re creating a simple API service and really care about performance, go with Perfect ➤ Otherwise, Vapor is the best solution ➤ Keep an eye on Kitura, IBM is investing heavily

    Slide 69

    Slide 69 text

    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 ➤ On how much you love Swift ➤ Expect to refactor

    Slide 70

    Slide 70 text

    CONSIDERATIONS ➤ No UIKit / AppKit dependency: You can use value types, protocols & generics ➤ Test under Linux. There’re still differences even in Foundation. ➤ Prefer abstractions provided by the framework ➤ Learn from the Vapor source code. It is insightful.

    Slide 71

    Slide 71 text

    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

    Slide 72

    Slide 72 text

    TIPS ➤ Use Swiftenv to manage Swift versions ➤ Watch this years WWDC Swift performance talks ➤ If you don't want to install Linux to run things, use Docker ➤ Will post links to Slides & Code on Twitter: @terhechte