[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-redis-user-state-management-websocket-all--*":3,"academy-blog-translations-4rpshti4th04y0d":84},{"data":4,"page":72,"perPage":72,"totalItems":72,"totalPages":72},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":79,"keywords":80,"locale":54,"published_at":81,"scheduled_at":13,"school_blog":76,"short_description":82,"status":74,"title":6,"updated":83,"updated_by":13,"slug":77,"views":78},"EP.75 Using Redis for User State Management in WebSocket","sclblg987654321","school_blog_translations","\u003Cp>In this episode, we’ll explore how to use Redis to manage user state in a WebSocket system — such as tracking online users, handling connection status, and supporting real-time applications with thousands of users. Redis enables efficient and scalable state management without relying solely on in-memory data structures within the WebSocket server itself.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Redis is a high-speed in-memory data store, making it a perfect fit for handling real-time connection state in systems like chat applications, multiplayer games, or collaborative tools.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>🔸 Why Use Redis for User State in WebSocket?\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>1. Fast and efficient:\u003Cbr>Redis operates in memory, allowing quick read\u002Fwrite operations — ideal for real-time systems like WebSocket.\u003C\u002Fp>\u003Cp>2. Scales to many users:\u003Cbr>Redis helps distribute the load and supports massive concurrent connections without slowing down the server.\u003C\u002Fp>\u003Cp>3. Easy state tracking:\u003Cbr>You can store connection status, user login sessions, room presence, and other stateful information with ease.\u003C\u002Fp>\u003Cp>4. Supports horizontal scaling:\u003Cbr>Redis is compatible with clustering and sharding, making it suitable for scalable WebSocket architectures.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>🔸 Core Components of User State Management with Redis\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Col>\u003Cli>Store user state in Redis:\u003Cbr>Track whether a user is connected or not by storing their status in Redis.\u003C\u002Fli>\u003Cli>Update state on connection\u002Fdisconnection:\u003Cbr>When a user connects or disconnects, update their state accordingly in Redis.\u003C\u002Fli>\u003Cli>Retrieve state as needed:\u003Cbr>At any time, the server can check who is online or fetch the user’s current state.\u003C\u002Fli>\u003C\u002Fol>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>🔸 Installing and Connecting Redis with Go\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>✅ Install Redis client for Go\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-plaintext language-bash\">go get github.com\u002Fgo-redis\u002Fredis\u002Fv8\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>🔸 Example WebSocket Server with Redis Integration\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport (\n    \"log\"\n    \"net\u002Fhttp\"\n    \"context\"\n    \"github.com\u002Fgorilla\u002Fwebsocket\"\n    \"github.com\u002Fgo-redis\u002Fredis\u002Fv8\"\n)\n\nvar clients = make(map[*websocket.Conn]bool)\nvar redisClient *redis.Client\nvar ctx = context.Background()\n\ntype Message struct {\n    User    string `json:\"user\"`\n    Message string `json:\"message\"`\n}\n\nvar upgrader = websocket.Upgrader{\n    CheckOrigin: func(r *http.Request) bool {\n        return true\n    },\n}\n\nfunc init() {\n    redisClient = redis.NewClient(&amp;redis.Options{\n        Addr: \"localhost:6379\",\n        DB:   0,\n    })\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(err)\n        return\n    }\n    defer conn.Close()\n\n    clients[conn] = true\n\n    user := r.URL.Query().Get(\"user\")\n    if user == \"\" {\n        user = \"anonymous\"\n    }\n\n    \u002F\u002F Mark user as online in Redis\n    redisClient.Set(ctx, user, \"online\", 0)\n\n    for {\n        var msg Message\n        err := conn.ReadJSON(&amp;msg)\n        if err != nil {\n            log.Println(err)\n            delete(clients, conn)\n            \u002F\u002F Remove user state from Redis on disconnect\n            redisClient.Del(ctx, user)\n            break\n        }\n        broadcast(msg)\n    }\n}\n\nfunc broadcast(msg Message) {\n    for client := range clients {\n        err := client.WriteJSON(msg)\n        if err != nil {\n            log.Println(err)\n            client.Close()\n            delete(clients, client)\n        }\n    }\n}\n\nfunc main() {\n    http.HandleFunc(\"\u002F\", handleConnections)\n    log.Println(\"Server started on :8080\")\n    log.Fatal(http.ListenAndServe(\":8080\", nil))\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>🔸 Testing Redis User State Management\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Here are key tests you should perform:\u003C\u002Fp>\u003Cp>✅ Online state check:\u003Cbr>Verify that when a user connects, Redis stores their status correctly as \u003Ccode inline=\"\">online\u003C\u002Fcode>.\u003C\u002Fp>\u003Cp>✅ Disconnect cleanup:\u003Cbr>Check that the user’s status is removed or updated when they disconnect.\u003C\u002Fp>\u003Cp>✅ Real-time state query:\u003Cbr>Use \u003Ccode inline=\"\">redisClient.Get()\u003C\u002Fcode> to query user state and determine who is online in real time.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Chr>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>💡 Challenge: Build an Admin Dashboard\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Try building a real-time admin dashboard that displays a live list of online users using data pulled from Redis. This can help chat moderators or system admins monitor user activity more effectively.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>🔜 Coming Next in EP.76\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Handling Large Data Transfers in WebSocket\u003Cbr>In the next episode, we’ll discuss how to send and manage large data payloads over WebSocket while ensuring performance, stability, and minimal latency.\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.school.th\">\u003Cstrong>Superdev School &nbsp;(Superdev)\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>superdevschool\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🎬 TikTok: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.tiktok.com\u002F@superdevschool\">\u003Cstrong>superdevschool\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp class=\"\" data-start=\"5978\" data-end=\"6095\">\u003Cstrong>🌐 Website: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002F\">\u003Cstrong>www.superdev.school\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>","122_11zon_39rhxueass.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002F7gwkb5q2w0kv9lo\u002F122_11zon_39rhxueass.webp","2026-03-04 08:47:14.040Z","",{"keywords":15,"locale":48,"school_blog":58},[16,23,28,33,38,43],{"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:47:12.508Z","rjl0l49347kfuke","Distributed Architecture","2026-04-10 16:13:25.192Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:40:54.606Z","s26m2zvbm8amosp","Real-time Systems","2026-04-10 16:12:40.769Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:47:12.717Z","gcx28k3rmbtt0ij","User State","2026-04-10 16:13:25.295Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:34:00.920Z","ecac9y661or1xka","WebSocket","2026-04-10 16:08:05.227Z",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-03-04 08:44:31.678Z","bd6jtks2o3q53bx","Redis","2026-04-10 16:12:46.240Z",{"code":49,"collectionId":50,"collectionName":51,"created":52,"flag":53,"id":54,"is_default":55,"label":56,"updated":57},"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":59,"collectionId":60,"collectionName":61,"created":13,"expand":62,"id":76,"slug":77,"updated":13,"views":78},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs",{"category":63},{"blogIds":64,"collectionId":65,"collectionName":66,"created":67,"created_by":13,"id":59,"image":68,"image_alt":13,"image_path":69,"label":70,"name":71,"priority":72,"publish_at":73,"scheduled_at":13,"status":74,"updated":75,"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":71,"th":71},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-04-25 02:32:15.470Z","4rpshti4th04y0d","redis-user-state-management-websocket",231,"7gwkb5q2w0kv9lo",[20,25,30,35,40,45],"2025-08-13 02:09:57.929Z","Discover how to use Redis to manage user state in WebSocket servers — including online tracking, connection updates, and real-time data retrieval — for scalable and high-performance applications.","2026-05-06 08:38:16.998Z",{"th":77,"en":77}]