How to use it with redis adapter?
Hello, currently not supported.
@zishang520, Are there any plans to add?
@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.
@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)
}
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
There are indeed many problems and need to be refactored. Thank you for your help in testing.
@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
Thanks for your patience in this long process, the redis adapter has been released.