Hacker News new | past | comments | ask | show | jobs | submit login
Vapor: a type-safe web framework for Swift (github.com)
200 points by elithrar on Feb 21, 2016 | hide | past | web | favorite | 70 comments

How is this "insanely fast"?

Running an example main with the route '/' mapped to return "Hello World"

   ab -n 10000
I get 1400 requests/s in debug mode and 1900 requests/s in release mode.

With wrk, I actually get between 3000-4000 requests/s with default parameters. Most of the time. Sometimes it just hangs. Trying to increase the number of requests with the -r parameter made the hangs consistent, I was not able to get wrk to finish in more than a dozen attempts, so there appears to be a problem with it dying under load.

For comparison, the built-in Apache gets 15000-20000 requests/s, my own web-framework based on libmicrohttpd + an Objective-C/Objective-Smalltalk wrapper gets around 25000-32000. None of these would be classified as "insanely" fast, and they are around an order of magnitude faster than this. It's actually a lot closer to Ruby/Sinatra (~600 requests/s).

EDIT: project readme says "insanely", not "blazingly", so updated.

At least the insanely fast speed of Vapor also goes well on the client side with Vapor.JS, the world's smallest and fastest JavaScript library: https://github.com/madrobby/vapor.js/tree/master

I prefer VanillaJS: http://vanilla-js.com/

It is perhaps not the definition of "insane" that you are looking for.

If so, one that ignores the performance of the alternatives.

"insanely" fast was meant as a comparison to languages like PHP and means both performance and time it takes to set up.

Good to see people getting serious about Swift cross platform projects. Let's hope it doesn't share the fate of Apple's Dylan projects.

Been looking into this recently so leaving it here as friendly competition: https://air.mozilla.org/ur-web-a-simple-model-for-programmin...

I also really have great hopes for Swift as a modern, x-platform, general purpose, compiled language. Go is not general purpose; Rust is too low level. Haskell has a too steep learning curve.

Ur/Web is really great, but not at all general purpose.

Why am I hung up over "general purposeness"? Well when I learn a language, it takes quite a bit of my time; then I want to be able to apply it broadly.

Therefor I shun Node.js, Go and Erlang/Elixir.

Julia is general purpose and really fast. You can make desktop and web apps in it.

Ur/Web is for web and thus not general purpose. I can't say if Ur (without web) is fit for general purpose use.

> Go is not general purpose

Can you explain?

There is no arguing with Turing equivalence of Go and most other languages but there are practical limitations which prevent more complete general purposeness.

I like Go since the beginning. However, Go cannot be easily used for kernel development, soft or hard real-time or embedded systems (no linker script support and porting runtime is weighty) and there are few transpilers.

Rust is slightly better, because PoC kernels have been put together with fewer hacks. Haskell, C, Ada, assembly are much more general purpose in terms of being able to build something like seL4, an F-15 cockpit, an engine management system or compiling code as ASIC circuitry. (Perl's supposed use in nuclear armaments notwithstanding.)

PS: I would like to see some geninuses come up with a back-back-end to LLVM or a disassembler which could formally prove binary satisfiability of program correctness without mind-bending, gibberish, over-mathematized, incantations inaccessible to us mere, stupid mortals.

Which language doesn't have "practical limitations"? And it's no doubt more "general purpose" than assembly. How much webdev are you going to do with assembly?

Pointers please to someone working on a kernel in Swift... :)

How is Go not general purpose?

Try to build a mobile app with Go.

How are you then defining general-purpose?

I'd say a language is general-purpose when it has not been tailored to a specific scenario or area of problems, but to work well for most. Isn't that, to a great extent, true for go?

You can build a mobile app in Go, for both Android and iOS.


I didn't say you can't do it. I said, try to do it. The phrase "general purpose" must be stretched considerably in such a case.

Is there a language you _do_ consider "general purpose"?

Yes. More than one, actually.

Why would you compare it to Dylan? Swift will be the most popular iOS language within 18 months. If you look at the new iOS books that are released, most use Swift. I count over 3 dozen books within 18 months of Swift's release:


It's weird how little qualifies for 'type-safe' nowadays. The templating language is not typesafe for example. The parameters in the request aren't either. Take a look at http://haskell-servant.github.io/ if you want to see what type-safe means.

Indeed, i don't see anything about it that's particularly type-safe, and the claim is not discussed beyond the bullet point at the top.

We are working hard to make all aspects of Vapor totally type safe. But, just by merit of being Swift, it's already infinitely more type safe than something like PHP.

A key goal of the project is to avoid the use of `Any` anywhere possible, and to move any run time safety checks to compile time.

Almost anything is better than PHP, that doesn't make this particular framework standout among many tens of others.

In any case, I'm not going to consider using any framework that isn't on the first half of this list


Many of them are for JVM, but one cool thing about JVM, is that you don't have to use Java (Kotlin is often compared to Swift for example) while still using frameworks written in Java, like Undertow or Rapidoid.

Is there documentation I'm missing? Based just on the readme I don't think I understand how this works. For instance, the section on the Request object omits HTTP headers, then the Async section shows sending response headers manually. But they're sent asynchronously for some reason? There's a .Hash method that apparently does SHA-1 hashing, though I don't know why. At the point I stopped reading because I didn't understand what the examples were telling me.

I don't mean to be negative or try to poke holes in your hard work. I'd just like to know how to use it. "Insanely fast, beautiful syntax, type safe" are super alluring features! But let me know how I can build a real application with it.

There is additional documentation in the Wiki.

But why? Why would you create a web framework in a single-thread environment? This line of code: https://github.com/tannernelson/vapor/blob/master/Sources/Se... - makes this framework totally useless.

The server isn't single threaded. It uses the dispatch concurrent thread pool for processing. The (granted, ugly) line you've linked is for blocking and keeping the main thread alive so the process doesn't end. However, the requests are processed on the dispatch "Background" queue. See "boot" here:


Do you realize that node.js is single threaded? Having more than one process works fine for most purposes. Because the bottlenecks are more IO driven than CPU time.

Please check the function I am referring to.

Also, yes, I think concurrency model in node.js is the worst possible one.

I saw it. It's just a function to keep the main thread, ie. main() alive while the worker threads process the async events. Nothing out of the ordinary..

And why is it the worst possible one? Preforked threads per request are much worse as NGinX showed Apache

This line is just meant to keep the process alive. The actual implementation of the ServerDriver uses threads. It's also worth mentioning that Vapor is totally protocol-based and modular, so you can easily plug in any class that conforms to ServerDriver and replace this logic.

Because it would be nice to run a web server on my phone? I'd love to have something like sandstorm in my pocket.

You can already do that.

I suppose most who do Swift at the moment also use it on front-end, since this advertises being "type-safe" what is their preferred method to connect to this JSON server from the Swift front-end in type safe manner?

Also, the Fluent is clearly not type-safe, it's stringly typed ORM to database. Since many backends are just fancy wrappers to database, this part should be most important for type-safety. It's nowhere as type safe as Quill, Slick or JOOQ.

> I suppose most who do Swift at the moment also use it on front-end

Given that it's primarily used for iOS and Mac OS X programming, yeah, of course. Both the GUI and app logic are typically coded in Swift.

This looks excellent and reminds me of the simplicity of Flask.

Once Fluent supports MySQL, I'll definitely use Vapor in a project.

The JSON response trick is just the right amount of magic.

I'm eager to make Swift my new web dev language and this looks like a very promising option if they continue to maintain that balance.

Honest question: what's the appeal of Swift outside of iOS/OSX development? I can understand the need for an Objective-C replacement, but after more than two decades of evolution, web dev doesn't lack of good and modern languages running on top of very performant and mature platforms, with rich ecosystems. Swift brings absolutely nothing to the table.

Turn that around: lets say you know Swift because you're an iOS developer. What web dev languages or platforms can you use?

The same argument was made about Node.JS, that backend services already had great languages / platforms / ecosystems.

It's not about existing experts in the area learning a new stack, it's about enabling experts in one domain to reach 'good enough' in another rapidly.

Frameworks. Frameworks Everywhere.

If you like Ruby on Rails you will like Swifton for Swift https://github.com/necolt/Swifton .

I'll admit my total naivete up front: isn't Swift already a typed language? Is there something about being a web framework that necessitates some other kind of type safety?

The more advanced a type system is, the more it depends on what you do with it when designing an API. A well designed API in a language with a rich, expressive type system can make higher-level guarantees. On the opposite side of the spectrum you could just use Strings for most of the types (it’s web after all!) and it would still be type safe, although the type guarantees wouldn’t be of much use.

Yes, this framework in particular doesn't demonstrate the benefits and uses of the type-safety and compile-time safety in Swift.

Check out Frank (https://github.com/nestproject/Frank#routes) which offers type-safe and compile-time safe path routing.

You define a closure to handle requests matching paths with type-safety. You are passed the correct parameter types and the correct amount of parameters directly to your closure. This gives you compile type safety.

    // Handle GET requests to path /users/{username}
    get("users", *) { (request, username: String) in
      return "Hello \(username)"
In contrast, without this compile-time safety you will most likely be passed an array of Strings and you will have to pull items out.

    Route.get("users/:username") { request in
      if let username = request.parameters["username"] {
        // Return something with username
      } else {
        // TODO Return 404
Here you have to manually validate these parameters, there is also the lack of compile time safety in the way you are writing a hard-coded string for each parameters. There is a string "username" twice, both the path and when you pull out the parameter from the array. There is a large room for user error or typos here.

Thanks for the clarifications. As soon as web text-based protocols were mentioned, I said "oh duh" to myself. There's nothing about the availability of a type system that forces a plain-data wrapper library to use it.

Reminds me of Clojure's Compojure library

What's the best GUI IDE for a new comer to swift?

Xcode or AppCode(from JetBrains).

Both run only on mac if I remember well? Anything that could run on ubuntu?

All the Jetbrains IDEs are cross platform (Linux too)

Except AppCode.

Not this one.

I've been using vim + vim-swift (https://github.com/keith/swift.vim) to learn Swift, with great results ..

Oh dear. Already a 2nd web framework in Swift. Are we about to have a cambrian explosion of Swift web frameworks, ensuring obscurity for all?

Competition is good. Two frameworks doesn't mean there'll be an explosion.

The mistake most of these make is they are both web frameworks and they have their own build in web server. We should learn from mistakes in previous communities such as in Python (https://www.python.org/dev/peps/pep-0333/#rationale-and-goal...) and not tightly coupling these things together.

A user should be free to choose their favourite web framework and then choose the best web server for their needs.

The server behind Vapor can be swapped out with any class that conforms to ServerDriver. So providers could be added to support any Swift web server.

I said "most", not "all" ;).

As of yesterday, I started working on one too!

If I could think of a name, I'd release it right now just to add it to your sweet list.

Interesting you mention so many similar HTTP servers but not Zewo (https://github.com/Zewo)

JavaScript is a bit of a "special" example.

This is a new one:

https://github.com/necolt/swifton (A Ruby on Rails inspired Web Framework for Swift that runs on Linux and OS X)

What's the first one?

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact