sacOO7 / socketcluster-client-go

GO client for socketcluster
https://socketcluster.io/
Apache License 2.0
56 stars 20 forks source link
golang gorilla socketcluster-client websocket

socketcluster-client-go

Refer examples for more details :

Overview

This client provides following functionality

To install use

    go get github.com/sacOO7/socketcluster-client-go/scclient

Description

Create instance of scclient by passing url of socketcluster-server end-point

    //Create a client instance
    client := scclient.New("ws://192.168.100.11:8000/socketcluster/");

Important Note : Default url to socketcluster end-point is always ws://somedomainname.com/socketcluster/.

Registering basic listeners

Different functions are given as an argument to register listeners

        package main

        import (
            "github.com/sacOO7/socketcluster-client-go/scclient"
            "text/scanner"
            "os"
            "fmt"
        )

        func onConnect(client scclient.Client) {
            fmt.Println("Connected to server")
        }

        func onDisconnect(client scclient.Client, err error) {
            fmt.Printf("Error: %s\n", err.Error())
        }

        func onConnectError(client scclient.Client, err error) {
            fmt.Printf("Error: %s\n", err.Error())
        }

        func onSetAuthentication(client scclient.Client, token string) {
            fmt.Println("Auth token received :", token)

        }

        func onAuthentication(client scclient.Client, isAuthenticated bool) {
            fmt.Println("Client authenticated :", isAuthenticated)
            go startCode(client)
        }  

        func main() {
            var reader scanner.Scanner
            client := scclient.New("ws://192.168.100.11:8000/socketcluster/");
            client.SetBasicListener(onConnect, onConnectError, onDisconnect)
            client.SetAuthenticationListener(onSetAuthentication, onAuthentication)
            go client.Connect()

            fmt.Println("Enter any key to terminate the program")
            reader.Init(os.Stdin)
            reader.Next()
            // os.Exit(0)
        }

        func startCode(client scclient.Client) {
            // start writing your code from here
            // All emit, receive and publish events
        }

Connecting to server

    //This will send websocket handshake request to socketcluster-server
    client.Connect()

Emitting and listening to events

Event emitter


    client.Emit(eventname,message);

    //  client.Emit("chat","This is a sample message")

    client.EmitAck("chat","This is a sample message", func(eventName string, error interface{}, data interface{}) {
        if error == nil {
            fmt.Println("Got ack for emit event with data ", data, " and error ", error)
        }
    })

Event Listener

The object received can be String, Boolean, Long or GO structure.

    // Receiver code without sending acknowledgement back
    client.On("chat", func(eventName string, data interface{}) {
        fmt.Println("Got data ", data, " for event ", eventName)
    })
    // Receiver code with ack
    client.OnAck("chat", func(eventName string, data interface{}, ack func(error interface{}, data interface{})) {
        fmt.Println("Got data ", data, " for event ", eventName)
        fmt.Println("Sending back ack for the event")
        ack("This is error", "This is data")
    }) 

Implementing Pub-Sub via channels

Creating channel

    // without acknowledgement
    client.Subscribe("mychannel")

    //with acknowledgement
    client.SubscribeAck("mychannel", func(channelName string, error interface{}, data interface{}) {
        if error == nil {
            fmt.Println("Subscribed to channel ", channelName, "successfully")
        }
    })

Publishing event on channel


       // without acknowledgement
       client.Publish("mychannel", "This is a data to be published")

       // with acknowledgement
       client.PublishAck("mychannel", "This is a data to be published", func(channelName string, error interface{}, data interface{}) {
            if error == nil {
                fmt.Println("Data published successfully to channel ", channelName)
            }
        })

Listening to channel

        client.OnChannel("mychannel", func(channelName string, data interface{}) {
                fmt.Println("Got data ", data, " for channel ", channelName)
        })

Un-subscribing to channel

         // without acknowledgement
        client.Unsubscribe("mychannel")

         // with acknowledgement
        client.UnsubscribeAck("mychannel", func(channelName string, error interface{}, data interface{}) {
            if error == nil {
                fmt.Println("Unsubscribed to channel ", channelName, "successfully")
            }
        })

Closing the connection with server

    client.Disconnect()

Setting request headers

    client.RequestHeader.Set("Accept-Encoding","gzip, deflate, sdch")
    client.RequestHeader.Set("Accept-Language","en-US,en;q=0.8")
    client.RequestHeader.Set("Pragma","no-cache")
    client.RequestHeader.Set("User-Agent","Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36")

Setting proxy server

    client.ConnectionOptions = gowebsocket.ConnectionOptions {
       Proxy: gowebsocket.BuildProxy("http://example.com"),
    }

Setting data compression, ssl verification and subprotocols

    client.ConnectionOptions = gowebsocket.ConnectionOptions {
        UseSSL:true,
        UseCompression:true,
        Subprotocols: [] string{"chat","superchat"},
    }