[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-th-1-1-all-typing-indicator-websocket-chat-all--*":3,"academy-blog-translations-qa8pf1ntbtf5iya":100},{"data":4,"page":87,"perPage":87,"totalItems":87,"totalPages":87},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":95,"keywords":96,"locale":69,"published_at":97,"scheduled_at":13,"school_blog":91,"short_description":98,"status":89,"title":6,"updated":99,"updated_by":13,"slug":92,"views":94},"EP.41 การเพิ่มฟีเจอร์แสดงสถานะการพิมพ์ (Typing Indicator) ใน WebSocket Chat","sclblg987654321","school_blog_translations","\u003Ch3 data-pm-slice=\"1 1 []\">\u003Cspan>\u003Cstrong>ทำไมต้องมี Typing Indicator ใน WebSocket Chat?\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fh3>\u003Cp>\u003Cspan>\u003Cstrong>Typing Indicator\u003C\u002Fstrong> ช่วยให้ผู้ใช้สามารถรับรู้ได้ว่า \u003Cstrong>คู่สนทนากำลังพิมพ์อยู่\u003C\u002Fstrong> ทำให้การสื่อสารในแชทมีความลื่นไหลและตอบสนองต่อผู้ใช้งานได้ดีขึ้น เหมาะสำหรับ:\u003C\u002Fspan>\u003C\u002Fp>\u003Cul data-spread=\"false\">\u003Cli>\u003Cspan>\u003Cstrong>แอปพลิเคชันแชทส่วนตัวและกลุ่ม\u003C\u002Fstrong> เช่น WhatsApp, Messenger\u003C\u002Fspan>\u003C\u002Fli>\u003Cli>\u003Cspan>\u003Cstrong>ระบบสนับสนุนลูกค้า (Customer Support Chat)\u003C\u002Fstrong> ที่ตัวแทนสามารถแจ้งให้ลูกค้าทราบว่ากำลังตอบกลับ\u003C\u002Fspan>\u003C\u002Fli>\u003Cli>\u003Cspan>\u003Cstrong>แพลตฟอร์มชุมชนและโซเชียลมีเดีย\u003C\u002Fstrong> ที่ต้องการให้ประสบการณ์ใช้งานสมจริงขึ้น\u003C\u002Fspan>\u003C\u002Fli>\u003C\u002Ful>\u003Ch3>\u003Cspan>\u003Cstrong>โครงสร้างของระบบ Typing Indicator ใน WebSocket Chat\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fh3>\u003Col data-spread=\"false\">\u003Cli>\u003Cspan>\u003Cstrong>WebSocket Server\u003C\u002Fstrong> - รับและกระจายสถานะการพิมพ์ไปยังผู้ใช้ที่อยู่ในห้องแชทเดียวกัน\u003C\u002Fspan>\u003C\u002Fli>\u003Cli>\u003Cspan>\u003Cstrong>Frontend (Client-Side)\u003C\u002Fstrong> - ส่งและรับสถานะการพิมพ์ พร้อมแสดงผลบน UI\u003C\u002Fspan>\u003C\u002Fli>\u003Cli>\u003Cspan>\u003Cstrong>Database (Optional)\u003C\u002Fstrong> - สามารถเก็บบันทึกการพิมพ์ได้หากต้องการวิเคราะห์พฤติกรรมผู้ใช้\u003C\u002Fspan>\u003C\u002Fli>\u003C\u002Fol>\u003Ch3>\u003Cspan>\u003Cstrong>การเพิ่มฟีเจอร์ Typing Indicator ใน WebSocket Server\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fh3>\u003Ch4>\u003Cspan>\u003Cstrong>1. อัปเกรด WebSocket Server ให้รองรับ Typing Indicator\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fh4>\u003Cp>\u003Cspan>ไฟล์ \u003C\u002Fspan>\u003Ccode>\u003Cspan>websocket_server.go\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\n\nimport (\n    \"encoding\u002Fjson\"\n    \"fmt\"\n    \"net\u002Fhttp\"\n    \"sync\"\n\n    \"github.com\u002Fgorilla\u002Fwebsocket\"\n)\n\ntype TypingStatus struct {\n    RoomID  int    `json:\"roomID\"`\n    Sender  string `json:\"sender\"`\n    Typing  bool   `json:\"typing\"`\n}\n\nvar upgrader = websocket.Upgrader{\n    CheckOrigin: func(r *http.Request) bool { return true },\n}\n\nvar (\n    clients   = make(map[*websocket.Conn]bool)\n    broadcast = make(chan TypingStatus)\n    mu        sync.Mutex\n)\n\nfunc handleConnections(w http.ResponseWriter, r *http.Request) {\n    conn, _ := upgrader.Upgrade(w, r, nil)\n    defer conn.Close()\n    clients[conn] = true\n\n    for {\n        var status TypingStatus\n        err := conn.ReadJSON(&amp;status)\n        if err != nil {\n            delete(clients, conn)\n            break\n        }\n        broadcast &lt;- status\n    }\n}\n\nfunc handleMessages() {\n    for {\n        status := &lt;-broadcast\n        fmt.Printf(\"User %s is typing: %v\\n\", status.Sender, status.Typing)\n        for client := range clients {\n            err := client.WriteJSON(status)\n            if err != nil {\n                client.Close()\n                delete(clients, client)\n            }\n        }\n    }\n}\n\nfunc main() {\n    http.HandleFunc(\"\u002Fws\", handleConnections)\n    go handleMessages()\n    fmt.Println(\"WebSocket Server Running on Port 8080\")\n    http.ListenAndServe(\":8080\", nil)\n}\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch3 data-pm-slice=\"1 1 []\">\u003Cspan>\u003Cstrong>2. เพิ่มฟังก์ชัน Typing Indicator ใน Frontend (Client-Side)\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fh3>\u003Cp>\u003Cspan>ไฟล์ \u003C\u002Fspan>\u003Ccode>\u003Cspan>client.js\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">const socket = new WebSocket(\"ws:\u002F\u002Flocalhost:8080\u002Fws\");\nconst typingIndicator = document.getElementById(\"typing-indicator\");\nlet typingTimeout;\n\nsocket.onmessage = (event) =&gt; {\n    const data = JSON.parse(event.data);\n    if (data.typing) {\n        typingIndicator.innerText = `${data.sender} is typing...`;\n    } else {\n        typingIndicator.innerText = \"\";\n    }\n};\n\nfunction sendTypingStatus(isTyping) {\n    socket.send(JSON.stringify({ roomID: 1, sender: \"JohnDoe\", typing: isTyping }));\n}\n\ndocument.getElementById(\"message-input\").addEventListener(\"input\", () =&gt; {\n    sendTypingStatus(true);\n    clearTimeout(typingTimeout);\n    typingTimeout = setTimeout(() =&gt; sendTypingStatus(false), 3000);\n});\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch3 data-pm-slice=\"1 1 []\">\u003Cspan>\u003Cstrong>การแสดง Typing Indicator บน UI\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fh3>\u003Cp>\u003Cspan>ไฟล์ \u003C\u002Fspan>\u003Ccode>\u003Cspan>index.html\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">&lt;input type=\"text\" id=\"message-input\" placeholder=\"Type a message...\"&gt;\n&lt;p id=\"typing-indicator\"&gt;&lt;\u002Fp&gt;\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch3 data-pm-slice=\"1 3 []\">\u003Cspan>\u003Cstrong>3. การทดสอบระบบ\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fh3>\u003Col data-spread=\"false\">\u003Cli>\u003Cspan>\u003Cstrong>รัน WebSocket Server\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fli>\u003C\u002Fol>\u003Cpre>\u003Ccode class=\"language-plaintext\">go run websocket_server.go\u003C\u002Fcode>\u003C\u002Fpre>\u003Col data-spread=\"false\" data-pm-slice=\"3 3 []\" start=\"2\">\u003Cli>\u003Cspan>\u003Cstrong>เปิดหน้าเว็บหลายแท็บแล้วพิมพ์ข้อความ\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fli>\u003Cli>\u003Cspan>\u003Cstrong>ดูผลลัพธ์ที่ WebSocket Server และ UI\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fli>\u003C\u002Fol>\u003Ch3 data-pm-slice=\"1 1 []\">\u003Cspan>\u003Cstrong>ท้าให้ลอง!\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fh3>\u003Cp>\u003Cspan>ลองเพิ่ม \u003Cstrong>ระบบแจ้งเตือนเมื่อผู้ใช้หยุดพิมพ์ (Stop Typing Indicator)\u003C\u002Fstrong> โดยปรับปรุงโค้ดให้ตรวจจับเมื่อไม่มีการพิมพ์หลังจากระยะเวลาหนึ่ง\u003C\u002Fspan>\u003C\u002Fp>\u003Chr>\u003Ch3>\u003Cspan>\u003Cstrong>EP ถัดไป\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fh3>\u003Cp>\u003Cspan>ใน \u003Cstrong>EP.42\u003C\u002Fstrong>, เราจะเพิ่ม \u003Cstrong>ฟีเจอร์ Read Receipts (การแจ้งเตือนว่าอ่านแล้ว) ใน WebSocket Chat\u003C\u002Fstrong> 🚀\u003C\u002Fspan>\u003C\u002Fp>","53_11zon_sdo62p0vh7.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Ff0symmnhfpvutpu\u002F53_11zon_sdo62p0vh7.webp","2026-03-04 08:51:01.871Z","",{"keywords":15,"locale":63,"school_blog":73},[16,23,28,33,38,43,48,53,58],{"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:50:49.558Z","8lxakhujb04wz6u","Chat UX","2026-04-10 16:14:28.724Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:51:00.968Z","qivmit8ukkhb143","Typing Status","2026-04-10 16:14:31.248Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:48:07.088Z","brfbypclggbbkcx","WebSocket API","2026-04-10 16:13:40.594Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:46:14.782Z","v0mhensk18fofru","WebSocket Chat","2026-04-10 16:13:10.563Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:47:05.949Z","caufix9o52uw4bh","Real-Time Chat","2026-04-10 16:13:23.517Z",{"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:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"collectionId":17,"collectionName":18,"created":54,"created_by":13,"id":55,"name":56,"updated":57,"updated_by":13},"2026-03-04 08:34:00.920Z","ecac9y661or1xka","WebSocket","2026-04-10 16:08:05.227Z",{"collectionId":17,"collectionName":18,"created":59,"created_by":13,"id":60,"name":61,"updated":62,"updated_by":13},"2026-03-04 08:51:01.365Z","h5pcr9najjxfqwh","Typing Indicator","2026-04-10 16:14:31.411Z",{"code":64,"collectionId":65,"collectionName":66,"created":67,"flag":68,"id":69,"is_default":70,"label":71,"updated":72},"th","pbc_1989393366","locales","2026-01-22 10:59:55.832Z","twemoji:flag-thailand","s8wri3bt4vgg2ji",true,"Thai","2026-04-10 15:42:46.614Z",{"category":74,"collectionId":75,"collectionName":76,"created":13,"expand":77,"id":91,"slug":92,"updated":93,"views":94},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs",{"category":78},{"blogIds":79,"collectionId":80,"collectionName":81,"created":82,"created_by":13,"id":74,"image":83,"image_alt":13,"image_path":84,"label":85,"name":86,"priority":87,"publish_at":88,"scheduled_at":13,"status":89,"updated":90,"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":86,"th":86},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-04-25 02:32:15.470Z","qa8pf1ntbtf5iya","typing-indicator-websocket-chat","2026-05-11 23:02:55.244Z",292,"f0symmnhfpvutpu",[20,25,30,35,40,45,50,55,60],"2025-03-24 01:50:55.427Z","เรียนรู้วิธีเพิ่ม Typing Indicator (สถานะการพิมพ์) ใน WebSocket Chat เพื่อให้ผู้ใช้สามารถเห็นเมื่อคู่สนทนากำลังพิมพ์ รองรับการแจ้งเตือนแบบเรียลไทม์ผ่าน WebSocket","2026-05-06 08:38:38.415Z",{"th":92,"en":92}]