Skip to content

an1sx7/socketx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

socketx

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.

Description

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.

Installation

go get github.com/an1sx7/socketx

Usage

package 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"))
}

Featurs

  • 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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages