Home

Awesome

Starscream-RxSwift

A simple and lightweight extension to Starscream to track suscribe to socket events with RxSwift

Features

The socket currently disconnects when disposing.

Usage examples

let events = socket?.rx_response()
  .subscribe { event in
    switch event {
      case .Error(let error): print(error)
      case .Next(let socketEvent):
        switch socketEvent {
          case .Connected: print("Chat connected")
          case .Message(let text): chat.showMessage(text)
          case .Data(let data): chat.showImageFromData(data)
        }
    }
  }
let messages = self.socket.rx_text()
  //transform the string we got into some sort of chat message we can use
  .flatMapLatest { jsonText -> Observable<ChatMessage> in
    if  let message = JSONParser.getChatMessageFromString(jsonText) {
      return Observable.just(message)
    } else {
      return Observable.empty()
    }
  }
  //display the parsed message
  .subscribeNext { message in
    chat.showMessage(message)
  }
  .addDisposableTo(disposeBag)