Latest 9.0.1
Homepage https://github.com/socketio/socket.io-client-swift
License MIT
Platforms ios 8.0, osx 10.10, tvos 9.0, requires ARC
Authors

Build Status

Socket.IO-client for iOS/OS X.

Example

import SocketIO

let socket = SocketIOClient(socketURL: URL(string: "http://localhost:8080")!, config: [.log(true), .forcePolling(true)])

socket.on(clientEvent: .connect) {data, ack in
    print("socket connected")
}

socket.on("currentAmount") {data, ack in
    if let cur = data[0] as? Double {
        socket.emitWithAck("canUpdate", cur).timingOut(after: 0) {data in
            socket.emit("update", ["amount": cur + 2.50])
        }

        ack.with("Got your currentAmount", "dude")
    }
}

socket.connect()

Objective-C Example

@import SocketIO;
NSURL* url = [[NSURL alloc] initWithString:@"http://localhost:8080"];
SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:url config:@{@"log": @YES, @"forcePolling": @YES}];

[socket on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) {
    NSLog(@"socket connected");
}];

[socket on:@"currentAmount" callback:^(NSArray* data, SocketAckEmitter* ack) {
    double cur = [[data objectAtIndex:0] floatValue];

    [[socket emitWithAck:@"canUpdate" with:@[@(cur)]] timingOutAfter:0 callback:^(NSArray* data) {
        [socket emit:@"update" withItems:@[@{@"amount": @(cur + 2.50)}]];
    }];

    [ack with:@[@"Got your currentAmount, ", @"dude"]];
}];

[socket connect];

Features

  • Supports socket.io 1.0+
  • Supports binary
  • Supports Polling and WebSockets
  • Supports TLS/SSL
  • Can be used from Objective-C

Installation

Requires Swift 3/Xcode 8.x

If you need swift 2.3 use the swift2.3 tag (Pre-Swift 3 support is no longer maintained)

If you need swift 2.2 use 7.x (Pre-Swift 3 support is no longer maintained)

If you need Swift 2.1 use v5.5.0 (Pre-Swift 2.2 support is no longer maintained)

If you need Swift 1.2 use v2.4.5 (Pre-Swift 2 support is no longer maintained)

If you need Swift 1.1 use v1.5.2. (Pre-Swift 1.2 support is no longer maintained)

Manually (iOS 7+)

  1. Copy the Source folder into your Xcode project. (Make sure you add the files to your target(s))
  2. If you plan on using this from Objective-C, read this on exposing Swift code to Objective-C.

Swift Package Manager

Add the project as a dependency to your Package.swift:

import PackageDescription

let package = Package(
    name: "YourSocketIOProject",
    dependencies: [
        .Package(url: "https://github.com/socketio/socket.io-client-swift", majorVersion: 9)
    ]
)

Then import import SocketIO.

Carthage

Add this line to your Cartfile:

github "socketio/socket.io-client-swift" ~> 9.0.1 # Or latest version

Run carthage update --platform ios,macosx.

CocoaPods 1.0.0 or later

Create Podfile and add pod 'Socket.IO-Client-Swift':

use_frameworks!

target 'YourApp' do
    pod 'Socket.IO-Client-Swift', '~> 9.0.1' # Or latest version
end

Install pods:

$ pod install

Import the module:

Swift:

import SocketIO

Objective-C:

@import SocketIO;

CocoaSeeds

Add this line to your Seedfile:

github "socketio/socket.io-client-swift", "v9.0.1", :files => "Source/*.swift" # Or latest version

Run seed install.

Docs

Detailed Example

A more detailed example can be found here

An example using the Swift Package Manager can be found here

License

MIT

Latest podspec

{
    "name": "Socket.IO-Client-Swift",
    "module_name": "SocketIO",
    "version": "9.0.1",
    "summary": "Socket.IO-client for iOS and OS X",
    "description": "Socket.IO-client for iOS and OS X.nSupports ws/wss/polling connections and binary.nFor socket.io 1.0+ and Swift.",
    "homepage": "https://github.com/socketio/socket.io-client-swift",
    "license": {
        "type": "MIT"
    },
    "authors": {
        "Erik": "[email protected]"
    },
    "platforms": {
        "ios": "8.0",
        "osx": "10.10",
        "tvos": "9.0"
    },
    "source": {
        "git": "https://github.com/socketio/socket.io-client-swift.git",
        "tag": "v9.0.1"
    },
    "source_files": "Source/**/*.swift",
    "requires_arc": true,
    "pod_target_xcconfig": {
        "SWIFT_VERSION": "3.1"
    },
    "pushed_with_swift_version": "3.1"
}

Pin It on Pinterest

Share This