socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

How to use it with redis adapter?

Open minbala opened this issue 2 years ago • 7 comments

minbala avatar Jul 04 '23 02:07 minbala

Hello, currently not supported.

zishang520 avatar Jul 04 '23 11:07 zishang520

@zishang520, Are there any plans to add?

dotX12 avatar Aug 26 '23 21:08 dotX12

@zishang520, Are there any plans to add?

This feature is currently being perfected, and I will release it as soon as it is perfected. I now need to deal with my existing work and life issues first.

zishang520 avatar Aug 26 '23 23:08 zishang520

@dotX12 I have now completed the development version of the Redis adapter, but have not proceeded with further testing yet. My evaluation may have some issues. Could you help with additional testing?

go mod: https://github.com/zishang520/socket.io-go-redis

demo:

package main

import (
	"context"
	"os"
	"os/signal"
	"regexp"
	"syscall"

	"github.com/redis/go-redis/v9"
	_types "github.com/zishang520/engine.io-go-parser/types"
	"github.com/zishang520/engine.io/v2/types"
	"github.com/zishang520/engine.io/v2/utils"
	"github.com/zishang520/socket.io-go-redis/adapter"
	rtypes "github.com/zishang520/socket.io-go-redis/types"
	"github.com/zishang520/socket.io/v2/socket"
)

func main() {
	opts := socket.DefaultServerOptions()
	opts.SetAllowEIO3(true)
	opts.SetCors(&types.Cors{
		Origin:      "*",
		Credentials: true,
	})
	redisClient := rtypes.NewRedisClient(context.Background(), redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Username: "",
		Password: "",
		DB:       0,
	}))
	redisClient.On("error", func(errors ...any) {
		utils.Log().Error("Error: %v", errors)
	})
	opts.SetAdapter(&adapter.RedisAdapterBuilder{
		Redis: redisClient,
		Opts:  &adapter.RedisAdapterOptions{},
	})
	httpServer := types.NewWebServer(nil)
	socketio := socket.NewServer(httpServer, opts)
	socketio.Of(
		regexp.MustCompile(`/\w+`),
		nil,
	).Use(func(client *socket.Socket, next func(*socket.ExtendedError)) {
		next(nil)
	}).On("connection", func(clients ...interface{}) {
		client := clients[0].(*socket.Socket)
		client.On("event", func(clients ...interface{}) {
			client.Emit("event", map[string]interface{}{
				"message": _types.NewStringBufferString("event"),
			})
		})
	})
	httpServer.Listen("127.0.0.1:3000", nil)
	exit := make(chan struct{})
	SignalC := make(chan os.Signal)

	signal.Notify(SignalC, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
	go func() {
		for s := range SignalC {
			switch s {
			case syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT:
				close(exit)
				return
			}
		}
	}()

	<-exit
	socketio.Close(nil)
	os.Exit(0)
}

zishang520 avatar Dec 13 '23 02:12 zishang520

I try BroadcastWithAck function ,get "operation has timed out",

io.To(socket.Room(da["room"].(string))).Timeout(1000*time.Millisecond).Emit("test", da["message"], func(msg []any, err error) {log.Println(msg, err)})

this is my code https://github.com/shitingbao/socket.io_go/blob/main/example/adapter.go and I tried to implement this process myself and had the same problem,code myself https://github.com/shitingbao/socket.io_go/blob/main/example/redis_adapter_example.go,I find local function not implement Ack function in BroadcastWith,this will result in no BroadcastWithAck callback in redis adapter,because MESSAGE_BROADCAST_ACK not callback in redis subscribe

shitingbao avatar Jan 10 '24 03:01 shitingbao

There are indeed many problems and need to be refactored. Thank you for your help in testing.

zishang520 avatar Jan 11 '24 00:01 zishang520

@dotX12 @minbala I added a basic library to implement basic functions. You can try to use it first and improve it yourself, saving some repetitive work.in this https://github.com/shitingbao/socketio-go-redis-adapter

shitingbao avatar Jan 11 '24 02:01 shitingbao

Thanks for your patience in this long process, the redis adapter has been released.

zishang520 avatar Sep 22 '24 13:09 zishang520