Latest 0.2.0
Homepage https://github.com/scaledrone/scaledrone-swift
License MIT
Platforms ios 8.0, osx 10.10, tvos 9.0, requires ARC
Dependencies Starscream
Authors

Scaledrone Swift

Use the Scaledrone Swift client to connect to the Scaledrone realtime messaging service

This project is still a work in progress, pull requests and issues are very welcome.

Installation

CocoaPods

Check out Get Started tab on cocoapods.org.

To use Scaledrone in your project add the following ‘Podfile’ to your project

pod 'Scaledrone', '~> 0.2.0'

Then run:

pod install

Usage

First thing is to import the framework. See the Installation instructions on how to add the framework to your project.

import Scaledrone

Once imported, you can connect to Scaledrone.

scaledrone = Scaledrone(channelID: "your-channel-id")
scaledrone.delegate = self
scaledrone.connect()

After you are connected, there are some delegate methods that we need to implement.

scaledroneDidConnect

func scaledroneDidConnect(scaledrone: Scaledrone, error: NSError?) {
    print("Connected to Scaledrone")
}

scaledroneDidReceiveError

func scaledroneDidReceiveError(scaledrone: Scaledrone, error: NSError?) {
    print("Scaledrone error", error ?? "")
}

scaledroneDidDisconnect

func scaledroneDidDisconnect(scaledrone: Scaledrone, error: NSError?) {
    print("Scaledrone disconnected", error ?? "")
}

Authentication

Implement the ScaledroneAuthenticateDelegate protocol and set an additional delegate

scaledrone.authenticateDelegate = self

Then use the authenticate method to authenticate using a JWT

scaledrone.authenticate(jwt: "jwt_string")

scaledroneDidAuthenticate

func scaledroneDidAuthenticate(scaledrone: Scaledrone, error: NSError?) {
    print("Scaledrone authenticated", error ?? "")
}

Sending messages

scaledrone.publish(message: "Hello from Swift", room: "myroom")
// Or
room.publish(message: ["foo": "bar", "1": 2])

Subscribing to messages

Subscribe to a room and implement the ScaledroneRoomDelegate protocol, then set additional delegation

let room = scaledrone.subscribe(roomName: "myroom")
room.delegate = self

scaledroneRoomDidConnect

func scaledroneRoomDidConnect(room: ScaledroneRoom, error: NSError?) {
    print("Scaledrone connected to room", room.name, error ?? "")
}

scaledroneRoomDidReceiveMessage

func scaledroneRoomDidReceiveMessage(room: ScaledroneRoom, message: Any) {
    if let message = message as? [String : Any] {
        print("Received a dictionary:", message)
    }
    if let message = message as? [Any] {
        print("Received an array:", message)
    }
    if let message = message as? String {
        print("Received a string:", message)
    }
}

Observable rooms

Implement the ScaledroneObservableRoomDelegate protocol, then set additional delegation.

Observable room names need to be prefixed with observable-

let room = scaledrone.subscribe(roomName: "observable-room")
room.delegate = self
room.observableDelegate = self

scaledroneObservableRoomDidConnect

func scaledroneObservableRoomDidConnect(room: ScaledroneRoom, members: [ScaledroneMember]) {
    // The list will contain yourself
    print(members.map { (m: ScaledroneMember) -> String in
        return m.id
    })
}

scaledroneObservableRoomMemberDidJoin

func scaledroneObservableRoomMemberDidJoin(room: ScaledroneRoom, member: ScaledroneMember) {
    print("member joined", member, member.id)
}

scaledroneObservableRoomMemberDidLeave

func scaledroneObservableRoomMemberDidLeave(room: ScaledroneRoom, member: ScaledroneMember) {
    print("member left", member, member.id)
}

Basic Example

import UIKit

class ViewController: UIViewController, ScaledroneDelegate, ScaledroneRoomDelegate {

    let scaledrone = Scaledrone(channelID: "your-channel-id")

    override func viewDidLoad() {
        super.viewDidLoad()
        scaledrone.delegate = self
        scaledrone.connect()
    }

    func scaledroneDidConnect(scaledrone: Scaledrone, error: NSError?) {
        print("Connected to Scaledrone channel", scaledrone.clientID)
        let room = scaledrone.subscribe(roomName: "notifications")
        room.delegate = self
    }

    func scaledroneDidReceiveError(scaledrone: Scaledrone, error: NSError?) {
        print("Scaledrone error")
    }

    func scaledroneDidDisconnect(scaledrone: Scaledrone, error: NSError?) {
        print("Scaledrone disconnected")
    }

    func scaledroneRoomDidConnect(room: ScaledroneRoom, error: NSError?) {
        print("Scaledrone connected to room", room.name)
    }

    func scaledroneRoomDidReceiveMessage(room: ScaledroneRoom, message: String) {
        print("Room received message:", message)
    }
}

For a longer example see the ViewController.swift file.

Todo:

  • Automatic reconnection

Latest podspec

{
    "name": "Scaledrone",
    "version": "0.2.0",
    "summary": "Scaledrone Swift Client",
    "homepage": "https://github.com/scaledrone/scaledrone-swift",
    "license": "MIT",
    "authors": {
        "Scaledrone": "[email protected]"
    },
    "source": {
        "git": "https://github.com/ScaleDrone/Scaledrone-Swift.git"
    },
    "requires_arc": true,
    "source_files": "scaledrone-swift/Scaledrone.swift",
    "platforms": {
        "ios": "8.0",
        "osx": "10.10",
        "tvos": "9.0"
    },
    "dependencies": {
        "Starscream": [
            "~> 2.1.0"
        ]
    },
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This