Mobile
Article

The Next Full Stack Language? Server-side Swift with Perfect

By Chris Ward

I will keep re-iterating (for the next 6 months at least) that whilst Swift is not the first language from a commercial company to be open-sourced, the enthusiasm and outcomes so far have been astounding.

Swift 2.2 was open sourced just over a month ago on December 3rd 2015 under an Apache 2.0 license. It’s currently hard to find an exact list of projects interpreting and compiling Swift as opposed to written in Swift, but looking at the project’s GitHub repository shows the level of interest, activity and contributions to the project in such a short time frame.

One of JavaScript’s major breakthroughs was the release of node.js, allowing for a common language across an application stack. Could server-side Swift accomplish the same for Apple’s language?

Introducing Perfect

Taking a presumptuous name comes ‘Perfect’ from PerfectlySoft Inc, a development agency specializing in tools for Swift developers, releasing the first server-side Swift implementation.

Before we dive into code and usage I asked their CEO, Sean Stephens some questions about their plans and ideas.

Why create the Swift backend project?

We think Swift is a beautiful language but to appeal to an enterprise development ecosystem, it needs a strong server-side framework.

At what state is your Swift backend project?

We have an early release available and hundreds are already using it in their projects. We believe more people will adopt it in the coming months.

Will the project always remain open source or will it become a commercial project?

It will always be open source, but we plan to commercialize it. We believe that there are opportunities for enterprise licensing and configuration utilities to build a strong business, and we are executing a plan to drive this forward.

What do you hope to accomplish?

We are going to take over the server-side development world.

Is anyone using the project with any applications yet? What has performance been like?

We are using the project in production and in all cases it has performed beautifully. It’s still in early stages, so updates are frequent, but the core product is solid. Swift is a wonderful language to work with and Perfect has already greatly reduced our speed to market.

Do you anticipate the more distinct separation between a mobile device and a server as an issue for uniting the front and backend? As opposed to node.js / JavaScript?

The node/Javascript example is similar to Swift/Perfect. However, node/Javascript are high-level languages, whereas Swift/Perfect are natively compiled. Swift/Perfect will have speed and reliability advantages over node/Javascript as Swift is a strongly & statically typed language which makes it far more suitable for building enterprise systems over JavaScript. As code bases grow around the mobile and server end, the Swift/Perfect union should reduce the distance and dissonance between front and back end development seen in other obtuse stack pairings (such as ObjC/Java, or Java/PHP). The existence of Perfect makes Swift increasingly powerful and useful, which means less code per project, with more scalable results.

What’s next and what do you need right now?

We have been approached by investors and are currently hiring more engineers to build more and better libraries to improve the product as a whole and build enterprise products for our emerging ecosystem.

Using Perfect

Mac OS X

Clone the Swift repository to your machine:

git clone https://github.com/PerfectlySoft/Perfect.git

Inside the project folder open Examples.xcworkspace, this contains a number of example projects you can investigate.

The simplest example to begin with is the ‘Tap Tracker’ application, select the server component in Xcode and click the run button:

Start Tap Server

And start the matching application:

Start Tap Application

Find out more about how the ‘Tap Tracker’ application works in Perfect’s documentation. A quick scan will show how well concerns are separated between the two application components. The server-side application handling database transactions with a mustache template file controlling the JSON output passed to the iOS application. This allows the mobile application to just handle getting a location and sending it to the Swift server endpoint. A next step to try would be adding a view that queries the endpoint for taps made and displays them.

Linux

This is a great start, but realistically, how many run server-side services on Mac OS X? Swift’s Linux support is the real game changer and Perfect supports this. First make sure you have the Linux Swift compiler installed, then install the dependencies required by PerfectLib:

sudo apt-get install libssl-dev libevent-dev libsqlite3-dev libicu-dev libcurl4-openssl-dev uuid-dev git

Clone and build PerfectLib:

git clone https://github.com/PerfectlySoft/Perfect.git
cd Perfect/PerfectLib
make
sudo make install

Build the Perfect server:

cd ../PerfectServer
make

Next we create a shared object for the Perfect server from the project we want to run, so sticking with the tap example:

cd ../Examples/Tap\ Tracker/
make

And copy the resulting output into a new PerfectLibraries folder in the server directory:

cd ../../PerfectServer
mkdir PerfectLibraries
cp ../Examples/Tap\ Tracker/TapTrackerServer.so ./PerfectLibraries

Finally run the server:

./perfectserverhttp

In ViewController.swift of the Tap Tracker application in Xcode, change the END_POINT_HOST value to the IP address of your Linux server and run it. Everything should be the same as when running the Mac Server, but of course we’re running Swift on two separate machines!

What Else Does Perfect Support?

All we’ve done in these examples is start an existing project, but for experienced Swift developers I hope you’ve already started to see some of the potential of connecting front end and server-side Swift. Here are some other features Perfect offers to sweeten the deal.

Apache Support

If you don’t want to use the built in http server, then Perfect provides an Apache module via FastCGI, find out more here.

Connections to Common Server Components

server-side applications rarely exist in isolation and Perfect has connectors for MySQL, PostgreSQL and MongoDB with more to come.

JSON Object Marshaling

Communication between the server and client side happens in standard JSON. This means there are no new ‘standards’ to learn. This also means that your Swift backend doesn’t have to just feed data to a Swift based front end application. server-side Swift feeding an Android application anyone?

What’s Missing?

As noted in my interview above, Perfect is new. Development is progressing swiftly (pun intended) but documentation is patchy with exploration and extrapolation needed to get things working. Fortunately the issue queue is not a dead zone where your cries for help will be ignored.

If you are an experienced Swift developer then you should be able to piece information available into something usable and maybe even production ready. If you are new to Swift then things will take you a little longer right now, but rest assured that you are learning one of the most popular programming languages of 2015 that can now take care of your application across the whole stack.

If you like the sound of what Perfect is trying to accomplish, then they have a comprehensive page detailing exactly how you can lend a hand.

What are your thoughts on server-side Swift? Great Promise or bandwagon jumping hype?

  • http://rafaelstz.github.io Rafael Corrêa Gomes ♛

    Very Cool!

    • Chris Ward

      Thanks!

  • ElDerecho

    Swift has great potential not only on the server, but as a desktop development language too.

    • http://journeythrive.tumblr.com/ Narek

      Swift is already used to built desktop applications on Mac. Some notable apps built with swift include Hipmunk, Clear, Day One, and LinkedIn (source: Apple website).

  • Saqib

    Does Swift run in the browsers? If not, how is it full stack?

    • Chris Ward

      In my opinion, full stack = back end + front end, which in this case is a mobile device, but actually it could be a browser in the future if someone created that functionality. Not sure if there’s much point with Swift having HTML / browser output, but it’s possible.

    • http://journeythrive.tumblr.com/ Narek

      You wouldn’t be able to run Swift inside a browser because it’s a compiled language. In the case of “full stack” this is only pertinent to client-side device (mobile app) and server-side server (something like Perfect). As Chris Ward mentioned, you could use Swift on the server-side to respond to an HTTP request with an HTML page generated server-side using Swift.

  • Jeff

    This is so cool!

  • david

    Hi Guys, Can I Use Swift As Language Programming Like Php ?

    • Chris Ward

      @disqus_MqBTvzDEvk:disqus If you mean for a backend, then yes!

  • Jeremy Camilleri

    Apart from the fact that the Perfect Github download does not contain the workspaces PerfectLib and PerfectServer, there is no Examples.workspace either.
    Having managed to find Perfect v1.0.0 which has these workspaces still PerfectServer HTTP App complains that its AppDelegate.swift can’t find PerfectLib thereby not building as ‘No such module PerfectLib’

Recommended

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in Mobile, once a week, for free.