Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/RxSwiftCommunity/RxStarscream
A lightweight extension to subscribe Starscream websocket events with RxSwift
https://github.com/RxSwiftCommunity/RxStarscream
Last synced: 2 months ago
JSON representation
A lightweight extension to subscribe Starscream websocket events with RxSwift
- Host: GitHub
- URL: https://github.com/RxSwiftCommunity/RxStarscream
- Owner: RxSwiftCommunity
- License: mit
- Created: 2016-05-18T15:11:50.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-12-11T18:36:21.000Z (about 1 year ago)
- Last Synced: 2024-05-09T08:04:41.524Z (9 months ago)
- Language: Swift
- Size: 15.9 MB
- Stars: 152
- Watchers: 7
- Forks: 52
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-rxswift - RxStarscream
README
RxStarscream
=========================================================================================================================
[![CircleCI](https://img.shields.io/circleci/project/github/RxSwiftCommunity/RxStarscream/master.svg)](https://circleci.com/gh/RxSwiftCommunity/RxStarscream/tree/master)
![pod](https://img.shields.io/cocoapods/v/RxStarscream.svg)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)A lightweight extension to [Starscream](https://github.com/daltoniam/Starscream) to track websocket events using RxSwift observables.
## Installation
### CocoaPods
RxStarscream is available through [CocoaPods](http://cocoapods.org/).
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.2 | \>= 0.10 | \>= 4.3 |
| < 4.2 | \>= 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.
```swift
import RxStarscream
```Once imported, you can open a connection to your WebSocket server.
```swift
socket = WebSocket(url: URL(string: "ws://localhost:8080/")!)
socket.connect()
```
Now you can subscribe e.g to all of the websocket events:```swift
socket.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")
}
}).disposed(by: disposeBag)
```Or just to a connect event:
```swift
socket.rx.connected.subscribe(onNext: { (isConnected: Bool) in
print("Is connected : \(isConnected)")
}).disposed(by: disposeBag)
```Or just to a message event:
```swift
socket.rx.text.subscribe(onNext: { (message: String) in
print("Message : \(message)")
}).disposed(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.