Skip to content
ActionCable is a new WebSocket server being released with Rails 5 which makes it easy to add real-time features to your app. This Swift client makes it dead-simple to connect with that server, abstracting away everything except what you need to get going.
Swift Ruby Other
Branch: master
Clone or download
danielrhodes Merge pull request #27 from ahbou/master
Assign unique identifier to channel
Latest commit b975c3e Mar 4, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
ActionCableClient.xcodeproj restored headers Dec 27, 2017
ActionCableClient.xcworkspace
Carthage/Checkouts updated cart file Dec 27, 2017
Example Swift 4 conversion Sep 14, 2017
Source
Tests Carthage support, updated Starscream to 2.0.1 Oct 28, 2016
.gitignore Carthage support, updated Starscream to 2.0.1 Oct 28, 2016
.gitmodules
.swift-version
.travis.yml Initial commit Feb 26, 2016
ActionCableClient.podspec Starscream in podspec Dec 27, 2017
Cartfile Carthage support, updated Starscream to 2.0.1 Oct 28, 2016
Cartfile.resolved updated cart file Dec 27, 2017
LICENSE
README.md Update README.md Nov 12, 2016

README.md

ActionCableClient

Version License Platform Carthage compatible

ActionCable is a new WebSockets server being released with Rails 5 which makes it easy to add real-time features to your app. This Swift client makes it dead-simple to connect with that server, abstracting away everything except what you need to get going.

Installation

To install, simply:

Cocoapods

Add the following line to your Podfile and run pod install

pod "ActionCableClient"

Carthage

Add the following to your Cartfile and run carthage update as normal.

github "danielrhodes/Swift-ActionCableClient"

Usage

Get Started & Connect

import ActionCableClient

self.client = ActionCableClient(url: URL(string: "ws://domain.tld/cable")!)

// Connect!
client.connect()

client.onConnected = {
    print("Connected!")
}

client.onDisconnected = {(error: Error?) in
    print("Disconnected!")
}

Subscribe to a Channel

// Create the Room Channel
let roomChannel = client.create("RoomChannel") //The channel name must match the class name on the server

// More advanced usage
let room_identifier = ["room_id" : identifier]
let roomChannel = client.create("RoomChannel", identifier: room_identifier, autoSubscribe: true, bufferActions: true)

Channel Callbacks

// Receive a message from the server. Typically a Dictionary.
roomChannel.onReceive = { (JSON : Any?, error : ErrorType?) in
    print("Received", JSON, error)
}

// A channel has successfully been subscribed to.
roomChannel.onSubscribed = {
    print("Yay!")
}

// A channel was unsubscribed, either manually or from a client disconnect.
roomChannel.onUnsubscribed = {
    print("Unsubscribed")
}

// The attempt at subscribing to a channel was rejected by the server.
roomChannel.onRejected = {
    print("Rejected")
}

Perform an Action on a Channel

// Send an action
roomChannel["speak"](["message": "Hello, World!"])

// Alternate less magical way:
roomChannel.action("speak", ["message": "Hello, World!"])

// Note: The `speak` action must be defined already on the server

Authorization & Headers

// ActionCable can be picky about origins, so if you
// need it can be set here.
client.origin = "https://domain.tld/"

// If you need any sort of authentication, you 
// will not have cookies like you do in the browser,
// so set any headers here.
//
// These are available in the `Connection`
// on the server side.

client.headers = [
    "Authorization": "sometoken"
]

Misc

client.onPing = {
    
}

For more documentation, see the wiki

Requirements

Starscream: The underlying WebSocket library.

Author

Daniel Rhodes, rhodes.daniel@gmail.com

License

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

You can’t perform that action at this time.