[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-go-websocket-cluster-all--*":3,"academy-blog-translations-8uyo22m1p5xsmxt":154},{"data":4,"page":142,"perPage":142,"totalItems":142,"totalPages":142},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":148,"keywords":149,"locale":124,"published_at":150,"scheduled_at":13,"school_blog":146,"short_description":151,"slug":152,"status":144,"title":6,"updated":153,"updated_by":13,"views":147},"Ep.27 Go and WebSocket Cluster - Enhancing Distributed User Support!","sclblg987654321","school_blog_translations","\u003Cp class=\"p1\">\u003Cstrong>Go and WebSocket Cluster - Enhancing Distributed User Support!\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">In this episode, we will discuss creating a WebSocket Server that can operate in a Cluster mode to support distributed usage, increasing the capacity to handle a large number of users and improving system stability!\u003C\u002Fp>\u003Cp class=\"p4\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>What is a Cluster?\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">A Cluster is a combination of multiple servers that work together as a single system, with a Load Balancer distributing tasks (such as WebSocket connections) to each server in the Cluster.\u003C\u002Fp>\u003Cp class=\"p4\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Advantages of Working in a Cluster :\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">1. Supports a large number of users : Distributes connections to multiple servers.\u003C\u002Fp>\u003Cp class=\"p3\">2. Increases stability : If some servers fail, the system continues to operate.\u003C\u002Fp>\u003Cp class=\"p3\">3. Easily scalable : Add or reduce the number of servers as needed.\u003C\u002Fp>\u003Cp class=\"p4\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Managing WebSocket in a Cluster\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">The main issues to address in a Cluster :\u003C\u002Fp>\u003Cp class=\"p3\">1. Session Stickiness : Ensures that a user's connections are sent to the same server.\u003C\u002Fp>\u003Cp class=\"p3\">2. Message Broadcasting : Allows messages sent from one server to be broadcast to all users in the Cluster.\u003C\u002Fp>\u003Cp class=\"p3\">3. State Synchronization : Maintains the state of users across multiple servers.\u003C\u002Fp>\u003Cp class=\"p4\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Building a WebSocket Cluster with Redis Pub\u002FSub\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">Redis Pub\u002FSub allows multiple servers to communicate with each other, where each server subscribes to messages from the same channel.\u003C\u002Fp>\u003Cp class=\"p4\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Example of Using Redis Pub\u002FSub for WebSocket Cluster\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">1. Install Redis and Go Redis Library\u003C\u002Fp>\u003Cp class=\"p3\">Install Redis on your server.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">sudo apt install redis\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp class=\"p2\">add Library Go Redis:&nbsp;\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">go get github.com\u002Fredis\u002Fgo-redis\u002Fv9\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp class=\"p3\">2. Example Code for WebSocket Cluster\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"log\"\n    \"net\u002Fhttp\"\n\n    \"github.com\u002Fgorilla\u002Fwebsocket\"\n    \"github.com\u002Fredis\u002Fgo-redis\u002Fv9\"\n)\n\nvar (\n    upgrader = websocket.Upgrader{\n        CheckOrigin: func(r *http.Request) bool {\n            return true\n        },\n    }\n    clients  = make(map[*websocket.Conn]bool)\n    broadcast = make(chan []byte)\n    rdb      = redis.NewClient(&amp;redis.Options{\n        Addr: \"localhost:6379\",\n    })\n    ctx = context.Background()\n)\n\nfunc handleConnections(w http.ResponseWriter, r *http.Request) {\n    conn, err := upgrader.Upgrade(w, r, nil)\n    if err != nil {\n        log.Println(\"Error upgrading connection:\", err)\n        return\n    }\n    defer conn.Close()\n\n    clients[conn] = true\n    defer delete(clients, conn)\n\n    for {\n        _, msg, err := conn.ReadMessage()\n        if err != nil {\n            log.Println(\"Error reading message:\", err)\n            break\n        }\n\n        \u002F\u002F ส่งข้อความไปยัง Redis Channel\n        if err := rdb.Publish(ctx, \"websocketChannel\", msg).Err(); err != nil {\n            log.Println(\"Error publishing to Redis:\", err)\n        }\n    }\n}\n\nfunc handleRedisMessages() {\n    sub := rdb.Subscribe(ctx, \"websocketChannel\")\n    ch := sub.Channel()\n\n    for msg := range ch {\n        log.Printf(\"Received message from Redis: %s\", msg.Payload)\n        broadcast &lt;- []byte(msg.Payload)\n    }\n}\n\nfunc handleBroadcasts() {\n    for {\n        msg := &lt;-broadcast\n        for client := range clients {\n            if err := client.WriteMessage(websocket.TextMessage, msg); err != nil {\n                log.Println(\"Error writing message:\", err)\n                client.Close()\n                delete(clients, client)\n            }\n        }\n    }\n}\n\nfunc main() {\n    http.HandleFunc(\"\u002Fws\", handleConnections)\n\n    go handleBroadcasts()\n    go handleRedisMessages()\n\n    log.Println(\"WebSocket server started at :8080\")\n    log.Fatal(http.ListenAndServe(\":8080\", nil))\n}\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Code Explanation:\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">1. Connect to Redis : All servers subscribe to websocketChannel.\u003C\u002Fp>\u003Cp class=\"p3\">2. Send Messages to Redis : When a user sends a message, it is published to Redis.\u003C\u002Fp>\u003Cp class=\"p3\">3. Broadcast Messages from Redis : Messages received from Redis are sent to all users connected to the server.\u003C\u002Fp>\u003Cp class=\"p4\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Testing the Cluster System:\u003C\u002Fstrong>\u003C\u002Fp>\u003Col class=\"ol1\">\u003Cli class=\"li3\">Run multiple WebSocket Servers (on different ports, e.g., :8080, :8081).\u003C\u002Fli>\u003Cli class=\"li3\">Use NGINX or a Load Balancer to distribute requests to each server.\u003C\u002Fli>\u003Cli class=\"li3\">Connect multiple clients and send messages to observe the results.\u003C\u002Fli>\u003C\u002Fol>\u003Cp class=\"p4\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Further Improvements:\u003C\u002Fstrong>\u003C\u002Fp>\u003Col class=\"ol1\">\u003Cli class=\"li3\">Add Sticky Sessions : Use a Load Balancer like NGINX to track user sessions.\u003C\u002Fli>\u003Cli class=\"li3\">Monitor Redis : Use tools like RedisInsight to check messages and Redis channels.\u003C\u002Fli>\u003Cli class=\"li3\">Enable Auto Scaling : Use Kubernetes to manage the number of servers.\u003C\u002Fli>\u003C\u002Fol>\u003Cp class=\"p4\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Summary:\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul class=\"ul1\">\u003Cli class=\"li3\">Use Redis Pub\u002FSub for message distribution in a WebSocket Cluster.\u003C\u002Fli>\u003Cli class=\"li3\">Use NGINX or a Load Balancer to distribute connections.\u003C\u002Fli>\u003Cli class=\"li3\">Improve state synchronization and add Sticky Sessions for enhanced performance.\u003C\u002Fli>\u003C\u002Ful>","24_11zon_7pvsf3ab2p.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002F4tgov0dlzzj6vmq\u002F24_11zon_7pvsf3ab2p.webp","2026-03-04 08:34:15.124Z","",{"keywords":15,"locale":118,"school_blog":128},[16,23,28,33,38,43,48,53,58,63,68,73,78,83,88,93,98,103,108,113],{"collectionId":17,"collectionName":18,"created":19,"created_by":13,"id":20,"name":21,"updated":22,"updated_by":13},"sclkey987654321","school_keywords","2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:32:51.346Z","tmzmy6jyz1n35rr","Go Programming","2026-04-10 16:08:01.434Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:33:59.808Z","qw7jy92h0uqd9wq","Go coding","2026-04-10 16:08:04.850Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:34:09.446Z","umkokcesoadazen","Go development","2026-04-10 16:08:08.379Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:33:59.315Z","btmgtfwmgpke1aa","Go language","2026-04-10 16:08:04.625Z",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-03-04 08:20:14.253Z","ah6lvy4x8qe08l5","Golang","2026-04-10 16:07:26.172Z",{"collectionId":17,"collectionName":18,"created":49,"created_by":13,"id":50,"name":51,"updated":52,"updated_by":13},"2026-03-04 08:34:00.920Z","ecac9y661or1xka","WebSocket","2026-04-10 16:08:05.227Z",{"collectionId":17,"collectionName":18,"created":54,"created_by":13,"id":55,"name":56,"updated":57,"updated_by":13},"2026-03-04 08:34:10.254Z","rss87kgvhtvkri1","Redis Pub\u002FSub","2026-04-10 16:08:08.714Z",{"collectionId":17,"collectionName":18,"created":59,"created_by":13,"id":60,"name":61,"updated":62,"updated_by":13},"2026-03-04 08:34:12.893Z","r8xmdym22w8bhnm","user support","2026-04-10 16:08:09.624Z",{"collectionId":17,"collectionName":18,"created":64,"created_by":13,"id":65,"name":66,"updated":67,"updated_by":13},"2026-03-04 08:34:13.771Z","kts1pcxglpsmge4","stability","2026-04-10 16:08:09.916Z",{"collectionId":17,"collectionName":18,"created":69,"created_by":13,"id":70,"name":71,"updated":72,"updated_by":13},"2026-03-04 08:34:14.357Z","pw77mrufqvygcbp","message broadcasting","2026-04-10 16:08:10.158Z",{"collectionId":17,"collectionName":18,"created":74,"created_by":13,"id":75,"name":76,"updated":77,"updated_by":13},"2026-03-04 08:34:14.671Z","mx45pnbomby0ttw","state synchronization","2026-04-10 16:08:10.324Z",{"collectionId":17,"collectionName":18,"created":79,"created_by":13,"id":80,"name":81,"updated":82,"updated_by":13},"2026-03-04 08:32:15.843Z","m0x7wo77i8iycf1","Programming Education","2026-04-10 16:07:51.675Z",{"collectionId":17,"collectionName":18,"created":84,"created_by":13,"id":85,"name":86,"updated":87,"updated_by":13},"2026-03-04 08:31:22.575Z","lfjse4xivbgg5wu","Practice programming","2026-04-10 16:07:39.541Z",{"collectionId":17,"collectionName":18,"created":89,"created_by":13,"id":90,"name":91,"updated":92,"updated_by":13},"2026-03-04 08:32:09.324Z","gon9gv2r39iu34p","programming development","2026-04-10 16:07:50.515Z",{"collectionId":17,"collectionName":18,"created":94,"created_by":13,"id":95,"name":96,"updated":97,"updated_by":13},"2026-03-04 08:32:26.073Z","vnvj1oaxje9m1q8","programming for beginners","2026-04-10 16:07:54.133Z",{"collectionId":17,"collectionName":18,"created":99,"created_by":13,"id":100,"name":101,"updated":102,"updated_by":13},"2026-03-04 08:31:49.362Z","2m9vv13etpn6zkx","programming language","2026-04-10 16:07:45.606Z",{"collectionId":17,"collectionName":18,"created":104,"created_by":13,"id":105,"name":106,"updated":107,"updated_by":13},"2026-03-04 08:20:33.316Z","ln1ntwattzmxo0o","programming","2026-04-10 16:07:27.299Z",{"collectionId":17,"collectionName":18,"created":109,"created_by":13,"id":110,"name":111,"updated":112,"updated_by":13},"2026-03-04 08:31:54.955Z","264sfjffyhspetq","programmers","2026-04-10 16:07:47.221Z",{"collectionId":17,"collectionName":18,"created":114,"created_by":13,"id":115,"name":116,"updated":117,"updated_by":13},"2026-03-04 08:26:59.195Z","gab60xd583s3qaw","Superdev School","2026-04-10 16:07:37.087Z",{"code":119,"collectionId":120,"collectionName":121,"created":122,"flag":123,"id":124,"is_default":125,"label":126,"updated":127},"en","pbc_1989393366","locales","2026-01-22 11:00:02.726Z","twemoji:flag-united-states","qv9c1llfov2d88z",false,"English","2026-04-10 15:42:46.825Z",{"category":129,"collectionId":130,"collectionName":131,"expand":132,"id":146,"views":147},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs",{"category":133},{"blogIds":134,"collectionId":135,"collectionName":136,"created":137,"created_by":13,"id":129,"image":138,"image_alt":13,"image_path":139,"label":140,"name":141,"priority":142,"publish_at":143,"scheduled_at":13,"status":144,"updated":145,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-04 08:33:53.210Z","59ty92ns80w_15oc1implw.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fwqxt7ag2gn7xcmk\u002F59ty92ns80w_15oc1implw.png",{"en":141,"th":141},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-04-25 02:32:15.470Z","8uyo22m1p5xsmxt",225,"4tgov0dlzzj6vmq",[20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110,115],"2025-01-27 04:41:37.283Z","Learn how to create a WebSocket Server in Cluster mode using Redis Pub\u002FSub to support a large number of users and improve system stability.","go-websocket-cluster","2026-04-25 02:47:30.252Z",{"en":152}]