[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-ep114-multi-device-multi-session-websocket-all--*":3,"academy-blog-faqs-wzoz7l3fkolypwa-en":91,"academy-blog-translations-wzoz7l3fkolypwa":92},{"data":4,"page":77,"perPage":77,"totalItems":77,"totalPages":77},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_l_url":11,"cover_image_m_url":12,"cover_image_path":13,"cover_image_s_url":14,"created":15,"created_by":16,"expand":17,"id":85,"keywords":86,"locale":57,"published_at":87,"scheduled_at":65,"school_blog":81,"short_description":88,"status":79,"title":6,"updated":89,"updated_by":90,"slug":82,"views":84},"EP.114 Managing Multi-device & Multi-session in WebSocket Systems","sclblg987654321","school_blog_translations","\u003Cp>In real-world real-time applications such as chat systems, notifications, or online games, a single user might log in from multiple devices simultaneously, like a mobile phone and a desktop 💻📱\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>If your WebSocket system doesn't handle multi-device or multi-session properly, you may face issues like delayed synchronization, duplicate messages, or session leaks.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>In this episode, you'll learn how to build a WebSocket system that:\u003C\u002Fp>\u003Cul>\u003Cli>Supports multiple active devices per user\u003C\u002Fli>\u003Cli>Manages independent sessions securely\u003C\u002Fli>\u003Cli>Syncs data across devices in real time\u003C\u002Fli>\u003Cli>Is production-ready for real-world scale 🚀\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>🔍 Common Problems with Multiple Sessions\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cfigure class=\"table\">\u003Ctable>\u003Cthead>\u003Ctr>\u003Cth>Problem\u003C\u002Fth>\u003Cth>Description\u003C\u002Fth>\u003C\u002Ftr>\u003C\u002Fthead>\u003Ctbody>\u003Ctr>\u003Ctd>Duplicate Sessions\u003C\u002Ftd>\u003Ctd>The same user opens multiple devices → duplicate messages appear\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Connection Leak\u003C\u002Ftd>\u003Ctd>Previous sessions not closed when a new device connects\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Expired Token\u003C\u002Ftd>\u003Ctd>Poor refresh token handling causes disconnection\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Slow Sync\u003C\u002Ftd>\u003Ctd>Delayed data updates across devices\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003C\u002Ffigure>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>✅ A solid system should give each device its own session, but ensure synchronized data across all.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>🧩 WebSocket Session Structure\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">type Session struct {\n\tUserID     string\n\tDeviceID   string\n\tConnection *websocket.Conn\n}\n\nvar (\n\tactiveSessions = make(map[string]map[string]*Session) \u002F\u002F userID → deviceID → session\n\tmu             sync.Mutex\n)\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>✅ One user can have multiple devices\u003Cbr>✅ Each device has its own session\u003Cbr>✅ You can send messages to specific devices or broadcast to all\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>🔌 Handling Connections from Multiple Devices\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">func handleConnection(w http.ResponseWriter, r *http.Request) {\n\tconn, _ := upgrader.Upgrade(w, r, nil)\n\tuserID := r.URL.Query().Get(\"user_id\")\n\tdeviceID := r.URL.Query().Get(\"device_id\")\n\n\tmu.Lock()\n\tif activeSessions[userID] == nil {\n\t\tactiveSessions[userID] = make(map[string]*Session)\n\t}\n\tactiveSessions[userID][deviceID] = &amp;Session{\n\t\tUserID:     userID,\n\t\tDeviceID:   deviceID,\n\t\tConnection: conn,\n\t}\n\tmu.Unlock()\n\n\tdefer func() {\n\t\tmu.Lock()\n\t\tdelete(activeSessions[userID], deviceID)\n\t\tif len(activeSessions[userID]) == 0 {\n\t\t\tdelete(activeSessions, userID)\n\t\t}\n\t\tmu.Unlock()\n\t\tconn.Close()\n\t}()\n\n\tfor {\n\t\t_, msg, err := conn.ReadMessage()\n\t\tif err != nil {\n\t\t\tbreak\n\t\t}\n\t\tlog.Printf(\"[%s-%s]: %s\", userID, deviceID, msg)\n\t}\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>✅ When a device disconnects, its session is removed\u003Cbr>✅ No stale sessions remain in the system\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>🔐 Token &amp; Session Validation\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">func validateToken(token string) (string, error) {\n\tclaims := &amp;jwt.StandardClaims{}\n\t_, err := jwt.ParseWithClaims(token, claims, func(token *jwt.Token) (interface{}, error) {\n\t\treturn []byte(\"SECRET_KEY\"), nil\n\t})\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\treturn claims.Subject, nil\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cul>\u003Cli>Each device should have its own access token\u003C\u002Fli>\u003Cli>Refresh tokens should be handled per device\u003C\u002Fli>\u003Cli>If a token expires → disconnect only that session\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>🌐 Real-time Sync Across Devices\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">func syncToAllDevices(userID string, message string) {\n\tmu.Lock()\n\tdefer mu.Unlock()\n\tfor _, session := range activeSessions[userID] {\n\t\tsession.Connection.WriteMessage(websocket.TextMessage, []byte(message))\n\t}\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>✅ Send a message from mobile → it appears on desktop instantly\u003Cbr>✅ Achieve real-time sync across all devices\u003Cbr>✅ Fully ready for production-scale applications\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>🧱 Production-level Recommendations\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cfigure class=\"table\">\u003Ctable>\u003Cthead>\u003Ctr>\u003Cth>Category\u003C\u002Fth>\u003Cth>Recommendation\u003C\u002Fth>\u003C\u002Ftr>\u003C\u002Fthead>\u003Ctbody>\u003Ctr>\u003Ctd>Session Storage\u003C\u002Ftd>\u003Ctd>Use Redis or a database to persist session state\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Security\u003C\u002Ftd>\u003Ctd>Bind tokens to specific device IDs\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Sync\u003C\u002Ftd>\u003Ctd>Use Redis Pub\u002FSub to distribute updates\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Timeout\u003C\u002Ftd>\u003Ctd>Automatically remove sessions after idle timeout\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Monitoring\u003C\u002Ftd>\u003Ctd>Track the number of connected devices per user\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003C\u002Ffigure>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>🚀 Ready for a Challenge?\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Try building a system where: One user logs in from both mobile and desktop → Sends a message from one device → Instantly appears on the other\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>If you pull this off, you're building like the pros do in real production environments ✅\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Chr>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>🌟 Coming Next: Advanced Multiplayer Game Systems (EP.115)\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>In EP.115, we’ll enter the world of real-time multiplayer games.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Learn how to sync player state, events, and positions across all users using Go and WebSocket so you're ready to build next-gen interactive gaming platforms 🎮\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cdiv class=\"raw-html-embed\">\u003Cdiv style=\"margin:0 0 6px 0; font-weight:700;\">Read more:\u003C\u002Fdiv>\n\u003Cul style=\"list-style:none; padding:0; margin:0; line-height:1.4;\">\n  \u003Cli style=\"margin:0;\">\u003Ca href=\"\u002Fen\u002Fblogs\u002Fcategories\u002FGolang\" title=\"Golang The Series\">Golang The Series\u003C\u002Fa>\u003C\u002Fli>\n  \u003Cli style=\"margin:0;\">\u003Ca href=\"\u002Fen\u002Fblogs\u002Fcategories\u002FJS2GO\" title=\"JS2GO\">JS2GO\u003C\u002Fa>\u003C\u002Fli>\n  \u003Cli style=\"margin:0;\">\u003Ca href=\"\u002Fen\u002Fblogs\u002Fcategories\u002FTailwind%20CSS\" title=\"Tailwind CSS\">Tailwind CSS\u003C\u002Fa>\u003C\u002Fli>\n\u003C\u002Ful>\u003C\u002Fdiv>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cdiv class=\"raw-html-embed\">\n  \u003Cp style=\"margin:0 0 6px 0;\">\u003Cstrong>Follow Us:\u003C\u002Fstrong>\u003C\u002Fp>\n  \u003Cul style=\"list-style:none; padding:0; margin:0; line-height: 0.4;\">\n    \u003Cli style=\"display:flex; align-items:center; gap:6px; margin:0;\">\n      \n      \u003Csvg width=\"16\" height=\"16\" viewBox=\"0 0 24 24\" fill=\"#1877F2\" aria-hidden=\"true\">\n        \u003Cpath d=\"M22 12.07C22 6.48 17.52 2 11.93 2S2 6.48 2 12.07c0 5 3.66 9.14 8.44 9.93v-7.02H7.9v-2.91h2.54V9.41c0-2.5 1.49-3.88 3.77-3.88 1.09 0 2.24.2 2.24.2v2.46h-1.26c-1.24 0-1.63.77-1.63 1.56v1.87h2.78l-.44 2.91h-2.34V22c4.78-.79 8.44-4.93 8.44-9.93Z\">\u003C\u002Fpath>\n      \u003C\u002Fsvg>\n      \u003Ca href=\"https:\u002F\u002Fwww.facebook.com\u002Fsuperdev.academy.th\" target=\"_blank\" rel=\"noopener\" title=\"Follow Superdev Academy on Facebook\">Facebook: Superdev Academy\u003C\u002Fa>\n    \u003C\u002Fli>\n\n    \u003Cli style=\"display:flex; align-items:center; gap:6px; margin:0;\">\n      \n      \u003Csvg width=\"16\" height=\"16\" viewBox=\"0 0 24 24\" fill=\"#FF0000\" aria-hidden=\"true\">\n        \u003Cpath d=\"M23.5 6.2a3 3 0 0 0-2.1-2.1C19.5 3.5 12 3.5 12 3.5s-7.5 0-9.4.6A3 3 0 0 0 .5 6.2 31.5 31.5 0 0 0 0 12a31.5 31.5 0 0 0 .5 5.8 3 3 0 0 0 2.1 2.1c1.9.6 9.4.6 9.4.6s7.5 0 9.4-.6a3 3 0 0 0 2.1-2.1A31.5 31.5 0 0 0 24 12a31.5 31.5 0 0 0-.5-5.8ZM9.75 15.02V8.98L15.5 12l-5.75 3.02Z\">\u003C\u002Fpath>\n      \u003C\u002Fsvg>\n      \u003Ca href=\"https:\u002F\u002Fwww.youtube.com\u002F@SuperdevAcademy\" target=\"_blank\" rel=\"noopener\" title=\"Watch on YouTube\">YouTube: Superdev Academy\u003C\u002Fa>\n    \u003C\u002Fli>\n\n    \u003Cli style=\"display:flex; align-items:center; gap:6px; margin:0;\">\n      \n      \u003Csvg width=\"16\" height=\"16\" viewBox=\"0 0 24 24\" fill=\"#E4405F\" aria-hidden=\"true\">\n        \u003Cpath d=\"M7 2h10a5 5 0 0 1 5 5v10a5 5 0 0 1-5 5H7a5 5 0 0 1-5-5V7a5 5 0 0 1 5-5Zm10 2H7a3 3 0 0 0-3 3v10a3 3 0 0 0 3 3h10a3 3 0 0 0 3-3V7a3 3 0 0 0-3-3Zm-5 3.5A5.5 5.5 0 1 1 6.5 13 5.5 5.5 0 0 1 12 7.5Zm0 2A3.5 3.5 0 1 0 15.5 13 3.5 3.5 0 0 0 12 9.5Zm5.75-2.75a1.25 1.25 0 1 1-1.25 1.25 1.25 1.25 0 0 1 1.25-1.25Z\">\u003C\u002Fpath>\n      \u003C\u002Fsvg>\n      \u003Ca href=\"https:\u002F\u002Fwww.instagram.com\u002Fsuperdevacademy\u002F\" target=\"_blank\" rel=\"noopener\" title=\"See behind-the-scenes on Instagram\">Instagram: Superdev Academy\u003C\u002Fa>\n    \u003C\u002Fli>\n\n    \u003Cli style=\"display:flex; align-items:center; gap:6px; margin:0;\">\n      \n      \u003Csvg width=\"16\" height=\"16\" viewBox=\"0 0 24 24\" fill=\"#000000\" aria-hidden=\"true\">\n        \u003Cpath d=\"M21 8.12a6.86 6.86 0 0 1-4.8-2V16a6 6 0 1 1-6-6 5.9 5.9 0 0 1 1.63.23V8.05a9.08 9.08 0 0 1-1.63-.15V4.5a6.86 6.86 0 0 0 4.8 2.05V6.5a6.86 6.86 0 0 0 4.8 1.62ZM9.2 12.5A3.5 3.5 0 1 0 12.7 16V9.94a6 6 0 0 1-1.63-.27v3.95a3.5 3.5 0 0 1-1.87 3.17 3.5 3.5 0 0 1-4.78-3.23 3.5 3.5 0 0 1 4.78-3.06Z\">\u003C\u002Fpath>\n      \u003C\u002Fsvg>\n      \u003Ca href=\"https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy\" target=\"_blank\" rel=\"noopener\" title=\"Watch short tips on TikTok\">TikTok: @superdevacademy\u003C\u002Fa>\n    \u003C\u002Fli>\n  \u003C\u002Ful>\n\u003C\u002Fdiv>\u003Cp>&nbsp;\u003C\u002Fp>","cover_image_managing_multi_device_multi_session_in_web_socket_systems_kp81pkol6c.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fvtvj7f7xktls88l\u002Fl\u002Fcover_image_managing_multi_device_multi_session_in_web_socket_systems_kp81pkol6c.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fvtvj7f7xktls88l\u002Fm\u002Fcover_image_managing_multi_device_multi_session_in_web_socket_systems_kp81pkol6c.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fvtvj7f7xktls88l\u002Fcover_image_managing_multi_device_multi_session_in_web_socket_systems_kp81pkol6c.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fvtvj7f7xktls88l\u002Fs\u002Fcover_image_managing_multi_device_multi_session_in_web_socket_systems_kp81pkol6c.webp","2026-03-04 08:45:13.010Z","76qprkevbgfdps8",{"keywords":18,"locale":51,"school_blog":61},[19,26,31,36,41,46],{"collectionId":20,"collectionName":21,"created":22,"created_by":16,"id":23,"name":24,"updated":25,"updated_by":16},"sclkey987654321","school_keywords","2026-03-04 08:45:10.982Z","dqqa1njvmvzgknq","Real-time Application","2026-06-07 06:46:45.244Z",{"collectionId":20,"collectionName":21,"created":27,"created_by":16,"id":28,"name":29,"updated":30,"updated_by":16},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-06-07 06:45:07.798Z",{"collectionId":20,"collectionName":21,"created":32,"created_by":16,"id":33,"name":34,"updated":35,"updated_by":16},"2026-03-04 08:45:11.216Z","4pcpc8j9lxwki40","Multi-session","2026-06-07 06:46:45.378Z",{"collectionId":20,"collectionName":21,"created":37,"created_by":16,"id":38,"name":39,"updated":40,"updated_by":16},"2026-03-04 08:45:11.536Z","dam2zlrgu9l1qrd","Multi-device","2026-06-07 06:46:45.580Z",{"collectionId":20,"collectionName":21,"created":42,"created_by":16,"id":43,"name":44,"updated":45,"updated_by":16},"2026-03-04 08:44:48.724Z","s6xhnfomy7n5ycp","WebSocket Server","2026-06-07 06:46:39.070Z",{"collectionId":20,"collectionName":21,"created":47,"created_by":16,"id":48,"name":49,"updated":50,"updated_by":16},"2026-03-04 08:34:00.920Z","ecac9y661or1xka","WebSocket","2026-06-07 06:45:55.734Z",{"code":52,"collectionId":53,"collectionName":54,"created":55,"flag":56,"id":57,"is_default":58,"label":59,"updated":60},"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":62,"collectionId":63,"collectionName":64,"created":65,"expand":66,"id":81,"slug":82,"updated":83,"views":84},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs","",{"category":67},{"blogIds":68,"collectionId":69,"collectionName":70,"created":71,"created_by":16,"id":62,"image":72,"image_alt":65,"image_path":73,"image_s_url":74,"label":75,"name":76,"priority":77,"publish_at":78,"scheduled_at":65,"status":79,"updated":80,"updated_by":16},[],"sclcatblg987654321","school_category_blogs","2026-03-04 08:33:53.210Z","59ty92ns80w_15oc1implw_bj2o4tjgh4.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fwqxt7ag2gn7xcmk\u002F59ty92ns80w_15oc1implw_bj2o4tjgh4.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fwqxt7ag2gn7xcmk\u002Fs\u002F59ty92ns80w_15oc1implw_bj2o4tjgh4.webp",{"en":76,"th":76},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-08-18 14:48:36.739Z","wzoz7l3fkolypwa","ep114-multi-device-multi-session-websocket","2026-10-23 04:13:40.601Z",284,"vtvj7f7xktls88l",[23,28,33,38,43,48],"2025-12-08 02:14:19.907Z","Learn how to design a WebSocket system that supports multiple devices per user with stable sessions, real-time synchronization, secure token management, and production-ready patterns complete with Go code examples.","2026-10-14 03:48:07.715Z","ebkf8ra4nd55ual",[],{"th":82,"en":82}]