Web Development with Swift

What Server-Side Swift means for web development and why you should care

Gianluca Tranchedone
The Swift Web Developer
6 min readFeb 1, 2017

--

Since its announcement in 2014, Swift has taken off to become one of the most popular programming languages on the planet. Not only that, according to a survey by StackOverflow, Swift is the second most loved programming language; the third most trending as well. In just over three years, that’s a remarkable achievement.

For some people, what made Swift appealing was the new syntax, the modern and functional features, and the inherent safety. For others, it was the promise Apple made to Open Source the language within a year from its launch. That made experienced Objective-C developers dream of using the same tools they were already familiar with to build scripts and server-side applications.

Swift in the Cloud

In December 2015, Apple kept its promise and started adding support to the language and standard library for Linux. An effort is also being made to port Foundation, libdispatch (also known as Grand Central Dispatch) and other libraries to that operating system.

Surprisingly, the Swift repository attracted quite a response with several pull requests opened on day one. Several developers around the globe are contributing to the language, while others focused their efforts in building libraries and frameworks. With Swift open source, many developers dedicated time to building frameworks for server-side development.

One year later, in fact, at WWDC 2016, Apple has even brought IBM on stage to talk about their web framework: Kitura. Shortly after, Apple announced the creation of a new group within swift.org, dedicated to the design and development of Swift Server APIs. This group is made of people from Apple as well as the major Swift web frameworks: Kitura, Vapor, Perfect and Zewo.

WWDC 2016 session introducing IBM’s Kitura

Clearly, Swift on the server is getting traction and a big push from Apple as well. In fact, although many developers don’t even know what Swift is, interest in using it to build Server-Side applications is growing by the day.

Just three months ago, Perfect, the most popular web framework for Swift right now, had over seven thousand stars on GitHub. Today, that number has grown to over ten thousand. Vapor, the second most successful, follows with almost nine thousand stars. By comparison, Ruby on Rails, one of the most famous open source web development frameworks of all time has collected about thirty-five thousand stars over its ten years lifespan.

Is this hype worth the attention?

The answer is simple: absolutely yes!

Apple is pushing hard on Swift. A lot of effort is being made to ensure the language is easy to understand and easy to learn; that it’s performant and fun to use. Companies like IBM are betting on it, are investing heavily in its development and are building tools built on top of it.

Let’s have a look at the actual benefits of adopting Swift on the server.

Swift is performant!

Benchmarks show that Swift is much faster than most other languages traditionally used on the web.

Swift speed performance. Source: https://realm.io/news/tryswift-chris-robert-end-to-end-application-development-swift-backend/.

Similarly to being performant, Swift also has a very low memory footprint compared to other languages.

Swift memory performance. Source: https://realm.io/news/tryswift-chris-robert-end-to-end-application-development-swift-backend/.

The existing Swift web frameworks greatly benefit by these qualities of the language and perform better than popular web frameworks used in other languages. Just as an example, Ryan Collins recently compared the performance of the major Swift web frameworks against Node.js. These are the results:

Economy

In the current Cloud economy, memory and CPU usage are among the key measures used to charge for hosted infrastructures such as AWS. With the performance it provides, businesses can save real money by moving to Swift.

Safety first

Swift is a type-safe language, which means the language helps you to be clear about the types of values your code can work with. If part of your code expects a String, type safety prevents you from passing it an Int by mistake. Likewise, type safety prevents you from accidentally passing an optional String to a piece of code that expects a non-optional String. Type safety helps you catch and fix errors as early as possible in the development process.

Developers productivity and mobility

Another benefit is that Swift developers can own a whole project end-to-end. By building both server and client sides using the same tools and technologies, teams can move faster, have a deeper understanding of the entire stack, and onboard new members faster.

Such flexibility can be extremely attractive for companies structured after the Spotify’s model or something similar, where small, contained groups own entire sections of the business.

Agencies and Startups can also benefit from having a more homogeneous team by moving people as needed and therefore potentially elude times when business is slow or when there’s sudden spike in workload. Again, being able to do so translates into real money savings.

A vast community

There’s an incredible community surrounding Swift already, made of course by iOS and macOS developers. And that community is also one of the most active in the Open Source space right now. The Swift Package Manager already counts dozens of packages built for Swift, and many others will be added over time. Nowadays many frameworks built in Swift only support CocoaPods, although they have no dependency on iOS. We can only hope those will be made available via the Package Manager soon.

Alternatives

I’ve recently come across this article by Eric Elliott where he urges developers to use JavaScript for developing both servers and clients:

JavaScript is the standard language of the web platform, and your app should be written in JavaScript on both the client and the server side. Why?

JavaScript is the most popular programming language on the planet, and the easiest to hire for.

There are more working open-source solutions in JavaScript than there are in any other language. Your team will have to write less code from scratch.

JavaScript is the only language that can run universal code natively across the entire web platform stack, including clients, servers, and mobile.

Those are all valid points. Although he was referring to browsers, native apps can be developed in JavaScript using React Native; therefore you could build everything using one language. However, platform-specific code has to be ported first or bridged to the JavaScript engine to make it work, and performance can be an issue.

I see Swift being in a similar position: it can be used to build native apps for all Apple devices, it can be used to develop server-side apps and in templating languages such as Leaf to render HTML, and it can even be used to build Android apps (sort of). Sure you’ll probably need JavaScript on the web, but the safety, performance, cost savings, and developer happiness that come with using Swift outweigh this, in my opinion.

Swift is going to be a big part of the web’s future

We’ve seen how Swift is getting incredible traction from the community. We’ve also seen adopting Swift for server-side development comes with several advantages, including team productivity and costs efficiency.

Top companies outside of Apple are investing in the development of Swift, web frameworks and packages. And let’s not forget that Swift is a fantastic language to for teaching programming, and several schools and universities are teaching CS courses with it.

To conclude, I’m very excited about Swift on the server and for web development in general. I believe we’re going to see an increasing number of people and companies moving in this direction in the coming months and years. It’s a fantastic time to be a Swift developer.

Subscribe to Swift Web Weekly!

If you’re interested in web development with Swift, Swift Web Weekly is a free newsletter I’m curating with interesting links related to server-side Swift, HTML, CSS, JavaScript, tools and more. If you don’t like it you can unsubscribe at any time, so why don’t give it a try?! 🙂

--

--

Gianluca Tranchedone
The Swift Web Developer

Software Engineer and Investor. I write about programming and investing.