Code Monkey home page Code Monkey logo

go-socketcluster-client's Introduction

Go SocketCluster Client

This library can be used to connect to a SocketCluster server.

It was written as an alternative to the official implementation, because its API is a 1:1 copy from the JS implementation, whereas this library attempts to implement a somewhat more Golang-like API.

It uses the gorilla/websocket library, so it should be fully RFC-6455 compliant. Besides that, it reconnects automatically using an exponential backoff algorithm. It does not, however, implement JSON Web Tokens (JWT), though Pull Requests are of course welcome :)

Getting started

import "github.com/Freeaqingme/go-socketcluster-client"

const wsUrl = "wss://sc-02.coinigy.com/socketcluster/"

func main() {
    client := scclient.New(wsUrl)

    // Supply a callback for any events that need to be performed upon every reconnnect
    client.ConnectCallback = func() error {
        _, err := client.Emit("auth", &authEvent{apiKey, apiSecret })
        return err
    }

    if err := client.Connect(); err != nil {
        panic(err)
    }

    channel, err := client.Subscribe("TRADE-KRKN--XBT--EUR")
    if err != nil {
        panic(err)
    }

    go func() {
        for msg := range channel {
            fmt.Println("New kraken trade: " + string(msg))
        }
    }()

    if res, err := client.Emit("exchanges", nil); err != nil {
        panic(err)
    } else {
        fmt.Println("exchanges: " + string(res))
    }
}

Using a custom dialer

Useful if you'd like to use a local proxy or some other special use case

import (
    "github.com/Freeaqingme/go-socketcluster-client"
    "github.com/gorilla/websocket"
)

func main() {
    client := scclient.New(wsUrl)

    uProxy, _ := url.Parse("http://localhost:8888")
    dialer := &websocket.Dialer{
        Proxy: http.ProxyURL(uProxy),
        TLSClientConfig: &tls.Config{
            InsecureSkipVerify: true, // Running this on production is bad, fyi...
        },
    }

    if err := client.ConnectWithDialer(dialer); err != nil {
        panic(err)
    }
}

License

This project is licensed under the Apache2 License - see the LICENSE file for details

go-socketcluster-client's People

Contributors

freeaqingme 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.