Skip to content
Websocket framework for GO
Go
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
client
common
examples
.gitignore
LICENSE
README.md
client.go
event_handler.go
sockets.go
utils.go

README.md

   ____         __       __    
  / __/__  ____/ /_____ / /____
 _\ \/ _ \/ __/  '_/ -_) __(_-<
/___/\___/\__/_/\_\\__/\__/___/               

Build Status

Sockets is a websocket framework based on gorilla/websocket providing a simple way to write real-time apps.

Features

  • JWT authentication.
  • Room & Room Channel support.
  • Easily broadcast to Rooms/Channels.
  • Multiple connections under the same username.

Installation

go get github.com/Syleron/sockets

Simple client usage

package main

import (
    "fmt"
    sktsClient "github.com/Syleron/sockets/client"
    "github.com/Syleron/sockets/common"
    "time"
)

type SocketHandler struct{}

func (h *SocketHandler) NewConnection() {
    // Do something when a new connection comes in
    fmt.Println("> Connection established")
}

func (h *SocketHandler) ConnectionClosed() {
    // Do something when a connection is closed
    fmt.Println("> Connection closed")
}

func (h *SocketHandler) NewClientError(err error) {
    // Do something when a connection error is found
    fmt.Printf("> DEBUG %v\n", err)
}

func main() {
    // Generate JWT token
    jwt, err := common.GenerateJWT("steve", "SuperSecretKey")
    if err != nil {
        panic(err)
    }

    // Create our websocket client
    client, err := sktsClient.Dial("127.0.0.1:5000", jwt, false, &SocketHandler{})
    if err != nil {
        panic(err)
    }
    defer client.Close()

    // Define event handler
    client.HandleEvent("pong", pong)

    payload := &common.Message{
        EventName: "ping",
    }

    // Send our initial request
    client.Emit(payload)

    // Send another
    count := 0
    for range time.Tick(5 * time.Second) {
        if count < 1 {
            client.Emit(payload)
            count++
        } else {
            return
        }
    }
}

func pong(msg *common.Message) {
    fmt.Println("> Recieved WSKT 'pong'")
}

Simple server usage

package main

import (
    "fmt"
    "github.com/Syleron/sockets"
    "github.com/Syleron/sockets/common"
    "github.com/gin-gonic/gin"
)

type SocketHandler struct {}

func (h *SocketHandler) NewConnection(ctx *sockets.Context) {
    // Do something when a new connection comes in
    fmt.Println("> Connection established")
}

func (h *SocketHandler) ConnectionClosed(ctx *sockets.Context) {
    // Do something when a connection is closed
    fmt.Println("> Connection closed")
}

func main () {
    // Setup socket server
    sockets := sockets.New("SuperSecretKey", &SocketHandler{})

    // Register our events
    sockets.HandleEvent("ping", ping)

    // Set our gin release mode
    gin.SetMode(gin.ReleaseMode)

    // Setup router
    router := gin.Default()

    // Setup websockets
    router.GET("/ws", func(c *gin.Context) {
        sockets.HandleConnection(c.Writer, c.Request)
    })

    fmt.Println("> Sockets server started. Waiting for connections..")

    // Start server
    router.Run(":5000")
}

func ping(msg *common.Message, ctx *sockets.Context) {
    fmt.Println("> Recieved WSKT 'ping' responding with 'pong'")
    ctx.Emit(&common.Message{
        EventName: "pong",
    })
}

Projects using sockets

  • Yudofu: Anime social network

Note: If your project is not listed here, let us know! :)

Licence

MIT

You can’t perform that action at this time.