A testable RxSwift wrapper around MultipeerConnectivity

RxMultipeer is a RxSwift wrapper for 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.

Installation

Carthage

Add this to your Cartfile

3.0
“>

github "RxSwiftCommunity/RxMultipeer" ~> 3.0

Example code

For a working example check out the RxMultipeer Example folder.

Advertise and accept nearby peers

<div class="highlight highlight-source-swift position-relative" data-snippet-clipboard-copy-content="import RxSwift
import RxCocoa
import RxMultipeer

let disposeBag: DisposeBag
let acceptButton: UIButton
let client: CurrentClient

client.startAdvertising()
let connectionRequests = client.incomingConnections().shareReplay(1)

acceptButton.rx_tap
.withLatestFrom(connectionRequests)
.subscribe(onNext: { (peer, context, respond) in respond(true) })
.addDisposableTo(disposeBag)

client.incomingCertificateVerifications()
.subscribe(onNext: { (peer, certificateChain, respond) in
// Validate the certificateChain
respond(true)
})
.addDisposableTo(disposeBag)
“>

import RxSwift
import RxCocoa
import RxMultipeer

let disposeBag: DisposeBag
let acceptButton: UIButton
let client: CurrentClient

client.startAdvertising()
let connectionRequests = client.incomingConnections().shareReplay(1)

acceptButton.rx_tap
  .withLatestFrom(connectionRequests)
  .subscribe(onNext: { (peer, context, respond) in respond(true) })
  .addDisposableTo(disposeBag)

client.incomingCertificateVerifications()
    .subscribe(onNext: { (peer, certificateChain, respond) in
      // Validate the certificateChain
      respond(true)
    })
    .addDisposableTo(disposeBag)