A fast, scalable, and easy-to-use Go wrapper built on top of the Fiber WebSocket contributor package. It seamlessly bridges Fiber WebSockets and Redis Pub/Sub for building real-time, distributed applications.
socketx simplifies horizontal scaling for WebSocket servers in Go. By connecting Fiber's WebSocket ecosystem with Redis Pub/Sub, it ensures that messages sent to one server instance are instantly broadcasted to clients connected to any other instance, all while preventing concurrency issues and memory leaks.
go get github.com/an1sx7/socketxpackage main
import (
"log"
"github.com/gofiber/contrib/v3/websocket"
"github.com/gofiber/fiber/v3"
"github.com/redis/go-redis/v9"
"github.com/an1sx7/socketx"
)
func main() {
app := fiber.New()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
manager := socketx.NewManager(rdb, "global_chat")
app.Get("/ws", websocket.New(func(c *websocket.Conn) {
userId := c.Query("userId")
if userId == "" {
return
}
client := manager.NewClient(c, userId)
msgChan := make(chan socketx.Message)
go client.Read(msgChan)
for msg := range msgChan {
client.Publish(msg)
}
}))
log.Fatal(app.Listen(":3000"))
}- Built for Fiber: Works directly with Fiber's WebSocket contributor package.
- Distributed Messaging: Leverages Redis Pub/Sub to scale horizontally across multi-server setups.
- Auto Cleanup: Automatically deletes disconnected clients from internal memory to eliminate leaks