Server-Side Swift and its frameworks

Abhishek NC

Ever since swift has become open source, there has been no boundaries to imagination, since swift can be implemented across different platforms. Apple has swiftly extended its support for server side development.
Realizing the fact that swift has its support for server side, let us see how swift is going to revolutionize server side development.

Why Swift on server is beneficial

Although there are many competitors like node.js and Ruby on Rails which are well established, swift is considered to be the better performer at server end because of its memory usage and task completion time. The amount of physical memory that’s needed to run a task is very less in a case of swift and the time it consumes to complete a task are excellent compared to other competitors. The minimum memory need of swift makes it even best for pushing your application to cloud.

From developer perspective one doesn’t clearly know what happens when they make any network call, having a server-side app running swift on it would provide a lot of customization option and easier troubleshooting of issues.

When there is swift implemented on both server end and client end it is possible to reuse and share the code.

Google Certified Agency

Server-Side Swift

With the need of consistent platform between client and server, the architecture was designed such that Standard libraries, Foundation and Dispatch were made available. Use of dispatch for concurrency would result in a greater performance. And finally in order to run code successfully at the server end certain features like networking, security if you’re going to do HTTPS, and secure web sockets and some form of HTTP parsing were added.

swift 1

Architectural diagram of BluePic app developed through Kitura

IBM developed a framework with all these functionalities, named Kitura. BluePic was developed as an example of what is possible with an iOS app running on a Swift Kitura-based server. BluePic is even open sourced so developers get a clear insight of developing swift based server application. BluePic is developed making use of services provided by IBM’s own cloud Bluemix.

Server side Swift Frameworks

Development at server side with swift can be made possible with lot of handy frameworks. With these written in Swift, a developer can develop their Swift application on the front end and easily move to developing on the back end, since Swift runs well on both front and back end.
Here is a list of some of the available frameworks

As we have already discussed Kitura is being developed by IBM, the syntax for Kitura looks similar to Vapor. We import Kitura, we create our router object, and then the router binds something to slash.

In Server-Side Swift we send a response. Here is the sample code

import Kitura
let router = Router()
router.get(“/”) { request, response, next in
response.send(“Hello, World!”)
Kitura.addHTTPServer(onPort: 8090, with: router)

Perfect is the most popular Swift framework and it was the first to hit the market.Perfect is designed by Kyle Jessup. It has a strong set of features which also provides a global function called PerfectServerModuleInit which initializes the routing handler and links the / route to our HelloWorldHandler. “Hello, World!” is then sent back to the client by a handler and informs the server about the completion of a request. Here is the sample code

import PerfectLib
public func PerfectServerModuleInit() {
Routing.Routes[“/”] = { _ in return HelloWorldHandler() }

class HelloWorldHandler: RequestHandler {
func handleRequest(request: WebRequest, response: WebResponse) {
response.appendBodyString(“Hello, World!”)

The second most popular framework for Swift on GitHub is Vapor. Vapor has the best documentation, and It would be the best option for anyone just diving into server-side Swift. Vapor is built in Swift 3. Here we shall create a sample code with a Vapor application object, and add a route handler for /, where we return “Hello World!”.
import Vapor
let app = Application()
app.get(“/”) { request in
return “Hello, World!”

Server-Side Swift

Zewo is another framework just like Perfect and Vapor. What makes Zewo different from other frameworks is its architecture which is super modular and is actually composed of more than 50 packages. If you want to build your own web server, Zewo architecture makes it perfect for beginners, another important feature is that Zewo base components are similar to that of Vapor and are interchangeable. Here is a sample code in Zewo
import HTTPServer
import Router

let app = Router { route in
route.get(“/”) { _ in
return Response(body: “Hello, World!”)
try Server(app).start()

If you are interested in Server-Side Swift, now is the time to get involved!. You can learn more about each framework and get involved here:

Perfect: Website | Github
Vapor: Website | Github
Kitura: Website | Github
Zewo: Website | Github

360 Videos


about the author

Abhishek NC