NKMultipeer alternatives and similar libraries
Based on the "Network" category.
Alternatively, view NKMultipeer alternatives based on common mentions on social networks and blogs.
-
Perfect
Server-side Swift. The Perfect core toolset and framework for Swift Developers. (For mobile back-end development, website and API development, and moreβ¦) -
Reachability.swift
Replacement for Apple's Reachability re-written in Swift with closures -
swifter
Tiny http server engine written in Swift programming language. -
SwiftSoup
SwiftSoup: Pure Swift HTML Parser, with best of DOM, CSS, and jquery (Supports Linux, iOS, Mac, tvOS, watchOS) -
Netfox
A lightweight, one line setup, iOS / OSX network debugging library! π¦ -
SwiftHTTP
Thin wrapper around NSURLSession in swift. Simplifies HTTP requests. -
CocoaMQTT
MQTT 5.0 client library for iOS and macOS written in Swift -
APIKit
Type-safe networking abstraction layer that associates request type with response type. -
Swifton
A Ruby on Rails inspired Web Framework for Swift that runs on Linux and OS X -
Zewo
Lightweight library for web server applications in Swift on macOS and Linux powered by coroutines. -
ResponseDetective
Sherlock Holmes of the networking layer. :male_detective: -
SwiftWebSocket
A high performance WebSocket client library for swift. -
BlueSocket
Socket framework for Swift using the Swift Package Manager. Works on iOS, macOS, and Linux. -
Connectivity
π Makes Internet connectivity detection more robust by detecting Wi-Fi networks without Internet access. -
WKZombie
WKZombie is a Swift framework for iOS/OSX to navigate within websites and collect data without the need of User Interface or API, also known as Headless browser. It can be used to run automated tests / snapshots and manipulate websites using Javascript. -
Taylor
A lightweight library for writing HTTP web servers with Swift -
Pitaya
π A Swift HTTP / HTTPS networking library just incidentally execute on machines -
Blackfish
A minimal, fast and unopinionated web framework for Swift -
PeerKit
An open-source Swift framework for building event-driven, zero-config Multipeer Connectivity apps -
Express
Swift Express is a simple, yet unopinionated web application server written in Swift -
Heimdallr.swift
Easy to use OAuth 2 library for iOS, written in Swift. -
Embassy
Super lightweight async HTTP server library in pure Swift runs in iOS / MacOS / Linux -
Reach
A simple class to check for internet connection availability in Swift. -
Socks
π Non-blocking TCP socket layer, with event-driven server and client. -
Digger
Digger is a lightweight download framework that requires only one line of code to complete the file download task -
SOAPEngine
This generic SOAP client allows you to access web services using a your iOS app, Mac OS X app and AppleTV app. -
Transporter
A tiny library makes uploading and downloading easier -
SwiftyOAuth
A simple OAuth library for iOS with a built-in set of providers -
XcodeServerSDK
Access Xcode Server API with native Swift objects. -
BigBrother
Automatically sets the network activity indicator for any performed request.
Appwrite - The open-source backend cloud platform
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of NKMultipeer or a related project?
README
This project has been moved to RxSwiftCommunity/RxMultipeer. Please update your projects to use the new repository, as this one will no longer be maintained.
A testable, Rx* wrapper around MultipeerConnectivity
Using the adapter pattern, we can test multipeer code with heavy mocking. In effect, we are trying to isolate all the
untestable bits of MultipeerConnectivity
into one library.
This library also gives you the flexibility to swap out the underlying mechanics of p2p with some other protocol such as websockets. At the moment it only comes with support for Apple's MultipeerConnectivity, however you can easily write your own adapters for different protocols.
Please note that NKMultipeer makes heavy use of RxSwift which you should read up on if unfamiliar with Rx* libraries. The mantra for this library: everything is a stream.
Installation
Carthage
Add this to your Cartfile
github "nathankot/NKMultipeer" ~> 1.0.0
CocoaPods
use_frameworks!
pod "NKMultipeer"
Example code
For a working example check out the NKMultipeer Example
folder.
Advertise and accept nearby peers
import RxSwift
import RxCocoa
import NKMultipeer
let acceptButton: UIButton
let client: CurrentClient<MCPeerID>
client.startAdvertising()
let connectionRequests = client.incomingConnections().shareReplay(1)
acceptButton.rx_tap
.withLatestFrom(connectionRequests)
.subscribeNext { (peer, context, respond) in respond(true) }
.addDisposableTo(disposeBag)
Browse for and connect to peers
import RxSwift
import NKMultipeer
let client: CurrentClient<MCPeerID>
client.startBrowsing()
let nearbyPeers = client.nearbyPeers().shareReplay(1)
// Attempt to connect to all peers
nearbyPeers
.map { (peers: [Client<MCPeerID>]) in
peers.map { client.connect($0, context: ["Hello": "there"], timeout: 12) }.zip()
}
.subscribe()
.addDisposableTo(disposeBag)
Sending and receiving strings
Sending them:
import RxSwift
import RxCocoa
import NKMultipeer
let client: CurrentClient<MCPeerID>
let peer: Observable<Client<MCPeerID>>
let sendButton: UIButton
sendButton.rx_tap
.withLatestFrom(peer)
.map { client.send(peer, "Hello!") }
.switchLatest()
.subscribeNext { _ in print("Message sent") }
.addDisposableTo(disposeBag)
And receiving them:
import RxSwift
import NKMultipeer
let client: CurrentClient<MCPeerID>
client.receive()
.subscribeNext { (peer: Client<MCPeerID>, message: String) in
print("got message \(message), from peer \(peer)")
}
.addDisposableTo(disposeBag)
Establishing a data stream
RxSwift makes sending streaming data to a persistent connection with another peer very intuitive.
The sender:
import RxSwift
import NKMultipeer
let client: CurrentClient<MCPeerID>
let peer: Observable<Client<MCPeerID>>
let queuedMessages: Observable<[UInt8]>
let pipe = peer.map { client.send(peer, streamName: "data.stream") }
pipe.withLatestFrom(queuedMessages) { $0 }
.subscribeNext { (sender, message) in sender(message) }
.addDisposableTo(disposeBag)
The receiver:
import RxSwift
import NKMultipeer
let client: CurrentClient<MCPeerID>
let peer: Observable<Client<MCPeerID>>
let incomingData = client.receive(peer, streamName: "data.stream").shareReplay(1)
incomingData.subscribeNext { (data) in print(data) }
.addDisposableTo(disposeBag)
Usage
Imports
import RxSwift
import NKMultipeer
Make a new build configuration for testing
Your project comes with Debug
and Release
build configurations by default, we need to make a new one called
Testing
. Please check here for step-by-step instructions.
Setting up the client
// See the link above,
// You'll need to define a new build configuration and give it the `TESTING` flag
let name = UIDevice.currentDevice().name
#if TESTING
typealias I = MockIden
let client = CurrentClient(session: MockSession(name: name))
#else
typealias I = MCPeerID
let client = CurrentClient(session: MultipeerConnectivitySession(
displayName: name,
serviceType: "multipeerex",
encryptionPreference: .None))
#endif
Supported transfer resource types
String
func send(other: Client, _ string: String, _ mode: MCSessionSendDataMode = .Reliable) -> Observable<()>
func receive() -> Observable<(Client, String)>
NSData
func send(other: Client, _ data: NSData, _ mode: MCSessionSendDataMode = .Reliable) -> Observable<()>
func receive() -> Observable<(Client, NSData)>
JSON
func send(other: Client, _ json: [String: AnyObject], _ mode: MCSessionSendDataMode = .Reliable) -> Observable<()>
func receive() -> Observable<(Client, [String: AnyObject])>
NSURL
func send(other: Client, name: String, url: NSURL, _ mode: MCSessionSendDataMode = .Reliable) -> Observable<NSProgress>
func receive() -> Observable<(Client, String, ResourceState)>
NSStream
func send(other: Client, streamName: String, runLoop: NSRunLoop = NSRunLoop.mainRunLoop()) -> Observable<([UInt8]) -> Void>
func receive(other: Client, streamName: String, runLoop: NSRunLoop = NSRunLoop.mainRunLoop(), maxLength: Int = 512) -> Observable<[UInt8]>
Testing
When testing, use preprocesser macros to ensure that your code uses a MockSession
instance instead of
MultipeerConnectivitySession
one. In order to achieve this you need to use preprocessor flags and swap out anywhere
that references Client<T>
(because T
will be different depending on whether you are testing or not.) First you will
need to set up a new build configuration, and then you can use preprocessor macros like so:
let name = UIDevice.currentDevice().name
#if TESTING
typealias I = MockIden
let client = CurrentClient(session: MockSession(name: name))
#else
typealias I = MCPeerID
let client = CurrentClient(session: MultipeerConnectivitySession(
displayName: name,
serviceType: "multipeerex",
encryptionPreference: .None))
#endif
Don't worry, you should only really need preprocessor macros in one centralized place, the type of your client can be inferred by the compiler thereafter.
Mocking other nearby peers in the test environment then becomes as simple as creating other CurrentClient(session:
MockSession(name: "other"))
. For example, if your app is running in a testing environment the following code will mock
a nearby client:
let otherclient = CurrentClient(session: MockSession(name: "mockedother"))
// Accept all connections
otherclient.startAdvertising()
otherclient.incomingConnections()
.subscribeNext { (client, context, respond) in respond(true) }
.addDisposableTo(disposeBag)
// Respond to all messages with 'Roger'
otherclient.receive()
.map { (client: Client<MockIden>, string: String) in return otherclient.send(client, "Roger") }
.concat()
.subscribeNext { _ in print("Response sent") }
.addDisposableTo(disposeBag)
Contributing
- Indent with 2 spaces
- Strip trailing whitespace
- Write tests
- Pull-request from feature branches.