All Projects → skylab-inc → Lightning

skylab-inc / Lightning

Licence: mit
A Swift Multiplatform Single-threaded Non-blocking Web and Networking Framework

Programming Languages

swift
15916 projects
swift4
162 projects

Projects that are alternatives of or similar to Lightning

Ios Oss
Kickstarter for iOS. Bring new ideas to life, anywhere.
Stars: ✭ 7,840 (+2412.82%)
Mutual labels:  frp, functional-reactive-programming, reactivecocoa
Kickstarter Reactiveextensions
A collection of extensions to the ReactiveSwift framework.
Stars: ✭ 183 (-41.35%)
Mutual labels:  frp, functional-reactive-programming, reactivecocoa
Message Io
Event-driven message library for building network applications easy and fast.
Stars: ✭ 321 (+2.88%)
Mutual labels:  asynchronous, non-blocking, tcp
Hareactive
Purely functional reactive programming library
Stars: ✭ 293 (-6.09%)
Mutual labels:  frp, functional-reactive-programming
Yampa
Functional Reactive Programming domain-specific language embedded in Haskell, for programming efficient hybrid (mixed discrete-time and continuous-time) systems.
Stars: ✭ 294 (-5.77%)
Mutual labels:  frp, functional-reactive-programming
demonstration-gsd
GSD (Get your Stuff Done) | Basic Todo list for demonstrating CQRS/Command Sourcing in Haskell
Stars: ✭ 46 (-85.26%)
Mutual labels:  functional-reactive-programming, frp
mutable
State containers with dirty checking and more
Stars: ✭ 32 (-89.74%)
Mutual labels:  functional-reactive-programming, frp
Shift
Light-weight EventKit wrapper.
Stars: ✭ 31 (-90.06%)
Mutual labels:  asynchronous, swift-package-manager
RxIo
Asynchronous non-blocking File Reader and Writer library for Java
Stars: ✭ 20 (-93.59%)
Mutual labels:  asynchronous, non-blocking
Swift-3-Functional-Programming
Code repository for Swift 3 Functional Programming, published by Packt
Stars: ✭ 78 (-75%)
Mutual labels:  functional-reactive-programming, frp
reflex-dom-ace
Reflex wrapper for the ACE editor
Stars: ✭ 12 (-96.15%)
Mutual labels:  functional-reactive-programming, frp
Reflex Dom
Web applications without callbacks or side-effects. Reflex-DOM brings the power of functional reactive programming (FRP) to the web. Build HTML and other Document Object Model (DOM) data with a pure functional interface.
Stars: ✭ 301 (-3.53%)
Mutual labels:  frp, functional-reactive-programming
asyncio-socks-server
A SOCKS proxy server implemented with the powerful python cooperative concurrency framework asyncio.
Stars: ✭ 154 (-50.64%)
Mutual labels:  tcp, asynchronous
purescript-pop
😃 A functional reactive programming (FRP) demo created with PureScript events and behaviors.
Stars: ✭ 33 (-89.42%)
Mutual labels:  functional-reactive-programming, frp
workerman
An asynchronous event driven PHP socket framework. Supports HTTP, Websocket, SSL and other custom protocols. PHP>=5.4.
Stars: ✭ 10,005 (+3106.73%)
Mutual labels:  tcp, asynchronous
twjitm-core
采用Netty信息加载实现长连接实时通讯系统,客户端可以值任何场景,支持实时http通讯、webSocket通讯、tcp协议通讯、和udp协议通讯、广播协议等 通过http协议,rpc协议。 采用自定义网络数据包结构, 实现自定义网络栈。
Stars: ✭ 98 (-68.59%)
Mutual labels:  tcp, asynchronous
recurrent
A library for building functional-reactive (FRP) GUIs in Clojurescript
Stars: ✭ 49 (-84.29%)
Mutual labels:  functional-reactive-programming, frp
anytunnel
内网穿透,内网穿透代理服务器,商用内网穿透代理系统,内网穿透平台,内网穿透多用户会员系统。
Stars: ✭ 115 (-63.14%)
Mutual labels:  tcp, frp
Firefly
Firefly is an asynchronous web framework for rapid development of high-performance web application.
Stars: ✭ 277 (-11.22%)
Mutual labels:  asynchronous, tcp
RxUploader
Uploader for Android using RxJava
Stars: ✭ 72 (-76.92%)
Mutual labels:  functional-reactive-programming, frp

Edge
Serverside non-blocking IO in Swift
Ask questions in our Slack channel!

Lightning

(formerly Edge)

Swift Build Status codecov Slack Status

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

import PackageDescription

let package = Package(
    name: "MyProject",
    dependencies: [
        .Package(url: "https://github.com/skylab-inc/Lightning.git", majorVersion: 0, minor: 3)
    ]
)

Swift 4

// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription

let package = Package(
    name: "MyProject",
    dependencies: [
        .package(url: "https://github.com/skylab-inc/Lightning.git", from: "0.3.0"),
    ]
)

Usage

Routing

import Lightning
import Foundation

// Create an API router.
let api = Router()

// Add a GET "/users" endpoint.
api.get("/users") { request in
    return Response(status: .ok)
}

// NOTE: Equivalent to `api.post("/auth/login")`
let auth = api.subrouter("/auth")
auth.post("/login") { request in
    return Response(status: .ok)
}

// Middleware to log all requests
// NOTE: Middleware is a simple as a map function or closure!
let app = Router()
app.map { request in
    print(request)
    return request
}

// Mount the API router under "/v1.0".
app.add("/v1.0", api)

// NOTE: Warnings on all unhandled requests. No more hanging clients!
app.any { _ in
    return Response(status: .notFound)
}

// Start the application.
app.start(host: "0.0.0.0", port: 3000)

Raw HTTP

import Lightning
import Foundation

func handleRequest(request: Request) -> Response {
    print(String(bytes: request.body, encoding: .utf8)!)
    return try! Response(json: ["message": "Message received!"])
}

let server = HTTP.Server()
server.listen(host: "0.0.0.0", port: 3000).startWithNext { client in

    let requestStream = client.read()
    requestStream.map(handleRequest).onNext{ response in
        client.write(response).start()
    }

    requestStream.onFailed { clientError in
        print("Oh no, there was an error! \(clientError)")
    }

    requestStream.onCompleted {
        print("Goodbye \(client)!")
    }

    requestStream.start()
}

RunLoop.runAll()

TCP


import Lightning
import Foundation

let server = try! TCP.Server()
try! server.bind(host: "0.0.0.0", port: 50000)
    
server.listen().startWithNext { connection in
    let byteStream = connection.read()
    let strings = byteStream.map { String(bytes: $0, encoding: .utf8)! }
    
    strings.onNext { message in
        print("Client \(connection) says \"\(message)\"!")
    }
    
    strings.onFailed { error in
        print("Oh no, there was an error! \(error)")
    }
    
    strings.onCompleted {
        print("Goodbye \(connection)!")
    }
    
    strings.start()
}

RunLoop.runAll()

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].