Swift was introduced in 2014 by Apple to replace Objective-C as the main programming language for all the Apple's platforms. It's fast, powerful, secure, and very beginner-friendly. Half a year later it was open sourced, so it was a matter of time when it would be ported to other platforms. Besides iOS, iPadOS, macOS, tvOS and watchOS it's officially supported on Linux and Windows. It even works on Android, but with some tweaks, you can read about it on Swift's official GitHub repo.
However, you can't really make much on these platforms besides some scripts and CLI programs. That is because a language is only a part of a code base. The other part is a User Interface framework. We have UIKit for mobile, AppKit for macOS and of course SwiftUI for both. Actually, there is a platform which does not require a UI framework. What is it? It's Arduino of course! You can create simple programs for your micro controller with this project, and write them with Swift! How neat!
Alright, it's a joke. Although it's real and sounds quite interesting for a side project, but I don't have any Arduino at my fingertips, so I'll leave it for later. I'm talking about server-side development of course.
Why Swift for a server?
Three main reasons:
- Swift. As I said, it's fast, powerful and secure. Besides, it's complied at runtime, so you'd be able to catch any errors at compile time before pushing the code to production.
- Familiar environment.
- If you, like me, have an iOS background, you'd be familiar with quite a lot of things when making a backend in Swift.
- Xcode. With all its flaws and bugs it's still a pretty powerful IDE and you can use the same instruments as when working with iOS, like breakpoints.
- You can reuse your code. Put some code into a Swift Package and use it for both backend and frontend, for example you could reuse your data models.
- Just for fun! It's always fun to learn new things, especially when it's something as unconventional as server-side Swift.
Server-side Swift as an idea is quite old and is supported by large companies like Apple, Amazon and MongoDB. Recently Apple introduced SwiftNIO - a low-level framework for network callls and async programming with Swift which sounds like they take this idea very seriously. Besides that, it's rumored that Amazon's Prime Video service's backed is written in Swift. What makes it even more interesting is that the people who chose Swift weren't ex-iOS devs, they were Java devs who chose Swift for its advantages. There's also an official work group called SSWG (Server Side Work Group) which meets biweekly and works on making Swift even more suitable for server-side development.
So what are the options?
But even if it's supported by a big company anything can happen. Up until 2020 IBM supported Kitura - a web framework in Swift, making it one of the most popular. And then they decided to drop the idea and stopped contributing to the project. Besides Kitura there are other frameworks like Perfect, Amazon's Smoke and Vapor, which is probably the best option at the moment.
Why Vapor?
- Support
Vapor get regular updates, including major updates that come with Swift updates. E.g. Vapor 4 comes with async/await features that came with Swift 5.5.
- Community
There's an official Discord server with many channels, including other languages. Vapor's twitter is very active and sometimes retweets developers who get into Vapor. And there're a lot of GitHub repos that use Vapor that might help with making your own project.
Besides, there's some third-party stuff that makes it even easier to start with Vapor, for example a CMS written in Swift, how cool is that?
- Technical readiness
PostgreSQL, Mongo, MySQL and SQLite are supported out-of-box. There's also a community-driven drivers for CouchDB, which means you are free to make your own stuff if you want. As I mentioned earlier, Swift runs on Linux natively, with Foundation an XCTest. Which means you can use Ubuntu, make a Docker container, deploy to Heroku, Digital Ocean or Nginx. All of these are supported and well documented.
- Documentation
Because it's so popular, Vapor gets a lot of attention from the community, with many blog posts (including this one), tutorials, YouTube videos and even books. WWDC22 even had an entire session backend development using Vapor and deploying to Heroku
Demo
I'll go into Vapor deeper in another blog post and leave a link to it, but for now here're my repos with backend and iOS app with a simple server-side app.
FAQ
- Is it ready for production?
Short answer - yes. Long answer - it depends on your use case. It'll be enough for most cases really. It's HTTP requests and REST API, what more do you want? Memory and power usage are low, which will keep the server prices low as well. For something specific or advanced you should still go with other technologies, Go for example.
- Why not Go?
Go is a great option for backend. It's similar to Swift: fast, powerful and memory-efficient. Up until lately it's main advantage was it's async features. It turns out async is very important for a server because of how fast you can access stuff in your database, for example.
However, Swift is relatively young and is very open about copying other languages' features if they seem great. And the Swift team is working on moving away from a language made exclusively for iOS and other frontend platforms to a more universal language.
So in 2021 with Swift 5.5 we got async/await. That didn't seem like a huge feature for an iOS developer and quite frankly I didn't think it would be the first thing we'd adopt in our everyday coding. And it wasn't made for iOS, it didn't seem so. It actually seems like a very important feature for a server-side development.
- So, only pros and no cons?
Not really. With traditional backend languages and frameworks you can get a lot of stuff made by other devs, which would make you life much easier. After all, a programmer's job is not about reinventing the wheel. It's about copying someone else's wheel and trying to figure out how it works. With Vapor (and Swift in general) you won't get as many ready-for-production solutions. As I said, there's stuff that would be enough in general, but not for something specific or advanced.
Thus, it's great for a pet project, a prototype or even an MVP because of how fast you could make your backend (and use the same backend for your Android app as well). But then, if for some reason your startup actually takes off and the growth is inevitable, it'd be much easier to find a backend developer than someone as specific as a Vapor/Server-side Swift developer.
You can also render web pages and make web frontend with it, and there are some websites written in pure Swift which sound very fun! It so happens that most of the websites I could find are either about Swift or Swift conferences, but that makes sense:
- https://www.serversideswift.info
- https://swiftpackageindex.com
- https://swiftleeds.co.uk
- https://tinyfac.es
In my opinion, Vapor is great. Swift evolution makes it seem like server-side Swift has a bright future and is definitely worth trying out.