GithubHelp home page GithubHelp logo

nsleche / swift-actioncableclient Goto Github PK

View Code? Open in Web Editor NEW

This project forked from danielrhodes/swift-actioncableclient

0.0 2.0 0.0 419 KB

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.

License: MIT License

Ruby 1.90% Swift 98.10%

swift-actioncableclient's Introduction

ActionCableClient

Version License Platform

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

ActionCableClient is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "ActionCableClient"

Swift 3

If you are looking for a Swift 3 Compatibile version, use the Podfile example below.

pod 'Starscream', :git => 'https://github.com/daltoniam/Starscream.git', :branch => 'swift3'
pod 'ActionCableClient', :git => 'https://github.com/danielrhodes/Swift-ActionCableClient', :branch => 'swift3'

Note: the API may have changed in some places.

Usage

Get Started & Connect

import ActionCableClient

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

// Connect!
client.connect()

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

client.onDisconnected = {(error: ErrorType?) 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

Channel Callbacks

// Receive a message from the server. Typically a Dictionary.
roomChannel.onReceive = { (JSON : AnyObject?, 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!"])

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

SwiftyJSON: Used internally to ensure we are getting good responses from the server.

Starscream: The underlying WebSocket library.

Author

Daniel Rhodes, [email protected]

License

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

swift-actioncableclient's People

Contributors

danielrhodes avatar vaughankg avatar

Watchers

James Cloos avatar Antonio Alves avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.