Skip to content


Switch branches/tags

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time


Refer examples for more details :


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


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

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

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

Registering basic listeners

Different functions are given as an argument to register listeners

        package main
        import (
        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://");
        	client.SetBasicListener(onConnect, onConnectError, onDisconnect)
        	client.SetAuthenticationListener(onSetAuthentication, onAuthentication)
        	go client.Connect()
        	fmt.Println("Enter any key to terminate the program")
        	// 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

Emitting and listening to events

Event emitter

  • eventname is name of event and message can be String, boolean, int or structure
    //  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
    //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
         // 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


Setting request headers

	client.RequestHeader.Set("Accept-Encoding","gzip, deflate, sdch")
	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(""),

Setting data compression, ssl verification and subprotocols

  • It can be set using connectionOptions inside socket
    client.ConnectionOptions = gowebsocket.ConnectionOptions {
        Subprotocols: [] string{"chat","superchat"},
  • ConnectionOptions needs to be applied before connecting to server