Server Side Swift with Slimane

Yuki Takei
4 min readMay 15, 2016

This is my first posting on Medium.

What is Slimane?

An express inspired micro Framework and HTTP Server for Swift3. https://github.com/noppoMan/Slimane

Before introduction for it, Can I have a little time to tell the story why it is began?

First, I spent the most of time as infrastructure, backend, data analysis and web development in my engineering life. So I haven’t enough experience with Swift(Especially iOS and Cocoa). But in march 2015 I had the opportunity to build an iOS app for LifeClips(Medium like Blogging platform) and then I used Swift for the first time. Soon I was love into Swift syntax, such as strong type system and the flexibility that enough to be compared with LLs.

Swift become OSS

Time goes by… Finally it become Open Source at Dec 2015. then I thought I should make something and it would be Slimane.

At first, it’s just weekend project as my hobby. Though in hobby, I’ve been making a lot of functions/modules day by day that are required for modern web systems and someday I realized it may use in the real Project…(I’m waiting for Swift3 releasing)

Features

  • 100% asynchronous with event loop backend (libuv)
  • Standalone HTTP Server
  • Faster
  • Modular
  • Full managed asynchronous Stream, TCP, Pipe, FileSystem, Process, Timer etc..
  • Adopts Open-Swift

Available Middlewares and Modules

  • WS: Websoket Server/Client
  • Hanger: Asynchronous HTTP Client
  • Thrush: Lightweight Promise implementation
  • QWFuture: Future implementation with `uv_queue_work`
  • Session: SessionMiddleware to enable session in the Slimane app.
  • Redis: Redis Client and Redis Store for Session
  • BodyParser: For parsing JSON and formData in the HTTP Request’s Body.
  • TemplateEngine: Mustache and javascript templates

and more… https://github.com/slimane-swift

Getting Started

The goal was to get a swift server up and try to play with some examples. Don’t worry in this post, you don’t need to write any codes. Just try them!

Installation

Follow the Install Guide to setup your machine.

https://github.com/noppoMan/Slimane/wiki/Install-Guide

Build and launch Server and App

cd /path/to/your/slimane-example

make debug

.build/debug/SlimaneExample

The Terminal said, the Server listened at 0.0.0.0:3000. Things are looking good so far, but let’s check it in a browser.

it seems to work fine!

Try more!

sliman-example has practical examples to make a web application with a lot of modules. I picked up the some interesting examples from it and introduce them in the following.

Chat app with Websocket

This was running on Local machine but if the Redis Client will support Pub/Sub, We can broadcast the packets between the server and server. I’m also considering to implement Socket.io backend with Slimane.

Working with Blocking apis

Currently, our project doesn’t have any Mysql Client yet. Don’t worry we can use some of oss them in Swift such as Zewo/Mysql, novi/swift-mysql etc.

Problem with blocking apis

But there should be a problem. Almost Mysql Clients have blocking apis due to depend on libmysqlclient…. it means event loop is blocked while waiting the operations. So that means We should make it by full scratch?

Nope!!

QWFuture and Thrush

We can avoid to block with QWFuture. It’s a wrapper of `uv_queue_work` with Future syntax as you know. and it can be combined with Thrush(Promise) to make a modern asynchronous flow.

In the closure of QWFuture<T> is performed in a separate thread and it’s not blocked. when the task is finished or threw an Error, future.onSuccess or future.onFailure will be called.

Cluster mode on the Multiprocess Environement

A single instance of Slimane runs in a single thread. To take advantage of multi-core systems the user will sometimes want to launch a cluster of Slimane processes to handle the load.

Codes for initializing a Master and Workers Processes(It’s little looks like Node.js’s Cluster module)
pass `— cluster` option to the executable
the requests were round robin to each workes

How were those? Slimane has more functions to build web apps such as Session/Cookie, JSON, Rest api server etc...

What now?

Actually, Writing Server Side codes with Swift so much fun and it might be next mainstream for the web development. So I believe Slimane will not be a major FrameWork but also It should have an important roll to tell the people how server side swift works and that’s so fun.

Anyway, I’ve been making Slimane as alone… So I want to make a community for that. So if you have a bit interest in Slimane, Please contact me(yuki@miketokyo.com).

--

--