Lightning
(formerly Edge)
Node
Lightning is an HTTP Server and TCP Client/Server framework written in Swift and inspired by Node.js. It runs on both OS X and Linux. Like Node.js, Lightning uses an event-driven, non-blocking I/O model. In the same way that Node.js uses libuv to implement this model, Lightning uses libdispatch.
This makes Lightning fast, efficient, and most crutially single-threaded by default. You simply do not need to worry about locks/mutexes/semaphores/etc if you have server-side state. Of course, Lightning applications can make use of libdispatch to easily offload heavy processing to a background thread if necessary.
Reactive Programming
Lightning's event API embraces Functional Reactive Programming by generalizing the familiar concept of promises. This API is called StreamKit.
StreamKit's architecture is inspired by both ReactiveCocoa and RxSwift.
Why did we reimplement?
- Lightning should be easy to use out of the box.
- Lightning is optimized for maximum performance, which requires careful tuning of the internals.
- The modified API is meant to be more similar to the familiar concepts of Futures and Promises.
- We don't want to be opinionated about any one framework. We want it to be easy to integate Lightning with either ReactiveCocoa or RxSwift.
FRP, greatly simplies management of asynchronous events. The general concept is that we can build a spout which pushes out asynchronous events as they happen. Then we hookup a pipeline of transformations that operate on events and pass the transformed values along. We can even do things like merge streams in interesting ways! Take a look at some of these operations or watch this talk about how FRP is used at Netflix.
Installation
Lightning is available as a Swift 3/4 package. Simply add Lightning as a dependency to your Swift Package.
Swift 3
Swift 4
Usage
Routing
Raw HTTP
TCP
Lightning is not Node.js
Lightning is not meant to fulfill all of the roles of Node.js. Node.js is a JavaScript runtime, while Lightning is a TCP/Web server framework. The Swift compiler and package manager, combined with third-party Swift packages, make it unnecessary to build that functionality into Lightning.