Fast Websockets in Swift for iOS and OSX
Clone or download
Latest commit 1f3049d Oct 26, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
Source Migration to Swift 4.2 Sep 30, 2018
SwiftWebSocket.xcodeproj Migration to Swift 4.2 Sep 30, 2018
Test-ObjectiveC [objc] Exposing `WebSocketReadyState` enum to Objective-C Jan 5, 2016
Test swift/3.0 Sep 10, 2016
tools swift/3.0 Sep 10, 2016
.gitignore ignore build directory Jan 22, 2016
.swift-version 2.6.5 Oct 12, 2016 2.6.1 May 6, 2016
LICENSE first commit May 26, 2015
Package.swift swift 2.2 Jan 19, 2016 Update Oct 17, 2018
SwiftWebSocket.podspec Release v2.7.0 Feb 18, 2018


API Docs Swift/4.0 Build Status

Conforming WebSocket (RFC 6455) client library for iOS and Mac OSX.

SwiftWebSocket passes all 521 of the Autobahn's fuzzing tests, including strict UTF-8, and message compression.

Project Status

I'm looking for someone to help with or take over maintenance of this project. If you are interested, please contact me at the email address listed on my GitHub profile page.


  • High performance.
  • 100% conforms to Autobahn Tests. Including base, limits, compression, etc. Test results.
  • TLS / WSS support. Self-signed certificate option.
  • The API is modeled after the Javascript API.
  • Reads compressed messages (permessage-deflate). RFC 7692
  • Send pings and receive pong events.
  • Strict UTF-8 processing.
  • binaryType property to choose between [UInt8] or NSData messages.
  • Zero asserts. All networking, stream, and protocol errors are routed through the error event.
  • iOS / Objective-C support.


func echoTest(){
    var messageNum = 0
    let ws = WebSocket("wss://")
    let send : ()->() = {
        messageNum += 1
        let msg = "\(messageNum): \(NSDate().description)"
        print("send: \(msg)")
    } = {
    ws.event.close = { code, reason, clean in
    ws.event.error = { error in
        print("error \(error)")
    ws.event.message = { message in
        if let text = message as? String {
            print("recv: \(text)")
            if messageNum == 10 {
            } else {

Custom Headers

var request = URLRequest(url: URL(string:"ws://url")!)
request.addValue("AUTH_TOKEN", forHTTPHeaderField: "Authorization")
request.addValue("Value", forHTTPHeaderField: "X-Another-Header")
let ws = WebSocket(request: request)

Reuse and Delaying WebSocket Connections

v2.3.0+ makes available an optional open method. This will allow for a WebSocket object to be instantiated without an immediate connection to the server. It can also be used to reconnect to a server following the close event.

For example,

let ws = WebSocket()
ws.event.close = { _,_,_ in                 // reopen the socket to the previous url"ws://otherurl")  // or, reopen the socket to a new url
}"ws://url") // call with url


The compression flag may be used to request compressed messages from the server. If the server does not support or accept the request, then connection will continue as normal, but with uncompressed messages.

let ws = WebSocket("ws://url")
ws.compression.on = true

Self-signed SSL Certificate

let ws = WebSocket("ws://url")
ws.allowSelfSignedSSL = true

Network Services (VoIP, Video, Background, Voice)

// Allow socket to handle VoIP in the background. = [.VoIP, .Background] 

Installation (iOS and OS X)


Add the following to your Cartfile:

github "tidwall/SwiftWebSocket"

Then run carthage update.

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

The import SwiftWebSocket directive is required in order to access SwiftWebSocket features.


Add the following to your Podfile:

pod 'SwiftWebSocket'

Then run pod install with CocoaPods 0.36 or newer.

The import SwiftWebSocket directive is required in order to access SwiftWebSocket features.


Copy the SwiftWebSocket/WebSocket.swift file into your project.
You must also add the libz.dylib library. Project -> Target -> Build Phases -> Link Binary With Libraries

There is no need for import SwiftWebSocket when manually installing.


Josh Baker @tidwall


SwiftWebSocket source code is available under the MIT License.