Modernize Application Development with Server-Side Swift
Technological innovation has given rise to many new languages, each offering specific features and advantages. Being in the field of software engineering, teams continuously feel the urge to upskill and get updated with the latest computer language, new-age technology and tools. Skill development in any language is a time-bound investment requiring knowledge, practice and experience. How about gaining proficiency in one language while developing skills to be able to work or understand the development cycle of different platforms? Sounds interesting? This blog covers the possibilities of using Swift on Server-side for application development.
The Evolution of Swift for App Development
Initially, Apple used Objective C to create applications. However, Objective C had some drawbacks on performance, complexity, syntax and maintenance. To overcome the drawbacks, Apple introduced Swift in 2014 WWDC (Worldwide Developer Conference).
Keeping the important aspect of performance in mind, Swift, a modern type-safe programming language was created. Swift language is among the fastest growing programming languages within the software engineering industry.
When a major update of Swift 2.0 was introduced in WWDC 2015, people thought the new language was designed to replace Objective C. But no, the language was supposed to stay long.
Moreover, modern digital products and application development isn't just about building for one platform. With growing time, engineering teams need something that can be molded to work on different platforms.
Having an architecture which uses a single language to build an application‚Äôs frontend as well as backend increases code reusability, saves development time and provides development interoperability through cross platform collaboration. Also, maintaining an application would become much easier. Apart from this, engineering teams, especially developers, can expand their capabilities on different platforms without having to learn a new language.
In December 2015, Swift Language, its supporting libraries, debugger, and package manager went open-source. This gained traction from many programmers. This charted the pathway for Swift usage on Linux, Android, and other platforms. Swift.org is the dedicated site to the open source Swift community, where developers can contribute and interact with each other to help evolve Swift.
However, when working on an application, it's quite often that the need for a server-side component would arise. Whether it's to store data, perform analytics, or provide levels of communication, servers become an integral part of the application‚Äôs ecosystem. Usually, when working with server-side components, it requires working with different languages. Besides having to continuously iterate and translate your ideas from one language to another can cause frustration and downtime.
Need for Server-Side Swift
In WWDC 2016, Server-side Swift was first introduced. Now why Server-side Swift?
Speed: One of the significant advantages of using Swift on Server is speed. It allows calling C code directly. If we compare this to Java, there is a requirement of JNI wrappers or Node.js/Python. This may require binary wrappers for any Objective C based interaction.
Server-side Swift is 2.6x faster than Objective C and 8.4x faster than Python.
Swift on Server promotes strong type which reduces errors and crashes at run time. Its syntax encourages engineering teams to write down clean and consistent code which can feel strict sometimes. But this provides safeguards to prevent errors and improve readability.
Swift on Server requires just half the memory occupied by Java, and delivers the same performance. Node.js requires less memory than Java but is still more compared to Swift. It can get more containers for an equivalent amount of cash, and thus run more instances of your application. Swift is the ideal language to run on the server.
Foundation kit, Dispatch Queue and XCTest:
While developing web applications, we can make use of asynchronous libraries.
Software development engineers (SDE) can step through it, add breakpoints, and have type checking, code coverage, profiling, and all those tools work on backend Swift.
Compiler Command Line Tools:
Apple developers have been working on SwiftNIO (a non-blocking IO architecture). It's been a base for all web frameworks available on Server-side Swift. It is an async protocol where rapid development can happen. It can handle multiple requests at a time and be very fast to provide information.
Swift has efficient memory management without the overhead of garbage collection. Swift's memory management is predictable and deterministic. Compare this to Java/JVM and the other garbage-collected language generally, during which the runtime might initiate a garbage pickup pause at the foremost inconvenient moment and freeze everything when you're serving a customer request.
Since the inception of Swift on cloud, programmers are coming out with different frameworks to write web application, API, server application in swift.
Key Server-Side Swift Frameworks
- Vapor is the most used framework created by Taneer Nelson & Wright and introduced in Feb 2016.
- It uses Fluent ORM framework which provides support for PostgresSQL, SQL, MySQL, MongoDB databases.
- Leaf is the native templating engine provided by Vapor. Apart from leaf, it also supports Mustache and Markdown.
- It uses SwiftLog for logging output.
- Vapor is more developer-centric when compared to other frameworks as it tends to move aggressively to provide the latest update whenever a new Swift version is launched.
- Vapor APIs and supporting libraries are majorly developed considering Swift. Hence, it appeals more to developers working on Apple products.
- Perfect was released on 23 November 2015 by Canadian company PerfectlySoft with years of experience in Server-side technology.
- Perfect uses StORM framework which is built on the top of Perfect framework. It supports database PostgresSQL, SQL, MySQL, MongoDB, CouchDB, MariaDB, FileMaker, Redis.
- It uses Mustache and Markdown for templating.
- Apart from mentioned tools, there are a plethora of third-party tools supported by perfect for routing, templating, and email support.
- Its libraries are closer to Vapor but do not have native route type validation and command line extension support. But it allows developers to explore Server-side Swift with third-party integration.
- Also, updates provided by Perfect community are slower compared to Vapor.
- Kitura is developed by IBM. It also provides other cloud application services which can go with Kitura. It was first introduced during WWDC 2016.
- It uses Swift-Kuery ORM framework built over Swift-Kuery, which is pluggable SQL database. It supports PostgresSQL, SQL, MySQL, MongoDB, CouchDB.
- Mustache, Markdown and Stencil can be used for templating as Kitura does not have any native templating engine.
- Helium logger is the library used for logging output in Kitura.
- Kitura uses a traditional development approach. Its APIs remarkably remained stable given Swift‚Äôs own aggressive evolution over the last few years.
In spite of having enough contents and frameworks to help initiating the use of Swift on cloud, why is Swift not yet extensively used as a server-side language? Because, Swift on Server has some pain points like:
- Asynchronous architecture with callbacks and async calls is a tedious part to learn.
- There are no good documentation/guidelines when it comes to performance.
- A tool like Xcode which is unavailable in Linux.
- Swift on Server has basic packages but does not have advanced packages. It has a lot of things to catch up with as there are packages unavailable or abandoned.
Way Forward for Server-Side Swift
There are definite advantages on safety and performance while using server-side. But Swift still has a long way to go to be compared with more mature languages like JAVA, Node or Erlang. Apple ought to improve on certain points in order to gain a stable position amidst server-side players.
Swift on Server isn‚Äôt yet a clear winner. Although the Server-side Swift community is very open and available to help developers where needed.
Apple is using Swift in production for services. Sponsors of Vapor framework such as Skelpo are also the most active users. Considering that those sponsors are consulting businesses, we could expect their clients to be using Server-side Swift in production for a while. So technologically, adopting Swift on Server in a small capacity, is an effective choice.