Categorygithub.com/eric11jhou/websocket/v2
modulepackage
2.0.3
Repository: https://github.com/eric11jhou/websocket.git
Documentation: pkg.go.dev

# README

websocket

Convenient, using Redis Pub/sub, Websocket lib for decentralized systems, kubenetes

Quick start

Import

import (
	"github.com/gin-gonic/gin"
	"github.com/go-redis/redis/v7"
	"github.com/eric11jhou/websocket"
)

Create Redis client

rdb := redis.NewClient(&redis.Options{
  Addr:     "localhost:6379",
  Password: "", // no password set
  DB:       0,  // use default DB
})

Create websocket engine

engine, _ := websocket.New(rdb)

Public Channels

Router

r := gin.Default()
publicChannel := engine.PublicChannel("foo")	//Subscribe "foo" Redis pub/sub
r.GET("/ws/info", publicChannel.Middleware(), handle1)

handle1

// Check authorization and rejection here
func handle1(c *gin.Context) {
	value, _ := c.Get("ws_channels")
	channels := value.(*websocket.Channels)

	received := channels.Received()
	for {
		select {
		case <-received:
		}
	}
}

Private Channels

Router

r := gin.Default()
privateChannel := engine.PrivateChannel("foo")
r.GET("/ws/order", handle1, privateChannel.Middleware(), handle2)

handle1

// Check authorization and rejection here
func handle1(c *gin.Context) {
	id := c.Param("user_id")  // You can also get the id from the database
	c.Set("ws_client_id", id) // ws_client_id is the prefix for client sending messages like bar:message
	c.Next()
}

handle2

// Subscribe to Redis pub/sub here
func handle2(c *gin.Context) {
	value, _ := c.Get("ws_channels")
	channels := value.(*websocket.Channels)

	received := channels.Received()
	for {
		select {
		case <- received:
		}
	}
}

publish

message := struct{
	ID	int64	`json:"id,string"`
	Data	interface{}	`json:"data"`
}{
	ID: 123,
	Data: "hello",	// ID只用來分配訊息,最終瀏覽器只會接收到Data
}
b, _ := json.Marshal(message)
rdb.Publish("foo", b)

# Functions

New Returns an Engine using Redis.

# Structs

Channels Manage a Hub and send and receive messages.
No description provided by the author
No description provided by the author
Hub maintains the set of active clients and broadcasts messages to the clients.
No description provided by the author