Code Monkey home page Code Monkey logo

gdaxsocketswift's Introduction

Deprecated in favor of CoinbaseSocketSwift

Version License Platform

Features

  • WebSocket library agnostic (use any library you like! the example uses Starscream)
  • Minimal Swift codebase with all JSON parsing handled internally
  • Objects for all outgoing and incoming channel JSON
  • Optional automatic request signing when subscribing to channels

Please read over the official GDAX documentation before using this library.

This library was inspired by GDAXSwift.

Requirements

  • iOS 8.1+ / macOS 10.13+ / tvOS 9.0+ / watchOS 4.0+
  • Xcode 9.0+
  • Swift 4.0+

Installation

GDAXSocketSwift is available through CocoaPods. Check out Get Started tab on cocoapods.org to learn more.

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

pod 'GDAXSocketSwift'

Then run:

pod install

Usage

Import

First thing is to import the framework. See the Installation instructions above how to add the framework to your project using CocoaPods.

import GDAXSocketSwift
Initialize

Create an instance of GDAXSocketClient using the available initializer. Note that it is probably best to use a property, so it doesn't get deallocated right after being setup.

The apiKey, secret64, and passphrase parameters are optional if you want to receive authenticated messages. Read the official GDAX documentation for more details on authenticated WebSocket messages.

socketClient = GDAXSocketClient(apiKey: "apiKey", secret64: "secret64", passphrase: "passphrase")
Setup WebSocket

GDAXSocketSwift is made to be used with any WebSocket library. As such, a class that conforms to the protocol GDAXWebSocketClient must be passed in to GDAXSocketClient. GDAXSocketClient keeps a strong reference to this socket class and handles connecting, disconnecting, receiving messages, and connection status. Check out the example project to see how to do this with Starscream.

socketClient?.webSocket = ExampleWebSocketClient(url: URL(string: GDAXSocketClient.baseAPIURLString)!)
Setup Logger (optional)

If you want to see logs from GDAXSocketSwift you can optionally pass a class that conforms to the protocol GDAXSocketClientLogger. A GDAXSocketClientDefaultLogger is provided with an example of basic logging.

socketClient?.logger = GDAXSocketClientDefaultLogger()
Setup Delegate

Finally, set a class to receive the delegate calls.

socketClient?.delegate = self

Then you can create an extension and implement the delegate methods. The delegate methods are optional (by way of empty default implementations).

extension ViewController: GDAXSocketClientDelegate {
    func gdaxSocketDidConnect(socket: GDAXSocketClient) {
        socket.subscribe(channels:[.ticker], productIds:[.BTCUSD])
    }
    
    func gdaxSocketDidDisconnect(socket: GDAXSocketClient, error: Error?) {
        
    }
    
    func gdaxSocketClientOnErrorMessage(socket: GDAXSocketClient, error: GDAXErrorMessage) {
        print(error.message)
    }
    
    func gdaxSocketClientOnTicker(socket: GDAXSocketClient, ticker: GDAXTicker) {
        let formattedPrice = priceFormatter.string(from: ticker.price as NSNumber) ?? "0.0000"
        self.tickerLabel.text = ticker.type.rawValue
        self.priceLabel.text = "Price = " + formattedPrice
        self.productIdLabel.text = ticker.productId.rawValue
        
        if let time = ticker.time {
            self.timeLabel.text = timeFormatter.string(from: time)
        } else {
            self.timeLabel.text = timeFormatter.string(from: Date())
        }
    }
}

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

TODOs

  • Tests...

Author

Hani Shabsigh, LinkedIn, GitHub

License

GDAXSocketSwift is available under the MIT license. See the LICENSE file for more info.

Donations

Making money using this? Support open source by donating.

Bitcoin: 1EkkFgBZp4jN21b6N85ZWDmxMohytt9L2Z

Bitcoin

gdaxsocketswift's People

Contributors

hanishabsigh avatar lemamichael avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.