Super simple implementation of Rails ActionCable client written in Swift. Working on top of swift websocket library: https://github.com/tidwall/SwiftWebSocket
// Initiate client with NSMutableURLRequest
let request = NSMutableURLRequest(URL: NSURL(string: "ws://localhost:3000/cable")!)
let client = ActionCableClient(mutableRequest: request)
// Create new channel
let exampleChannel = ActionChannel.init(name: "ExampleChannel")
// callback on message from server
exampleChannel.onMessage = { json in
print(json)
// send unsubscribe event to the server
client.unsubscribeFrom(exampleChannel)
}
// callback on succesfull subsbscription
exampleChannel.onSubscribed = {
print("succesfully subscribed!")
}
// send subscribe to request to server and start listening
client.subscribeTo(exampleChannel)
exampleChannel.perform("say_hello")
Rails server example is here: https://github.com/tenshilg/ActionCableExample
SwiftyActionCable is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "SwiftyActionCable"
- SwiftyJSON https://github.com/SwiftyJSON/SwiftyJSON
- WebSockets https://github.com/tidwall/SwiftWebSocket
Roman Kovtunenko, roman.kovtunenko@gmail.com
SwiftyActionCable is available under the MIT license. See the LICENSE file for more info.