All Projects → sacOO7 → socketcluster-client-go

sacOO7 / socketcluster-client-go

Licence: Apache-2.0 license
GO client for socketcluster

Programming Languages

go
31211 projects - #10 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to socketcluster-client-go

Http
An alternative HTTP client implementation for Go.
Stars: ✭ 242 (+384%)
Mutual labels:  gorilla
Websocket
A fast, well-tested and widely used WebSocket implementation for Go.
Stars: ✭ 16,070 (+32040%)
Mutual labels:  gorilla
Mux
A powerful HTTP router and URL matcher for building Go web servers with 🦍
Stars: ✭ 15,667 (+31234%)
Mutual labels:  gorilla
Websocket
Gorilla WebSocket implementation for fasthttp.
Stars: ✭ 193 (+286%)
Mutual labels:  gorilla
Sessions
Package gorilla/sessions provides cookie and filesystem sessions and infrastructure for custom session backends.
Stars: ✭ 2,148 (+4196%)
Mutual labels:  gorilla
Pat
a pretty simple HTTP router for Go.
Stars: ✭ 113 (+126%)
Mutual labels:  gorilla
Wstest
go websocket client for unit testing of a websocket handler
Stars: ✭ 83 (+66%)
Mutual labels:  gorilla
Gowebsocket
Gorilla websockets based simplified websocket-client implementation in GO.
Stars: ✭ 77 (+54%)
Mutual labels:  gorilla
Handlers
A collection of useful middleware for Go HTTP services & web applications 🛃
Stars: ✭ 1,174 (+2248%)
Mutual labels:  gorilla
Pgstore
A Postgres session store backend for gorilla/sessions
Stars: ✭ 66 (+32%)
Mutual labels:  gorilla
Css
A CSS3 tokenizer.
Stars: ✭ 63 (+26%)
Mutual labels:  gorilla
Carrot
🥕 Build multi-device AR applications
Stars: ✭ 32 (-36%)
Mutual labels:  gorilla
Schema
Package gorilla/schema fills a struct with form values.
Stars: ✭ 884 (+1668%)
Mutual labels:  gorilla
Csrf
gorilla/csrf provides Cross Site Request Forgery (CSRF) prevention middleware for Go web applications & services 🔒
Stars: ✭ 631 (+1162%)
Mutual labels:  gorilla
Context
A golang registry for global request variables.
Stars: ✭ 406 (+712%)
Mutual labels:  gorilla
bana
Set of extensions for Autodesk Maya's Python API.
Stars: ✭ 32 (-36%)
Mutual labels:  gorilla
gorilla-cpm
GORILLA.BAS port to CP/M in Turbo Modula-2. Supported terminals: VT52, VT100, ANSI, ADM-31, KayPro, C128, Memotech monochrome, CPC / Zenith Z19
Stars: ✭ 45 (-10%)
Mutual labels:  gorilla
go course
個人多年來學習與實作上的心得筆記
Stars: ✭ 25 (-50%)
Mutual labels:  gorilla

socketcluster-client-go

Refer examples for more details :

Overview

This client provides following functionality

  • Easy to setup and use
  • Support for emitting and listening to remote events
  • Pub/sub
  • Authentication (JWT)
  • Can be used for testing of all server side functions

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

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

Emitting and listening to events

Event emitter

  • eventname is name of event and message can be String, boolean, int or structure
    client.Emit(eventname,message);
        
    //  client.Emit("chat","This is a sample message")
  • To send event with acknowledgement
	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

  • For listening to events :

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)
	})
    
  • To send acknowledgement back to server
    // 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

  • For creating and subscribing to channels:
    // 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

  • For publishing event :
       // 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

  • For listening to channel event :
        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

  • It can be set using connectionOptions by providing url to proxy server
    client.ConnectionOptions = gowebsocket.ConnectionOptions {
       Proxy: gowebsocket.BuildProxy("http://example.com"),
    }

Setting data compression, ssl verification and subprotocols

  • It can be set using connectionOptions inside socket
    client.ConnectionOptions = gowebsocket.ConnectionOptions {
        UseSSL:true,
        UseCompression:true,
        Subprotocols: [] string{"chat","superchat"},
    }
  • ConnectionOptions needs to be applied before connecting to server
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].