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?

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.

Perfect

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.

import PerfectLib public func PerfectServerModuleInit() { Routing.Handler.registerGlobally() Routing.Routes["/"] = { _ in return HelloWorldHandler() } } class HelloWorldHandler: RequestHandler { func handleRequest(request: WebRequest, response: WebResponse) { response.appendBodyString("Hello, World!") response.requestCompletedCallback() } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import PerfectLib public func PerfectServerModuleInit ( ) { Routing . Handler . registerGlobally ( ) Routing . Routes [ "/" ] = { _ in return HelloWorldHandler ( ) } } class HelloWorldHandler : RequestHandler { func handleRequest ( request : WebRequest , response : WebResponse ) { response . appendBodyString ( "Hello, World!" ) response . requestCompletedCallback ( ) } }

See more on Perfect’s website!

Vapor

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!”.

import Vapor let app = Application() app.get("/") { request in return "Hello, World!" } app.start() 1 2 3 4 5 6 7 8 9 10 import Vapor let app = Application ( ) app . get ( "/" ) { request in return "Hello, World!" } app . start ( )

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

Kitura

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.

import Kitura let router = Router() router.get("/") { request, response, next in response.send("Hello, World!") next() } Kitura.addHTTPServer(onPort: 8090, with: router) Kitura.run() 1 2 3 4 5 6 7 8 9 10 11 12 import Kitura let router = Router ( ) router . get ( "/" ) { request , response , next in response . send ( "Hello, World!" ) next ( ) } Kitura . addHTTPServer ( onPort : 8090 , with : router ) Kitura . run ( )

See more on Kitura’s website.

Zewo

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.

import HTTPServer import Router let app = Router { route in route.get("/") { _ in return Response(body: "Hello, World!") } } try Server(app).start() 1 2 3 4 5 6 7 8 9 10 11 import HTTPServer import Router let app = Router { route in route . get ( "/" ) { _ in return Response ( body : "Hello, World!" ) } } try Server ( app ) . start ( )

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!