We build your Software

Speedy Web Applications with Swift and Vapor

Two people working on web programming on a computer

The best-known languages for Web programming are rather old. Most of them are interpreted, which seriously limits their performance. What if you could use a modern, compiled language to write Web applications? You can do just that, using the Swift language and the Vapor framework.

Fast response is important to getting and keeping page views. The longer a page takes to load, the more people will give up on it. In addition, slow pages pay a penalty in search ranking. A fast language with low memory requirements, such as Swift, will help sites to deliver their pages faster.

It takes a bit of courage and quirkiness to name a software product "Vapor," but it's real and usable. Version 1 was released in 2016 and Version 2 in May of 2017. Developers who have learned its value for Mac OS and iOS software can now apply their skills to the Web. Vapor is open-source software, available under the MIT license. The code repository and bug tracking are visible to all on GitHub, and the development team has a discussion channel on Slack.

Vapor keeps up with the latest developments in Swift. Vapor 2.2.0, released in August, includes preliminary support for Swift 4. Vapor 3 is currently in alpha and promises a more "distinctly Swift development experience" along with better documentation and improved performance.

Why use Vapor for the Web?

Developers who have used Swift for Apple systems know it incorporates the latest concepts in language design. Its syntax and semantics make common errors, such as nil values and uninitialized values, less likely. It's type-safe, yet its syntax minimizes the amount of work that's necessary solely to meet the language's requirements. These features mean very fast-running code with fewer bugs and therefore fewer security holes.

Vapor matches Swift in being powerful but lightweight. Its modular design lets the developer use only the features that are necessary. It's built out of packages, with only the most commonly used ones included by default. They include, among others:

  • Vapor. The Web framework, handling authentication, sessions, and cookies.

  • Engine. The transport mechanisms, including HTTP and even SMTP.

  • TLS. Handling of HTTPS connections, using the OpenSSL API.

  • Crypto. Encryption functions, including a native implementation of BCrypt.

  • JSON. Handling of JSON data.

Optional and third-party packages add many features, including database access, forms, background tasks, templating, and more.

A Vapor web application consists of one or more Droplets. A Droplet can contain discrete handlers for each path or RESTful handling of arbitrary paths. A droplet is associated with a view, which turns the output into HTML, an email message, or other formats.

Object-relational mapping is safer and more maintainable than inline SQL for database interaction. Vapor includes the Fluent ORM framework. Currently it supports MySQL, PostgreSQL, MongoDB, and SQLite. Independent developers can add more databases by implementing the Database protocol. The protocol is based on models, which map database fields to typed values. This means fewer chances for mistakes, including deadly SQL injection vulnerabilities. Design that prevents mistakes is the Swift way.

Third-party extras

Some important contributions to the Vapor environment come from independent sources. This not only expands the options but shows the breadth of interest in Vapor.

  • SwiftyBeaver is a logging system, originally developed for Apple devices running Swift code. It has worked with Vapor since late 2016, simplifying Web application logging.

  • Flock handles automated deployment of Vapor web applications. Once the deployment is set up, issuing a new version requires just one command. Another logging package is Heimdall, which logs HTTP requests.

  • SteamPress is a blogging platform, written in Swift and running on Vapor.

  • Vapor Forms facilitates creating and handling reusable Web forms.

Building HTML with Leaf

The point of building a Web application is to generate HTML. The piece of Vapor that allows this is the Leaf template engine. It uses a small set of strict rules to define pages that include Swift functionality. The language supports variables, loops, and arrays.

Leaf defines a basic set of tags, and Swift code can define custom tags. You don't embed Swift into a Leaf file; you create a LeafRenderer and set it as a Droplet's view. This provides a clean separation of computation and HTML. With many other languages, HTML and executable code mix together in a way that can become messy to maintain. In principle, Leaf could be used with other languages than Swift.

Requirements for Vapor

Writing Swift code for Vapor requires the latest version of XCode. This means you have to do development on a Mac. However, you aren't limited to deploying Droplets on Apple hardware. It will also run on Ubuntu Linux, AWS EC2 with Ubuntu, and Heroku PaaS. Deployment in a Docker container is an option.

This does mean fewer deployment options than with better-established languages. But the upside is that experienced Apple developers can expand their skills into Web development ahead of everyone else. As Vapor expands onto more platforms, they'll be the experts.

A Linux compiler for Swift exists, so providing a Vapor development environment there will undoubtedly follow soon. JetBrains has a Swift plugin for its CLion IDE, though it doesn't support Vapor yet. There has been work on a CLion Leaf plugin.

These are still the early days for server-side Swift. The Vapor framework and the work on Linux are helping Swift to break loose from being an Apple-only language. With its combination of speed and modern features, it's in a good position to become one of the dominant languages on the Web.

A person working on web application to generate HTML using a Mac.
We hope you liked this article?
If you would like to receive emails about news and further articles we publish please subscribe to our Newsletter.



Shoot us an email, we’ll find a way to be helpful.

No pressure. Really.

English - Deutsch