Popularity
3.0
Stable
Activity
0.0
Stable
141
6
12

Code Quality Rank: L4
Programming language: Swift
License: MIT License
Tags: Network     Socket    
Latest version: v2.0.1

SocketIO-Kit alternatives and similar libraries

Based on the "Socket" category.
Alternatively, view SocketIO-Kit alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of SocketIO-Kit or a related project?

Add another 'Socket' Library

README

SocketIO-Kit Logo SocketIO-Kit

⚠️ This project is no longer maintained. Please use the official framework Socket.IO-Client-Swift.

SocketIO-Kit is a Socket.io iOS client with type safe, clean syntax and speed in mind. WebSocket is the only transport that is implemented and it uses SwiftWebSocket.

Carthage compatible Version Build Status GitHub license

Installation

Carthage

github "ricardopereira/SocketIO-Kit"

Then run carthage update.

Follow the current instructions in Carthage's README for up to date installation instructions.

CocoaPods

To install it, simply add the following line to your Podfile:

pod "SocketIOKit"

You will also need to make sure you're opting into using frameworks:

use_frameworks!

Then run pod install with CocoaPods 0.36 or newer.

Manually

Download the project and copy the SocketIO-Kit folder into your project and use the Source files. You need Runes v2.0.0 (Swift 1.2) framework to run SocketIO-Kit because it uses infix operators for monadic functions and you need SwiftWebSocket v0.1.18 (Swift 1.2).

There is no need for import SocketIOKit when manually installing.

Requirements

  • iOS 8.0+ / Mac OS X 10.10+
  • Xcode 6.3 (Swift 1.2)

⚠️ Swift 2.0 version, please use branch Swift2.

Usage

import SocketIOKit

// Type safety events
enum AppEvents: String, Printable {
    case ChatMessage = "chat message"
    case GetImage = "getimage"
    case Image = "image"

    var description: String {
        return self.rawValue
    }
}

// Mutable
var socket: SocketIO<AppEvents>!
socket = SocketIO(url: "http://localhost:8000/")

// or 
// Immutable
let socket = SocketIO<AppEvents>(url: "http://localhost:8000/")

socket.on(.ConnectError) {
    switch $0 {
    case .Failure(let error):
        println(error)
    default:
        break
    }
}.on(.TransportError) {
    switch $0 {
    case .Failure(let error):
        println("WebSocket error: \(error)")
    default:
        break
    }
}.on(.Connected) { result in
    println("Connected")
    socket.emit(.ChatMessage, withMessage: "I'm iOS")
}.on(.Disconnected) { result in
    switch result {
    case .Message(let reason):
        println("Disconnected: \(reason)")
    default:
        break
    }
}

socket.on(.ChatMessage) {
    switch $0 {
    case .Message(let message):
        println("Remote: \(message)")
    default:
        println("Not supported")
    }
}

socket.connect()

Options

socket = SocketIO(url: "http://localhost:8001/", withOptions: SocketIOOptions().namespace("/gallery"))

Examples

Retrieving an image

// NodeJS Server
var io = require('socket.io')(http)
var fs = require('fs')

io.on('connection', function(socket) {
  socket.on('getimage', function(msg) {
    // Image
    fs.readFile(__dirname + '/image.png', function(err, buf){
      // It's possible to embed binary data within arbitrarily-complex objects
      socket.emit('image', { image: true, buffer: buf.toString('base64') });
    });
  });
});
// SocketIO-Kit Client
socket.on(.Image) {
    switch $0 {
    case .JSON(let json):
        if let image = json["buffer"] as? String >>- SocketIOUtilities.base64EncodedStringToUIImage {
            println(image)
        }
    default:
        println("Not supported")
    }
}

Working with structs

struct ImageInfo: SocketIOObject {

    let buffer: String

    init(buffer: String) {
        self.buffer = buffer
    }

    init(dict: NSDictionary) {
        self.init(buffer: dict["buffer"] as! String) //Force casts should be avoided!
    }

    var asDictionary: NSDictionary {
        return ["buffer": buffer]
    }

}
// Example using ImageInfo
socket.on(.Image) {
    switch $0 {
    case .JSON(let json):
        let imageInfo = ImageInfo(dict: json) //<---

        if let image = imageInfo.buffer >>- SocketIOUtilities.base64EncodedStringToUIImage {
            println(image)
        }
    default:
        println("Not supported")
    }
}

Working with classes

class Person: SocketIOObject {

    let name: String

    init(name: String) {
        self.name = name
    }

    convenience required init(dict: NSDictionary) {
        self.init(name: dict["name"] as! String) //Force casts should be avoided!
    }

    var asDictionary: NSDictionary {
        return ["name": name]
    }

}
// Example sending John instance
let john = Person(name: "John")

socket.emit(.Login, withObject: john)

Features

To implement
  • [ ] Reconnect automatically
  • [x] Complete documentation
  • [ ] Test on iOS 7.0+ and Mac OS X 10.9+
  • [ ] BDD Tests

Debugging

You can opt into seeing messages by supplying the DEBUG flag. Just add -D DEBUG in Build Settings > Swift Compiler - Custom Flags: Other Swift Flags.

Contributing

See the [CONTRIBUTING] document. Thank you, contributors!

Author

Ricardo Pereira, @ricardopereiraw


*Note that all licence references and agreements mentioned in the SocketIO-Kit README section above are relevant to that project's source code only.