Swift has been around for some time now, and it is becoming a more and more appealing solution, not only for mobile and macOS developers – the next frontier is the server side! The ultimate aim is to enable developers to build complete products solely in Swift, with backend and frontend. Swift, which is a fast and safe language by design, has recently gained support from a few important players with their frameworks for building server-side Swift applications. The three most frequently used frameworks are Perfect, Vapor, and Kitura. Let’s compare them!
Perfect was created by a Canadian company, PerfectlySoft. Currently, among all server-side Swift frameworks, Perfect has the most starts on GitHub (12613). It was first released on 23 November 2015, and a lot of people have used since then. As claimed by authors, the framework is production-ready and can be used in commercial products. The latest version (3.0.3) dates back to 11 January 2018. The framework is updated on a monthly basis, and there are only a few open pull requests on GitHub, which indicates the maturity of Perfect.
Vapor was introduced by Tanner Nelson and Logan Wright in January, 2016 as an open-source, MIT-licensed software. At some point, the framework gained a sponsor, nodesagency.com, which allowed its two main developers to begin working full-time. Despite that, they’re not planning to change the license in the future. Vapor's primary objective was to speed up API development in an environment that Cocoa developers are already familiar with, and it is believed to be the primary course for Vapor. With the recent release of version 2.4.3, Vapor is gradually evolving into a very flexible and performant framework, which makes it a very interesting prospect for the future. It's on the verge of becoming the most popular server-side Swift framework with 12471 stars on GitHub, only 142 fewer than Perfect.
Kitura has been on the market since 9 February 2016. Developed by IBM, Kitura receives a new release once in a couple of weeks. Its code is publicly available on GitHub, and the framework itself has been released under the Apache 2.0 license. The first noticeable thing about Kitura is the company responsible for it. IBM has been actively involved in Swift development since its inception (see the famous article from Mike Gilfix, Vice President, Process Transformation @ IBM, dated 22 September 2016, which deemed Swift a language ready for enterprise purposes). It looks like it is an important niche for IBM, which promises further improvements and new functionalities.
Perfect and Vapor can be installed via Swift Package Manager, which also manages external dependencies for them. Additionally, Perfect provides a variety of template projects available on GitHub and Perfect Assistant 2.0, a companion macOS app with a set of convenience tools designed to help server-side Swift developers start, manage, compile, test, and prepare a project for deployment more easily. It can be used to develop an application with any Swift framework, not only Perfect. Kitura uses Homebrew for the installation – the Xcode project can be generated by the `kitura init` command. Perfect and Kitura are highly customisable with pluggable frameworks. Virtually every aspect of the server can be developed in many ways with the use of modules, even if they come from outside of the framework itself. Developers can write their solutions and plug them in as well.
Vapor has a similar mechanism. The only difference is the distinction between provided solutions (standalone packages) and external ones (managed by creating Providers). The Vapor CLI toolbox is a nice addition – it enables developers to create a template app with just one command.
The most basic task of creating a simple server looks similar in all the frameworks, as does the logic for routing and handling requests. All frameworks rely on controllers for this purpose, i.e. classes made by the developer for specific endpoints.
All frameworks enable authentication with two of the most significant services on the market: Google or Facebook. In addition, a user can be authorised with a GitHub account (Perfect, Kitura), Turnstile (Perfect, Vapor), LinkedIn, or Slack (Perfect only). Kitura adds its Kitura-Credentials pluggable framework and Twitter authentication, Perfect has its implementation of OAuth2, while Vapor has Vapor’s Auth Provider.
All frameworks support JSON as the default data format. Perfect and Kitura also have native support for Aphid MQTT (Internet of Things/IoT Messaging). XML support can be found in Perfect and Vapor, while in Kitura, you need to use an external package for it. Additionally, Perfect supports binary JSON (BSON) and has a native cURL wrapper. But the most significant advantage of using Perfect in terms data processing is its native support for direct operations on files, directories, and .zip files.
Vapor uses a templating system called Leaf. It has the advantages of being natively Swift and being designed to integrate in a way that works well with Vapor’s engine, the underlying system that deals with requests and responses. All frameworks support Mustache, Markdown, and Stencil (except Perfect).
The gold standard for all frameworks are SQL databases: MySQL, PostgreSQL, and SQLite. All three frameworks described in this article provide support for SQL databases with native connectors, as in the case of Redis databases. Perfect and Kitura also support CouchDB, while Perfect and Vapor also have support MongoDB. There are also exclusive connectors in Kitura (Apache Cassandra, ScyllaDB) and Perfect (MariaDB, FileMaker).
The most prominent difference when it comes to dealing with databases is how a framework handles managing entities. Kitura is the only one lacking a native ORM. It attempts to provide one single interface for SQL databases contained in one general Kuery class. It has a noticeable upside – the underlying database can be replaced without any changes to the actual code. However, it comes with two disadvantages. First, Kuery has an SQL-like syntax for operations, which can be a high entry point for some object-oriented developers. Second, the chosen syntax enables developers to use plain strings as a part of a request, or just to create a full request from an SQL string. This is a double-edged sword: it covers almost every case of SQL requests but also produces very irritating runtime errors after switching databases, if any database specific commands were used. For instance, MySQL does not have a RETURNING command from PostgreSQL, which can be used as a suffix while creating a general Kuery request.
Perfect, on the other hand, recommends a more objective approach from the beginning with its StORM, an ORM abstraction layer that handles all low-level database connections, leaving the developer with nice and clean objects associated with entities. This solution is far more user-friendly and error resistant.
Vapor has its own solution named Fluent. An object-oriented approach to the problem results with code that looks almost identically to code produced by StORM. Big thumbs up for Perfect and Vapor for creating clean and user-friendly ORM solutions.
Vapor is the only framework written entirely in Swift and developed with the "Swift-first" principle in mind. This has given rise to the most straightforward syntax and the lowest entry threshold. It can be the first choice for developers determined to write solely in Swift. It has a downside, though, as Vapor’s syntax can be less descriptive than other solutions – many modules have their specific names unrelated to server development (as Droplet, Leaf, etc.). It results in the necessity to dig into the documentation in order to find the desired module for a particular task.
Although similar to Vapor, Perfect’s syntax is very descriptive, which results in highly readable and more explicit code. However, Perfect has a higher entry point as it is very technically oriented and focused on performance.
Kitura falls in the middle with its syntax. One noticeable thing: it’s dead easy to create some simple solutions – you can do it in a matter of minutes. On the other hand, this comes at a cost as the application grows. Kitura seems to involve more and more boilerplate code. Vapor, on the other hand, provides more mature, modular architecture, but it takes time to explore.
All frameworks support Heroku and Amazon Web Service. Kitura offers out-of-the-box support for Bluemix, which is probably going to end the discussion about choosing the right framework for many projects.
In terms of community, Vapor wins by a landslide – its users are well-known from high activity and eagerness to help the newbies, especially on Slack. There is plenty of source code, and also many solutions and tutorials are available. However, it is nothing comparing to Perfect's learning resources. The number of them is so big that PerfectlySoft had to create separate organisations on GitHub to organise them properly. There are plenty of samples along with ready-to-deploy solutions. Ray Wenderlich created comprehensive courses of developing server applications with both the above frameworks.
Kitura has the smallest community to date, but IBM tries to expand it actively. So far, Kitura has received 6271 stars and 410 forks on GitHub. It is almost exactly half of the stars that Perfect and Vapor have. The latter two are complete and mature solutions, but with all of IBM's power and enthusiasm, this might change in the future. For now, it is easy to notice IBM’s involvement – its engineers are active and helpful on GitHub and, especially, on the dedicated Slack channel.
Although Perfect seems to be the most mature and complete framework, the decision which one of them choose should be made on a case by case basis. The aspects you will need to consider are the supported databases, the data format support, and the development tools (Perfect Assistant, Vapor Toolbox). The frameworks also have some exclusive functionalities:
We can state with confidence: Swift is going to remain one of the possible solutions for writing backend. And with the amount of support from big companies, developers, and enthusiasts, it will grow to be a serious competitor for the major players such as Node.js or Ruby on Rails.