Are you ready for Swift on the server

I’m a full-stack developer, so I build my iOS applications in Swift and backends in Node.js. But, does it have to be that way?

I started using Swift two years ago, right after Apple announced it at WWDC in 2014. Like me, many developers jumped at the opportunity to build iOS and macOS apps in a modern language and quickly fell in love with Swift.

In 2015, when Apple announced that they would open source Swift, we became even more excited. Swift would run on Linux, and could be used for more than just iOS and macOS applications, including building web applications. And after hearing about various server-side frameworks for Swift, I was dying to try it out!

Since I work at Stormpath (we’re an Identity API for developers), I decided to spend some time learning more about the server-side Swift community to see how we could potentially contribute.

Why Swift on the Server?

Developer Productivity vs. App Performance chart at WWDC

As Apple expressed in their Swift announcement, some programming languages are great for developer productivity but run slowly. Others run quickly, but are harder for developers to build with quickly. Swift is both fast to write, and fast to run.

Swift has taken off at a tremendous pace, mostly because of the great job Apple has done at delivering on these goals. In this year’s Stack Overflow Developer Survey, Swift was voted the second most loved programming language, with 75% more votes than last year.

Need another reason to use Swift on the server? Node.js, rose to popularity because it enables “full-stack” developers to work on both the frontend and backend of their web apps. This allows teams to iterate faster, ultimately leading to more successful products. Swift decreases the iteration time for mobile development teams in the same way.

Server-Side Swift

There are dozens of server-side Swift frameworks, but I’m going to focus on four of the most popular frameworks on GitHub: Perfect, Vapor, Kitura, and Zewo. Combined, they have over 15,000 stars; in comparison, Rails, a much more established framework, has about 31,000. This really shows the level of interest for Swift on the server.

Let’s take a quick look at each framework, and what a simple “Hello, World!” webpage looks like in code.

PerfectPerfect, a framework for Swift

The most popular Swift framework on GitHub, Perfect was the first to hit the market and gain traction. Perfect is designed by Kyle Jessup, the creator of the Lasso web programming language. It has a strong set of features, and you can tell that it’s been built by someone with a deep knowledge of web backends. Perfect’s team has even raised $1.2MM in seed funding to bring Perfect to perfection! That said, Perfect’s API feels a bit antiquated for a modern web framework. The team is addressing this for Perfect 2 (for Swift 3), so you can expect their syntax to change from this example in the near future.

In this example using Perfect 1.0 (which runs on Swift 2), we’ll create a Swift framework for the Perfect server to use and import. A global function called PerfectServerModuleInit initializes the routing handler and links the / route to our HelloWorldHandler. The handler then sends “Hello, World!” back to the client and tell the server that the request is completed.

See more on Perfect’s website!

VaporVapor, a framework for Swift

The second most popular framework for Swift on GitHub is Vapor, and it’s my personal favorite. Vapor has the best documentation of the four listed here, and I would recommend it as a starting point for anyone just diving into server-side Swift. Vapor is built in Swift 3, and has funding for two full-time developers.

In this example, we’ll create a Vapor application object, and add a route handler for /, where we return “Hello World!”.

Want to build this? See our tutorial on building your first Vapor app. Otherwise, check out Vapor’s website.

KituraKitura, a framework for Swift

Third on the list is IBM’s Kitura. IBM has been throwing a lot of resources into making Swift the next big enterprise language after Java, and Kitura is part of that effort. At last week’s Swift meetup at IBM, I met a few members of the StrongLoop team (who maintain Express.js) that are switching over to Swift. If you’re familiar with Express, Kitura will look pretty familiar.

See more on Kitura’s website.

ZewoZewo, a framework for Swift

The final framework I want to share is Zewo. What’s really interesting with Zewo is their architecture: it’s super modular and is actually composed of more than 50 packages. If you want to build your own web server, Zewo is a great place to start because of this architecture — for instance, Swifton (Rails) is a web framework built on Zewo.

The second thing I find interesting about Zewo is that their base components are actually interchangeable with Vapor. Vapor and Zewo have collaborated on Open Swift, a set of common interfaces for server components: middleware, request, response, and other objects. With Open Swift, anyone building middleware or other plugins for a Swift server can use them in both Vapor and Zewo.

Zewo’s code looks fairly similar to Vapor and Kitura, but is a little more verbose because of its modularity.

See more on Zewo’s website

Is Server Side Swift Ready for Production?

Not yet.

While you can run Perfect on Swift 2.2, all four frameworks are currently writing new code in Swift 3 to support the Swift Package Manager release and to better support for Swift on Linux.

For now, simple tasks and libraries that web developers take for granted are still difficult in the Swift ecosystem. For example, if you want to make web requests, you need to link to native C code in most of these libraries. However, if writing those libraries sound like your thing, I think you’ll have a lot of fun participating in the Swift ecosystem!

Swift 3 is expected to go live in September, bringing with it additional stability and…

Want to contribute?

All four projects are open to contributions, and I’ve found the entire Swift community to be really friendly. If you like building web components and being a pioneer, you’ll have a lot of fun with Swift on the server.

Chat channels for these frameworks:

Want more?

This blog post is based on a talk I gave at the Swift Language Meetup in San Francisco. Watch the recording!

Want to try building your first website with Swift? Try out our tutorial on how to build your first app with Vapor!

Questions about this post? Comment below or ping me on Twitter!

  • Pingback: Tutorial: Build Your First Swift Web App with Vapor - Stormpath User Identity API()

  • swiftlang

    Speaking of server-side Swift, it’s good to mention Tailor framework by John Brownlee, Taylor by Jorge Izquierdo with 850+ stars on GitHub, Swifter by Damian Kołakowski with 1700+ stars.

    • Yeah, there’s definitely quite a few frameworks and application servers out there! Too many to list ;] There’s also Frank, Swift Express, etc… hopefully this post sparks people’s interest and gets them to start playing around with server-side Swift!

  • Fareed Quraishi

    Wow, buddy – what a great write up. I try to dabble in all the SSS frameworks. Your write up is so clear and pristine, it does a great job at depicting the landscape. I am going to try your Vapor tutorial, I am hoping to make my own one for Perfect soon. I tend to talk about SSS at meet-ups, do you mind if I use your photo of performance vs productivity in my slide deck?

    Thanks,
    Fareed

    • Thanks Fareed! And no problem; the photos is actually from Apple’s keynote when they announced Swift. I’m not a lawyer, but I believe using it in a post like this or a presentation like yours is considered fair use, so go for it! =]

  • Pingback: Swift Mushroom Clouds – Under The Bridge()

  • Pingback: Swift | Pearltrees()

  • Andrew Morris

    When do you expect Vapor or any of the other frameworks to be production ready? Also what features are you waiting to see before you consider them production ready?

    • That’s a great question! Most of the Swift web frameworks are being written in Swift 3, so I’m sure many of them will hit 1.0 / “production ready” when Swift 3 officially releases (probably next month?).

      Besides for language instability, the other problem is that Foundation (essentially Apple’s standard library for iOS / macOS apps) isn’t fully implemented on Linux. So you’ll have to rewrite some basic functionality right now — which is a bit annoying to say the least!

  • Ethan Kay

    So when will Stormpath’s authentication platform be available for these? So far I’ve only found recommendations for using Express.js and Node.js, but I’d love to my server code in swift and take advantage of the user-authorization. Or am I not understanding how Stormpath works?

  • Pingback: Server Side Swift – ANA NOGAL()

  • Pingback: Server Side Swift – Open Code Group()

  • Pingback: Swift Web Framework Vapor 소개 – BitBoy Labs()

  • John D. Pope

    As a jump starter – you can use
    https://hub.docker.com/r/swiftdocker/swift/
    I modified this to add dev support with sshd + zsh https://gist.github.com/johndpope/01a74ffb93921df48e334097117be1b3

    If you’re looking to build a microservice – then I’d say swift3 would be suitable for production with above frameworks.

    Swift Package Manager still has a long way to catch up with feature parity with say ruby gems / node package manager.

  • Anibal

    is Vapor (or nay other framework) ready to production so far?

  • Pingback: margaret cunniffe()