[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-th-1-1-all-reply-message-websocket-chat-all--*":3,"academy-blog-translations-tfjiw54rh3j333a":99},{"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":94,"keywords":95,"locale":69,"published_at":96,"scheduled_at":13,"school_blog":91,"short_description":97,"status":89,"title":6,"updated":98,"updated_by":13,"slug":92,"views":93},"EP.52 การเพิ่มฟีเจอร์การตอบกลับข้อความใน WebSocket Chat","sclblg987654321","school_blog_translations","\u003Cp>ในบทความนี้เราจะพาคุณไปเรียนรู้วิธีการเพิ่มฟีเจอร์การตอบกลับข้อความใน WebSocket Chat ที่จะช่วยให้ผู้ใช้สามารถตอบกลับข้อความใด ๆ ที่ส่งไปแล้ว โดยที่ข้อความตอบกลับจะแสดงผลลัพธ์ที่เชื่อมโยงกับข้อความต้นฉบับในห้องแชทแบบเรียลไทม์\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>ขั้นตอนการเพิ่มฟีเจอร์การตอบกลับข้อความใน WebSocket Chat:\u003C\u002Fh2>\u003Col>\u003Cli>\u003Ch3>ทำไมต้องมีฟีเจอร์การตอบกลับข้อความใน WebSocket Chat?\u003C\u002Fh3>\u003Cul>\u003Cli>การตอบกลับข้อความช่วยเพิ่มความสะดวกในการสื่อสารภายในห้องแชท และทำให้การสนทนามีความเป็นระเบียบมากยิ่งขึ้น\u003C\u002Fli>\u003Cli>ผู้ใช้สามารถตอบกลับข้อความเดิมเพื่อให้บริบทการสนทนาเป็นที่เข้าใจมากขึ้น\u003C\u002Fli>\u003C\u002Ful>\u003C\u002Fli>\u003Cli>\u003Ch3>การเพิ่มฟีเจอร์การตอบกลับใน WebSocket Server\u003C\u002Fh3>\u003Cul>\u003Cli>เราจะเพิ่มโค้ดใน WebSocket Server เพื่อรองรับการส่งข้อความที่มีการตอบกลับจากผู้ใช้\u003C\u002Fli>\u003C\u002Ful>\u003C\u002Fli>\u003Cli>\u003Ch3>การเพิ่ม UI ปุ่มตอบกลับข้อความใน Frontend\u003C\u002Fh3>\u003Cul>\u003Cli>เราจะเพิ่มปุ่ม \"Reply\" ที่จะให้ผู้ใช้สามารถตอบกลับข้อความใด ๆ ได้ โดยที่ข้อความที่ตอบกลับจะแสดงว่าเป็นข้อความตอบจากผู้ใช้คนไหน\u003C\u002Fli>\u003C\u002Ful>\u003C\u002Fli>\u003Cli>\u003Ch3>การทดสอบฟีเจอร์การตอบกลับข้อความ\u003C\u002Fh3>\u003Cul>\u003Cli>เราจะทดสอบฟีเจอร์นี้โดยการส่งข้อความในห้องแชท และคลิกปุ่มตอบกลับเพื่อดูผลลัพธ์ใน UI\u003C\u002Fli>\u003C\u002Ful>\u003C\u002Fli>\u003C\u002Fol>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>ตัวอย่างโค้ดสำหรับการตอบกลับข้อความใน WebSocket Chat\u003C\u002Fh2>\u003Col>\u003Cli>\u003Ch3>การอัปเดตฐานข้อมูล (Database)\u003C\u002Fh3>\u003C\u002Fli>\u003C\u002Fol>\u003Cp>อัปเดตฐานข้อมูลเพื่อรองรับการตอบกลับข้อความ โดยการเพิ่มคอลัมน์ \u003Ccode inline=\"\">reply_to_message_id\u003C\u002Fcode> เพื่อเชื่อมโยงข้อความที่ตอบกลับกับข้อความต้นฉบับ\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext language-sql\">ALTER TABLE chat_messages ADD COLUMN reply_to_message_id INT REFERENCES chat_messages(id);\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Col start=\"2\">\u003Cli>\u003Ch3>โค้ดใน WebSocket Server (Backend)\u003C\u002Fh3>\u003C\u002Fli>\u003C\u002Fol>\u003Cp>เพิ่มโค้ดใน WebSocket Server เพื่อรองรับการตอบกลับข้อความจากผู้ใช้ และแจ้งให้ลูกค้าทุกคนในห้องแชทรับรู้\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport (\n    \"database\u002Fsql\"\n    \"encoding\u002Fjson\"\n    \"fmt\"\n    \"net\u002Fhttp\"\n    \"sync\"\n\n    \"github.com\u002Fgorilla\u002Fwebsocket\"\n    _ \"github.com\u002Flib\u002Fpq\"\n)\n\ntype ReplyRequest struct {\n    MessageID   int    `json:\"messageID\"`\n    Sender      string `json:\"sender\"`\n    NewContent  string `json:\"newContent\"`\n    ReplyToID   int    `json:\"replyToID\"`\n}\n\ntype ReplyResponse struct {\n    MessageID   int    `json:\"messageID\"`\n    NewContent  string `json:\"newContent\"`\n    Sender      string `json:\"sender\"`\n    ReplyToID   int    `json:\"replyToID\"`\n}\n\nvar (\n    clients   = make(map[*websocket.Conn]bool)\n    broadcast = make(chan ReplyResponse)\n    mu        sync.Mutex\n    db        *sql.DB\n)\n\nfunc handleReplyMessage(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 request ReplyRequest\n        err := conn.ReadJSON(&amp;request)\n        if err != nil {\n            delete(clients, conn)\n            break\n        }\n\n        \u002F\u002F เพิ่มข้อความตอบกลับลงฐานข้อมูล\n        _, err = db.Exec(\"INSERT INTO chat_messages (content, sender, reply_to_message_id) VALUES ($1, $2, $3)\", request.NewContent, request.Sender, request.ReplyToID)\n        if err == nil {\n            broadcast &lt;- ReplyResponse{MessageID: request.MessageID, NewContent: request.NewContent, Sender: request.Sender, ReplyToID: request.ReplyToID}\n        }\n    }\n}\n\nfunc notifyClients() {\n    for {\n        msg := &lt;-broadcast\n        for client := range clients {\n            err := client.WriteJSON(msg)\n            if err != nil {\n                client.Close()\n                delete(clients, client)\n            }\n        }\n    }\n}\n\nfunc main() {\n    http.HandleFunc(\"\u002Fws\", handleReplyMessage)\n    go notifyClients()\n    fmt.Println(\"WebSocket Server Running on Port 8080\")\n    http.ListenAndServe(\":8080\", nil)\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Col start=\"3\">\u003Cli>\u003Ch3>โค้ดใน Frontend (Client)\u003C\u002Fh3>\u003C\u002Fli>\u003C\u002Fol>\u003Cp>เพิ่มปุ่ม \"Reply\" เพื่อให้ผู้ใช้สามารถตอบกลับข้อความได้\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-javascript\">const socket = new WebSocket(\"ws:\u002F\u002Flocalhost:8080\u002Fws\");\nconst chatContainer = document.getElementById(\"chat-container\");\n\nsocket.onmessage = (event) =&gt; {\n    const data = JSON.parse(event.data);\n    const messageElement = document.getElementById(`msg-${data.messageID}`);\n    if (data.replyToID) {\n        messageElement.innerHTML = `${data.sender} replied: ${data.newContent}`;\n    }\n};\n\nfunction sendReplyRequest(messageID, newContent, replyToID) {\n    socket.send(JSON.stringify({ messageID, sender: \"JohnDoe\", newContent, replyToID }));\n}\n\nfunction displayMessage(id, sender, content) {\n    const msgElement = document.createElement(\"p\");\n    msgElement.id = `msg-${id}`;\n    msgElement.innerText = `${sender}: ${content}`;\n    \n    const replyButton = document.createElement(\"button\");\n    replyButton.innerText = \"Reply\";\n    replyButton.onclick = () =&gt; {\n        const newContent = prompt(\"Reply to this message:\", content);\n        if (newContent) sendReplyRequest(id, newContent, id);\n    };\n    \n    msgElement.appendChild(replyButton);\n    chatContainer.appendChild(msgElement);\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Chr>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>ท้าให้ลอง!\u003C\u002Fh3>\u003Cp>ลองเพิ่มฟีเจอร์ \u003Cstrong>การตอบกลับหลายข้อความพร้อมกัน\u003C\u002Fstrong> (Multiple Replies) เพื่อให้ผู้ใช้สามารถตอบกลับหลายข้อความในห้องแชทได้ในครั้งเดียว!\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cstrong>EP ถัดไป:\u003C\u002Fstrong>\u003Cbr>ใน EP ถัดไป เราจะมาดู \u003Cstrong>การเพิ่มฟีเจอร์การติดตามข้อความที่ถูกตอบกลับ\u003C\u002Fstrong> (Follow-up on Replies) เพื่อให้ผู้ใช้สามารถติดตามคำตอบของข้อความที่ตอบกลับได้อย่างสะดวกมากยิ่งขึ้น!\u003C\u002Fp>","75_11zon_gtcyqgml7f.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002F05fgono4o0lbbe1\u002F75_11zon_gtcyqgml7f.webp","2026-03-04 08:48:52.423Z","",{"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:48:46.903Z","wqd5lairiftowzr","Frontend","2026-04-10 16:13:54.137Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:44:53.062Z","puutdnxuitnxxgq","Backend","2026-04-10 16:12:51.264Z",{"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:48:41.296Z","yp5nm5cxdv83ffw","โปรแกรมมิ่ง","2026-04-10 16:13:52.263Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:48:37.396Z","7rautg2afwgj00c","ฟีเจอร์ WebSocket","2026-04-10 16:13:51.129Z",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-03-04 08:48:37.086Z","lyvc6m6wy8i75nq","การพัฒนาแอปพลิเคชันแชท","2026-04-10 16:13:51.062Z",{"collectionId":17,"collectionName":18,"created":49,"created_by":13,"id":50,"name":51,"updated":52,"updated_by":13},"2026-03-04 08:48:51.155Z","y0u2j9otd5ec4nx","การตอบกลับข้อความ","2026-04-10 16:13:54.944Z",{"collectionId":17,"collectionName":18,"created":54,"created_by":13,"id":55,"name":56,"updated":57,"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":59,"created_by":13,"id":60,"name":61,"updated":62,"updated_by":13},"2026-03-04 08:34:00.920Z","ecac9y661or1xka","WebSocket","2026-04-10 16:08:05.227Z",{"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":13,"views":93},"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","tfjiw54rh3j333a","reply-message-websocket-chat",227,"05fgono4o0lbbe1",[20,25,30,35,40,45,50,55,60],"2025-06-18 04:47:04.618Z","เรียนรู้วิธีเพิ่มฟีเจอร์การตอบกลับข้อความ (Reply to Message) ใน WebSocket Chat ที่ช่วยให้ผู้ใช้สามารถตอบกลับข้อความใด ๆ ที่ส่งไปแล้ว โดยการแสดงข้อความตอบกลับที่เชื่อมโยงกับข้อความต้นฉบับในห้องแชท","2026-04-22 07:10:16.264Z",{"th":92,"en":92}]