RxSwiftCommunity/RxStarscream

Name: RxStarscream

Owner: RxSwift Community

Description: A lightweight extension to subscribe Starscream websocket events with RxSwift

Created: 2016-05-18 15:11:50.0

Updated: 2018-05-17 03:04:45.0

Pushed: 2018-03-24 22:31:47.0

Homepage: null

Size: 16653

Language: Swift

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

RxStarscream

CircleCI pod Carthage compatible

A lightweight extension to Starscream to track websocket events using RxSwift observables.

Installation
CocoaPods

RxStarscream is available through CocoaPods. Add the following line to your Podfile:

pod 'RxStarscream'

Then run:

pod install
RxStarscream version vs Swift version.

Below is a table that shows which version of RxStarscream you should use for your Swift version.

| Swift | RxStarscream | RxSwift | | —– | ————- |—————| | 4.X | >= 0.8 | >= 4.0 | | 3.X | 0.7 | 3.0.0 - 3.6.1 |

Carthage

Add this to your Cartfile

github "RxSwiftCommunity/RxStarscream"

Then run:

carthage update
Usage examples

After installing via CococPods or Carthage, you should import the framework.

rt RxStarscream

Once imported, you can open a connection to your WebSocket server.

et = WebSocket(url: URL(string: "ws://localhost:8080/")!)
et.connect()

Now you can subscribe e.g to all of the websocket events:

et.rx.response.subscribe(onNext: { (response: WebSocketEvent) in
switch response {
case .connected:
    print("Connected")
case .disconnected(let error):
    print("Disconnected with optional error : \(error)")
case .message(let msg):
    print("Message : \(msg)")
case .data(_):
    print("Data")
case .pong:
    print("Pong")
}
isposed(by: disposeBag)

Or just to a connect event:

et.rx.connected.subscribe(onNext: { (isConnected: Bool) in        
print("Is connected : \(isConnected)")
isposed(by: disposeBag)

Or just to a message event:

et.rx.text.subscribe(onNext: { (message: String) in        
print("Message : \(message)")
isposed(by: disposeBag)
Sample Project

There's a sample project (you need to run carthage update for it to compile).

The sample project uses echo server - https://www.websocket.org/echo.html

Have fun!

Thanks

Everyone in the RxSwift Slack channel.

Contributing

Bug reports and pull requests are welcome.

License

RxStarscream is available under the MIT license. See the LICENSE file for more info.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.