/ Server

Server side Swift with SwiftNIO

Learn the fundamentals of Apple's SwiftNIO event-driven network application framework to make non-blocking server side Swift apps.


After I was placing all my bets on the server side Kitura framework (by IBM) last month, Apple made a surprise move. They've released a low level backend framework on the 1st of March 2018, now the Server API project is the thing of the past. 😅

Time to say hello to SwiftNIO!

SwiftNIO

Event-driven network application framework for high performance protocol servers & clients, non-blocking.

Never heard about Netty until SwiftNIO was released, but it seems like that the creation of Norman Maurer is used by many big companies around the globe. 🌏

I know that JAVA is behind Apple's server stack, but is this the first sign of that in
5-10-20-x years everything will be written in Swift? You should not forget that Craig Federighi already told us: Swift totally rulez! Now Chris Lattner works for Google. Is he a secret double agent spreading the use of Swift? He's modest goal for Swift is total world domination!!! Totally makes sense. 😎

SwiftNIO is a low level framework that relies on non-blocking IO. Network operations are non-blocking from the processing thread perspective. All the blocking operations are delegated to additional channels, those trigger events on network operations. Yep you have to deal with all the low level HTTP stuff by yourself. 🤓

The purpose of SwiftNIO is being a fast, stable and scalable underlying tookit for building high performance web frameworks like Kitura, Vapor and other network service (not just HTTP) providers.

So please, do not use SwiftNIO directly unless you have a superior understanding of network layers, event loops, pipelines, channels, futures and many more... 😳

What should I use as a Swift backend framework in 2018???


Kitura

In my previous post I was embracing Kitura, but now after a month of real on-field experience I can honestly tell you that it was a very disappointing experience for me. The truth is that it's simply not ready yet. Codable routing is a great idea, but it has a terrible implementation, and the codebase is clearly growing over the authors. 😢

Here are a few reasons why Kitura is not the ideal choice for me:

  • No custom JSON coding strategy option
  • Heavy JSON format dependency hardcoded into the routing system
  • Only a few supported mime types, missing ones are going to be filtered out
  • No charset declared for JSON output by default (also no option to set one)
  • No way to add custom output with codable routing (only JSON)
  • Slow development pace, with not so polished codebase

What about the good parts?

  • Codable routing

Codable routing was the main advantage for me, but it seams like it's also the biggest weakness of Kitura 2.0 right now, because it's clearly in a work in progress stage. You know it feels like Kitura is just an internal "toy" of IBM that no-one really used yet in the outside world. Maybe I'm wrong, so if you are using it in production for a real web server, please let me know. Now that I have a way better understanding of the framework it's time for me to leave Kitura for a while, but maybe I'll check back on Kitura 3.0... 🙄 Farewell. 👋


Vapor

First of all I'd like to give an honorable mention to team Vapor:

This was the moment when I made a decision that I'll definitely use Vapor for something in the future. I'm learning, but here are the advantages of it so far:

  • Amazing codebase (design, style, modularity)
  • Great community (it's also the biggest)
  • Well-written documentation and tutorials

The bad parts?

  • Still a little odd API for me

Learning new things is fun, but it's more important that it'll give you valuable knowledge and better understanding of how things work. In my case I'd love to know more about Vapor, because I know that it'll help me to get better in writing backend applications in Swift language. 📖

You should learn Vapor 3.0 too!

That's why I'm going to make detailed tutorials after the new version comes out. Also you should listen to this podcast with Tanner Nelson the creator of Vapor.


The "great takeaway" from this whole situation is that even if you think that you are sure about something, Apple can come and turn everything upside down right away with a secret weapon or not so secret, but sudden move. 🙃

External sources