Swift / iOS Quick Start

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

Connecting

To connect to a channel you need to first create it in the admin panel of Scaledrone's website. One instance of Scaledrone establishes a single connection.

First import the framework.

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.

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

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

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

Listening to messages

Rooms are used to isolate different message types. To listen to messages, you need to subscribe to a specific room. Users can freely connect to multiple rooms (this does not create extra connections).

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

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

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

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)
    }
}

Sending messages

Messages are published to rooms. All users subscribed to the room will receive the message (including the user that sent the message, if they are subscribed). You don't have to be subscribed to a room when publishing to it.

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

Basic example

Make sure you replace the CHANNEL_ID string with your Scaledrone channel's ID from the dashboard.

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)
    }
}

Who's online functionality

To keep track of users that are connected to a room see the observable rooms documentation.

Authentication

Socket client authentication is an optional step and can be used to give precise access to specific actions and rooms. Read more about this from the authentication documentation as well as the full Java API docs.