[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-golang-websocket-redis-pubsub-all--*":3,"academy-blog-translations-m3tqwd4wu0hq9ka":79},{"data":4,"page":67,"perPage":67,"totalItems":67,"totalPages":67},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":73,"keywords":74,"locale":49,"published_at":75,"scheduled_at":13,"school_blog":71,"short_description":76,"slug":77,"status":69,"title":6,"updated":78,"updated_by":13,"views":72},"EP.86 Managing WebSocket Connections Using Redis Pub\u002FSub","sclblg987654321","school_blog_translations","\u003Cp>As real-time applications grow and user numbers increase, running a single WebSocket server may no longer be sufficient. This creates the need to scale out your WebSocket infrastructure using multiple instances (multi-instance setup) to handle a larger load.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>In this episode, we’ll walk you through how to use Redis Pub\u002FSub to synchronize communication across multiple WebSocket server instances — so that users connected to any server can still send and receive messages in real time.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>🔄 Why Use Redis Pub\u002FSub?\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Typically, a WebSocket server only holds connections of the clients that are directly connected to it.\u003Cbr>When multiple instances are running, a client connected to Server A won’t be able to send messages to clients on Server B — unless we bridge the communication between servers.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Redis Pub\u002FSub solves this challenge by acting as a message broker between WebSocket server instances:\u003C\u002Fp>\u003Cfigure class=\"table\">\u003Ctable>\u003Cthead>\u003Ctr>\u003Cth>Role\u003C\u002Fth>\u003Cth>Description\u003C\u002Fth>\u003C\u002Ftr>\u003C\u002Fthead>\u003Ctbody>\u003Ctr>\u003Ctd>Publisher\u003C\u002Ftd>\u003Ctd>When a client sends a message to a WebSocket server, that server publishes the message to a Redis channel.\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Subscriber\u003C\u002Ftd>\u003Ctd>All WebSocket server instances subscribe to the same Redis channel and broadcast received messages to their connected clients.\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003C\u002Ffigure>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>✅ Result:\u003C\u002Fh3>\u003Cul>\u003Cli>All users receive the same messages regardless of which server they are connected to.\u003C\u002Fli>\u003Cli>The system is ready for horizontal scaling with ease.\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>🧪 Example: Go + Redis Pub\u002FSub\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"log\"\n    \"net\u002Fhttp\"\n\n    \"github.com\u002Fgo-redis\u002Fredis\u002Fv8\"\n    \"github.com\u002Fgorilla\u002Fwebsocket\"\n)\n\nvar (\n    upgrader = websocket.Upgrader{\n        CheckOrigin: func(r *http.Request) bool { return true },\n    }\n    clients = make(map[*websocket.Conn]bool)\n    ctx     = context.Background()\n)\n\nfunc main() {\n    rdb := redis.NewClient(&amp;redis.Options{\n        Addr: \"localhost:6379\",\n    })\n\n    go subscribeRedis(rdb)\n\n    http.HandleFunc(\"\u002Fws\", func(w http.ResponseWriter, r *http.Request) {\n        conn, err := upgrader.Upgrade(w, r, nil)\n        if err != nil {\n            log.Println(\"Upgrade error:\", err)\n            return\n        }\n        defer conn.Close()\n        clients[conn] = true\n\n        for {\n            _, msg, err := conn.ReadMessage()\n            if err != nil {\n                log.Println(\"ReadMessage error:\", err)\n                delete(clients, conn)\n                break\n            }\n\n            \u002F\u002F Publish message to Redis\n            if err := rdb.Publish(ctx, \"chat_channel\", msg).Err(); err != nil {\n                log.Println(\"Publish error:\", err)\n            }\n        }\n    })\n\n    log.Println(\"WebSocket server started on :8080\")\n    http.ListenAndServe(\":8080\", nil)\n}\n\nfunc subscribeRedis(rdb *redis.Client) {\n    sub := rdb.Subscribe(ctx, \"chat_channel\")\n    ch := sub.Channel()\n\n    for msg := range ch {\n        for client := range clients {\n            if err := client.WriteMessage(websocket.TextMessage, []byte(msg.Payload)); err != nil {\n                log.Println(\"WriteMessage error:\", err)\n                client.Close()\n                delete(clients, client)\n            }\n        }\n    }\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>🔍 Code Explanation\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cfigure class=\"table\">\u003Ctable>\u003Cthead>\u003Ctr>\u003Cth>Section\u003C\u002Fth>\u003Cth>Description\u003C\u002Fth>\u003C\u002Ftr>\u003C\u002Fthead>\u003Ctbody>\u003Ctr>\u003Ctd>\u003Ccode inline=\"\">clients map\u003C\u002Fcode>\u003C\u002Ftd>\u003Ctd>Tracks WebSocket connections for clients connected to the current instance\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>\u003Ccode inline=\"\">rdb.Publish\u003C\u002Fcode>\u003C\u002Ftd>\u003Ctd>Sends a message to the Redis channel when a client sends data\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>\u003Ccode inline=\"\">subscribeRedis()\u003C\u002Fcode>\u003C\u002Ftd>\u003Ctd>Listens to Redis channel and forwards incoming messages to connected clients\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003C\u002Ffigure>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>💡 Benefits of Using Redis Pub\u002FSub\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>✅ Supports multi-instance WebSocket servers\u003Cbr>✅ Enables easy horizontal scaling\u003Cbr>✅ Handles high user loads without losing message integrity\u003Cbr>✅ Instances don’t need to know about each other directly\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Chr>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>🚀 Challenge: Try It Yourself!\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Col>\u003Cli>Launch multiple WebSocket server instances (e.g., multiple processes or containers)\u003C\u002Fli>\u003Cli>Use Redis Pub\u002FSub as a central message broker\u003C\u002Fli>\u003Cli>Test cross-server communication: clients connected to different servers should still receive messages from each other\u003C\u002Fli>\u003C\u002Fol>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>🔜 Next EP:\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>EP.87: Securing WebSocket Servers Against DDoS Attacks\u003C\u002Fp>\u003Cp>We’ll explore how to protect your WebSocket infrastructure from threats like DDoS, connection flooding, and malformed frame attacks, ensuring your real-time system is production ready.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp data-start=\"498\" data-end=\"834\">\u003Cstrong>Read more\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul>\u003Cli>\u003Cp data-start=\"498\" data-end=\"834\">\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FGolang\">\u003Cstrong>Golang The Series\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp data-start=\"498\" data-end=\"834\">\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FJS2GO\">\u003Cstrong>JS2GO\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp data-start=\"498\" data-end=\"834\">\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fen\u002Fblogs\u002Fcategories\u002FTailwind%20CSS\">\u003Cstrong>10 Eps That Will Make You a Pro Tailwind CSS Overnight\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Cp>\u003Cstrong>🔵 Facebook: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.facebook.com\u002Fsuperdev.academy.th\">\u003Cstrong>Superdev Academy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🔴 YouTube: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.youtube.com\u002Fchannel\u002FUC2eI2RxcA2zbTqZyHyWIfRg\">\u003Cstrong>Superdev Academy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>📸 Instagram: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.instagram.com\u002Fsuperdevschool\u002F\">\u003Cstrong>Superdev Academy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🎬 TikTok: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\">\u003Cstrong>https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🌐 Website: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdevacademy.com\u002Fen\">\u003Cstrong>https:\u002F\u002Fwww.superdevacademy.com\u002Fen\u003C\u002Fstrong>\u003C\u002Fa>\u003Cstrong>&nbsp;\u003C\u002Fstrong>\u003C\u002Fp>","144_11zon_7smibegq0b.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fa9935bo66vozl58\u002F144_11zon_7smibegq0b.webp","2026-03-04 08:46:52.462Z","",{"keywords":15,"locale":43,"school_blog":53},[16,23,28,33,38],{"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:34:10.254Z","rss87kgvhtvkri1","Redis Pub\u002FSub","2026-04-10 16:08:08.714Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:44:31.678Z","bd6jtks2o3q53bx","Redis","2026-04-10 16:12:46.240Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:34:00.920Z","ecac9y661or1xka","WebSocket","2026-04-10 16:08:05.227Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:20:14.253Z","ah6lvy4x8qe08l5","Golang","2026-04-10 16:07:26.172Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"code":44,"collectionId":45,"collectionName":46,"created":47,"flag":48,"id":49,"is_default":50,"label":51,"updated":52},"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":54,"collectionId":55,"collectionName":56,"expand":57,"id":71,"views":72},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs",{"category":58},{"blogIds":59,"collectionId":60,"collectionName":61,"created":62,"created_by":13,"id":54,"image":63,"image_alt":13,"image_path":64,"label":65,"name":66,"priority":67,"publish_at":68,"scheduled_at":13,"status":69,"updated":70,"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":66,"th":66},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-04-25 02:32:15.470Z","m3tqwd4wu0hq9ka",255,"a9935bo66vozl58",[20,25,30,35,40],"2025-09-05 03:37:59.560Z","Learn how to use Redis Pub\u002FSub to enable real-time communication across multiple WebSocket server instances in Go. Includes full example code and practical guidance for scalable deployments.","golang-websocket-redis-pubsub","2026-04-25 02:48:09.528Z",{"en":77}]