Slide 1

Slide 1 text

DÜSSELDORF

Slide 2

Slide 2 text

SWIFT? ON THE SERVER?

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

SWIFT?

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

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)

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

SWIFT, THE LANGUAGE

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

INTRODUCTION

Slide 13

Slide 13 text

INTRODUCTION

Slide 14

Slide 14 text

INTRODUCTION

Slide 15

Slide 15 text

INTRODUCTION

Slide 16

Slide 16 text

INTRODUCTION

Slide 17

Slide 17 text

INTRODUCTION

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

OPTIONALS

Slide 21

Slide 21 text

GENERICS

Slide 22

Slide 22 text

GENERICS

Slide 23

Slide 23 text

PROTOCOL ORIENTED PROGRAMMING

Slide 24

Slide 24 text

PROTOCOL ORIENTED PROGRAMMING

Slide 25

Slide 25 text

PROTOCOL ORIENTED PROGRAMMING

Slide 26

Slide 26 text

PROTOCOL ORIENTED PROGRAMMING

Slide 27

Slide 27 text

ENUMS & PATTERNS

Slide 28

Slide 28 text

ENUMS & PATTERNS

Slide 29

Slide 29 text

ERROR HANDLING

Slide 30

Slide 30 text

ERROR HANDLING

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

GO

Slide 34

Slide 34 text

GO THREE REASONS

Slide 35

Slide 35 text

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

Slide 36

Slide 36 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 stride for less 1.

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

SWIFT WEB FRAMEWORKS

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 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 44

Slide 44 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 45

Slide 45 text

WEB FRAMEWORK FEATURES

Slide 46

Slide 46 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 47

Slide 47 text

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

Slide 48

Slide 48 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 49

Slide 49 text

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

Slide 50

Slide 50 text

ORM ➤ Spares you from writing custom SQL ➤ Vapor

Slide 51

Slide 51 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 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 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 55

Slide 55 text

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

    Slide 56

    Slide 56 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 57

    Slide 57 text

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

    Slide 58

    Slide 58 text

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

    Slide 59

    Slide 59 text

    DEPLOYMENT Method Heroku Docker AWS Apache Nginx Blue mix DigitalOcean Perfect Vapor Kitura Zewo

    Slide 60

    Slide 60 text

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

    Slide 61

    Slide 61 text

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

    Slide 62

    Slide 62 text

    PERFORMANCE

    Slide 63

    Slide 63 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 64

    Slide 64 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 65

    Slide 65 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 droplet vs. 40 Core 32GB Ram Monster) ➤ I ran less tests (only plaintext)

    Slide 66

    Slide 66 text

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

    Slide 67

    Slide 67 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 68

    Slide 68 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 69

    Slide 69 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 70

    Slide 70 text

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

    Slide 71

    Slide 71 text

    HELLO WORLD

    Slide 72

    Slide 72 text

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

    Slide 73

    Slide 73 text

    RESPONSE CONFORMANCE PROTOCOL

    Slide 74

    Slide 74 text

    RESPONSE CONFORMANCE PROTOCOL

    Slide 75

    Slide 75 text

    RESPONSE CONFORMANCE PROTOCOL

    Slide 76

    Slide 76 text

    DEFAULT ARGUMENTS

    Slide 77

    Slide 77 text

    VALIDATION

    Slide 78

    Slide 78 text

    MIDDLEWARE

    Slide 79

    Slide 79 text

    MIDDLEWARE

    Slide 80

    Slide 80 text

    MIDDLEWARE

    Slide 81

    Slide 81 text

    AUTHENTICATION

    Slide 82

    Slide 82 text

    AUTHENTICATION

    Slide 83

    Slide 83 text

    AUTHENTICATION

    Slide 84

    Slide 84 text

    AUTHENTICATION

    Slide 85

    Slide 85 text

    CHUNKED DATA

    Slide 86

    Slide 86 text

    WEB SOCKETS

    Slide 87

    Slide 87 text

    OUTLOOK

    Slide 88

    Slide 88 text

    SHARE ALL THE CODE??

    Slide 89

    Slide 89 text

    SHARE ALL THE CODE?? NO

    Slide 90

    Slide 90 text

    HOWEVER

    Slide 91

    Slide 91 text

    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

    Slide 92

    Slide 92 text

    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

    Slide 93

    Slide 93 text

    SECURITY ➤ Don’t use it for anything vaguely important

    Slide 94

    Slide 94 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 ➤ Whether the future direction of Swift aligns with business

    Slide 95

    Slide 95 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 96

    Slide 96 text

    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)

    Slide 97

    Slide 97 text

    HIRUNDO - SWIFT DISCUSSION & MORE STYLEMAC.COM/HIRUNDO

    Slide 98

    Slide 98 text

    THANKS! ➤ Will post links to Slides & Code on Twitter: @terhechte