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

Share this article

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?

Frequently Asked Questions about Server-Side Swift with Perfect

What is Server-Side Swift with Perfect?

Server-Side Swift with Perfect is a powerful tool that allows developers to build server-side applications using Swift, a popular programming language developed by Apple. Perfect is a server-side Swift framework that provides developers with the tools they need to build robust and efficient web applications and services. It includes a suite of integrated tools for managing databases, handling requests and responses, and even managing user authentication.

How does Server-Side Swift with Perfect compare to other server-side Swift frameworks?

Perfect is one of the most mature and feature-rich server-side Swift frameworks available. It offers a wide range of features and tools that are not available in other frameworks, such as Vapor. Perfect also has a more extensive community and better documentation, making it easier for developers to get started and find help when they need it.

Is Server-Side Swift with Perfect suitable for large-scale applications?

Yes, Server-Side Swift with Perfect is suitable for large-scale applications. It is built to handle high levels of traffic and can scale to meet the needs of large applications. It also includes features for managing databases and user authentication, which are essential for large-scale applications.

How does Server-Side Swift with Perfect handle database management?

Perfect includes a suite of tools for managing databases. It supports a wide range of databases, including MySQL, PostgreSQL, and MongoDB, and provides developers with a simple and intuitive API for interacting with these databases. This makes it easy to perform common database operations, such as querying data or updating records.

Can I use Server-Side Swift with Perfect to build RESTful APIs?

Yes, you can use Server-Side Swift with Perfect to build RESTful APIs. Perfect includes a powerful routing system that makes it easy to define routes for your API, and it supports all of the HTTP methods that you need to build a RESTful API.

What kind of support is available for Server-Side Swift with Perfect?

Perfect has a large and active community of developers who can provide support and help answer questions. There is also extensive documentation available, which covers all aspects of using Perfect, from getting started to advanced topics.

How does Server-Side Swift with Perfect handle user authentication?

Perfect includes features for managing user authentication. It supports a variety of authentication methods, including OAuth and JWT, and provides developers with a simple and intuitive API for managing user sessions.

Is Server-Side Swift with Perfect suitable for beginners?

While Server-Side Swift with Perfect is a powerful tool, it is also user-friendly and suitable for beginners. The extensive documentation and active community make it easy for beginners to get started and learn how to use Perfect effectively.

How does Server-Side Swift with Perfect handle error handling?

Perfect includes a robust error handling system that allows developers to catch and handle errors effectively. This makes it easier to build reliable and robust applications.

Can I use Server-Side Swift with Perfect for mobile app development?

While Perfect is primarily designed for server-side development, it can also be used in conjunction with Swift on the client side to build mobile apps. This allows developers to use the same language for both server-side and client-side development, simplifying the development process.

Chris WardChris Ward
View Author

Developer Relations, Technical Writing and Editing, (Board) Game Design, Education, Explanation and always more to come. English/Australian living in Berlin, Herzlich Willkommen!

backendchriswswift
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week