All Projects → ScaleDrone → Scaledrone-Swift

ScaleDrone / Scaledrone-Swift

Licence: MIT license
Swift Client for Scaledrone Realtime Messaging Service (WIP)

Programming Languages

swift
15916 projects
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Scaledrone-Swift

React Stomp
React websocket component for STOMP protocol over SockJs
Stars: ✭ 110 (+358.33%)
Mutual labels:  websocket-client
Embedded Jetty Websocket Examples
Embedded Jetty WebSocket Examples
Stars: ✭ 159 (+562.5%)
Mutual labels:  websocket-client
Websocket Client
WebSocket client for Python
Stars: ✭ 2,810 (+11608.33%)
Mutual labels:  websocket-client
Xsound
Web Audio API Library for Synthesizer, Effects, Visualization, Multi-Track Recording, Audio Streaming, Visual Audio Sprite ...
Stars: ✭ 123 (+412.5%)
Mutual labels:  websocket-client
Osc Js
OSC library for Node.js, Electron, Chrome Apps, Webpages or any other JS application. It comes with a customizable Plugin API for WebSocket, UDP or bridge networking
Stars: ✭ 135 (+462.5%)
Mutual labels:  websocket-client
Flutter socket io
Socket IO supprt for flutter. Looking for contributors Swift and Java.
Stars: ✭ 170 (+608.33%)
Mutual labels:  websocket-client
Python Bittrex Websocket
Python websocket for Bittrex (non async).
Stars: ✭ 104 (+333.33%)
Mutual labels:  websocket-client
meteorman
A DDP client with GUI (The Postman for Meteor)
Stars: ✭ 51 (+112.5%)
Mutual labels:  websocket-client
Poloniex Api Node
Poloniex API client for REST and WebSocket API
Stars: ✭ 138 (+475%)
Mutual labels:  websocket-client
Ixwebsocket
websocket and http client and server library, coming with ws, a command line swiss army knife utility
Stars: ✭ 204 (+750%)
Mutual labels:  websocket-client
Birdsong
🐦🎼 Swift WebSockets client for Phoenix Channels.
Stars: ✭ 124 (+416.67%)
Mutual labels:  websocket-client
Bolt Js
A framework to build Slack apps using JavaScript
Stars: ✭ 1,971 (+8112.5%)
Mutual labels:  websocket-client
Claws
Awesome WebSocket CLient - an interactive command line client for testing websocket servers
Stars: ✭ 187 (+679.17%)
Mutual labels:  websocket-client
Php Wss
Web-socket server/client with multi-process and parse templates support on server and send/receive options on client
Stars: ✭ 117 (+387.5%)
Mutual labels:  websocket-client
Websocat
Command-line client for WebSockets, like netcat (or curl) for ws:// with advanced socat-like functions
Stars: ✭ 3,477 (+14387.5%)
Mutual labels:  websocket-client
Coinbasepro Python
The unofficial Python client for the Coinbase Pro API
Stars: ✭ 1,386 (+5675%)
Mutual labels:  websocket-client
Libhv
🔥 比libevent、libuv更易用的国产网络库。A c/c++ network library for developing TCP/UDP/SSL/HTTP/WebSocket client/server.
Stars: ✭ 3,355 (+13879.17%)
Mutual labels:  websocket-client
action-cable-react-jwt
Rails action-cable integration with JWT authentication
Stars: ✭ 38 (+58.33%)
Mutual labels:  websocket-client
websocket-scala-client
WebSocket client based on Netty
Stars: ✭ 37 (+54.17%)
Mutual labels:  websocket-client
Bolt Python
A framework to build Slack apps using Python
Stars: ✭ 190 (+691.67%)
Mutual labels:  websocket-client

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.5.2'

Then run:

pod install

Carthage

Check out the Carthage Quick Start instructions.

To use Scaledrone with Carthage, add the following to your Cartfile:

github "ScaleDrone/Scaledrone-Swift"

Then run:

carthage update

After that, follow the instructions on Carthage's docs.

Swift Package Manager

Use Xcode to add this repo as a package. Search for https://github.com/ScaleDrone/Scaledrone-Swift.

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: Error?) {
    print("Connected to Scaledrone")
}

scaledroneDidReceiveError

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

scaledroneDidDisconnect

func scaledroneDidDisconnect(scaledrone: Scaledrone, error: Error?) {
    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: Error?) {
    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: Error?) {
    print("Scaledrone connected to room", room.name, error ?? "")
}

scaledroneRoomDidReceiveMessage

The member argument exists when the message was sent to an observable room using the socket API (not the REST API).

func scaledroneRoomDidReceiveMessage(room: ScaledroneRoom, message: ScaledroneMessage) {
    if message.member != nil {
        // This message was sent to an observable room
        // This message was sent through the socket API, not the REST API
        print("Received message from member:", message.memberID as Any)
    }
    
    let data = message.data
    
    if let messageData = data as? [String: Any] {
        print("Received a dictionary:", messageData)
    }
    if let messageData = data as? [Any] {
        print("Received an array:", messageData)
    }
    if let messageData = data as? String {
        print("Received a string:", messageData)
    }
}

Observable rooms

Observable rooms act like regular rooms but provide additional functionality for keeping track of connected members and linking messages to members.

Adding data to the member object

Observable rooms allow adding custom data to a connected user. The data can be added in two ways:

  1. Passing the data object to a new instance of Scaledrone in your Swift code.
let scaledrone = Scaledrone(channelID: "<channel_id>", data: ["name": "Swift", "color": "#ff0000"])

This data can later be accessed like so:

func scaledroneObservableRoomMemberDidJoin(room: ScaledroneRoom, member: ScaledroneMember) {
    print("member joined with clientData", member.clientData)
}
  1. Adding the data to the JSON Web Token as the data clause during authentication. This method is safer as the user has no way of changing the data on the client side.
{
  "client": "client_id_sent_from_javascript_client",
  "channel": "channel_id",
  "data": {
    "name": "Swift",
    "color": "#ff0000"
  },
  "permissions": {
    "^main-room$": {
      "publish": false,
      "subscribe": false
    }
  },
  "exp": 1408639878000
}

This data can later be accessed like so:

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

Receiving the observable events

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

Message History

When creating a Scaledrone room you can supply the number of messages to recieve from that room's history. The messages will arrive, in reverse chronological order and one by one, in scaledroneRoomDidReceiveMessage, just like real-time messages.

In order to recieve message history messages, this feature needs to be enabled in the Scaledrone dashboard. You can learn more about Message History and its limitations in Scaledrone docs.

let room = scaledrone.subscribe(roomName: "chat-room", messageHistory: 50)

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: Error?) {
        print("Connected to Scaledrone channel", scaledrone.clientID)
        let room = scaledrone.subscribe(roomName: "notifications")
        room.delegate = self
    }

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

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

    func scaledroneRoomDidConnect(room: ScaledroneRoom, error: Error?) {
        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.

Migration notes

0.5.0

Scaledrone 0.5.0 removes the use of NSError in favor of Error in the delegate methods, and adds support for Swift 5.

0.5.2:

scaledroneRoomDidReceiveMessage(room:message:member) was renamed to scaledroneRoomDidReceiveMessage(room:message:) and message is now of type ScaledroneMessage which includes the member and message IDs, the message's time as well as the data that was sent.

Todo:

  • Automatic reconnection
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].