Swift is a very young language as it was just released in 2014. It was designed to be a language with an easy syntax but that is as powerful as C. In this chapter, you are going to learn why Swift is an excellent choice when it comes to developing server applications. Swift arrives as a fresh alternative to all current and established solutions.
This chapter will introduce you to the server side of Swift; the basic understanding of Swift itself is something you should be familiar with already. At first, you are going to look at Swift as a generic language and why it fits well into the server world. Afterward, we look at the performance Swift delivers compared to other web technologies. A review of the current state of Swift for servers and an introduction to the new features of Swift 5 will sum this chapter up.
The following topics will be covered...
You will need the following software components up and running:
- Vapor Toolbox
- Swift 5
- XCode (11+)
- macOS or Linux
We will assume you are using macOS but all this should work the same under Linux.
You can find all the code for this chapter in the following Git repository: https://github.com/PacktPublishing/Hands-On-Swift-5-Microservices-Development/tree/master/Chapter 2
To write server apps in Swift, we require an understanding of where Swift comes from and how it fits into the world of server languages. Swift was invented by Apple engineers as an alternative to C-derivatives, for example, C++, and, Objective-C, Objective-C++. After initially keeping Swift proprietary Apple then released Swift as open source for macOS and Linux. You might have wondered why Apple would bother to make Swift available on Linux, as Apple doesn't usually release any Linux products (not even iTunes).
Swift was designed to be a native language from the very beginning. It means an application gets compiled into binary code that runs directly on the processor, making it the fastest way for an application to run.
When Apple released Swift for Linux, there was little application. The UI was still functioning only on Apple devices, but a good...
Swift is competing with a good number of frameworks and languages. Some might argue that performance is only one out of many factors that should be considered when choosing a technology. While that is undoubtedly true, think of this: you want to build a project in a way where technology will not hold you back much. Facebook, as well as Uber, has spent years rebuilding and reworking their infrastructure. No matter what technology you choose, you will most likely refactor your application as well. However, having selected a stack that allows you to do so gracefully is crucial.
You could be looking at some raw benchmarks in this section, but I think it makes more sense to analyze with a bit more depth what performance holistically looks like for a project. When comparing Swift to its competition, you want to stay as clean and objective as possible; the following...
What does it look like to run a Swift application on a server? When Swift was released as open source, it was released for Linux right away as well. Specifically, Swift favors the Linux flavor, Ubuntu. Hosting a Swift application is very different from traditional web hosting. If you get a web hosting package from companies such as GoDaddy, you will not see them say "Runs Swift 5". Most mainstream web hosters only offer scripting languages. The reasoning is simple:
- Most applications use scripting languages.
- Scripting applications can share the same server.
Swift, on the other hand, as a native language, is the actual web server itself. What that means is that your Swift application is directly responding to incoming requests. Let's look at the following aspects of server-side Swift:
- A self-contained server
You already have an understanding of how Swift works. In this section, we are looking at some of the newest features that Apple has released at the time of writing this book. There are a lot of bug fixes and changes in every Swift update, but the following features are particularly noteworthy for us in Swift version 5:
- ABI stability
- Raw strings and UTF8 strings
- Result type
- Future enum cases
- Flattening nested optionals
There are a couple more interesting new features of Swift 5. However, this section only contains the ones that will be most relevant for server development. Take a look.
One particularly interesting new feature is that Swift is now ABI-compatible with version 5.0 and later...
In this chapter, you looked at the history of Swift and why it stands out from other languages. You then learned why Swift has been leaning toward server development from the beginning. After looking at Swift's performance compared to other languages and frameworks, you learned about Swift on the server and what is currently supported. Docker and SwiftNIO should be terms you can explain by now. Finally, you explored the features of Swift 5 and have seen examples of what the changes could look like in practice.
Go through the Questions section to validate your understanding of Swift on the server.
In the next chapter, you are going to explore the Vapor framework and you will be set up to create web applications with Vapor and Swift.
To see whether you understood the gist of server-side Swift, try answering the following questions:
- Why is Swift being a native language helpful?
- What is the preferred OS for server-side Swift applications at the moment?
- What is SwiftNIO?
- How does Docker help?
- Name three of the added benefits of Swift 5.