[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-th-1-1-all-websocket-chat-room-management-graphql-all--*":3,"academy-blog-translations-nhxtrdpa103lppi":104},{"data":4,"page":92,"perPage":92,"totalItems":92,"totalPages":92},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":99,"keywords":100,"locale":74,"published_at":101,"scheduled_at":13,"school_blog":96,"short_description":102,"status":94,"title":6,"updated":103,"updated_by":13,"slug":97,"views":98},"EP.37 การสร้างระบบจัดการห้องแชทใน WebSocket และ GraphQL","sclblg987654321","school_blog_translations","\u003Ch3 data-pm-slice=\"1 1 []\">\u003Cspan>\u003Cstrong>ทำไมต้องมีระบบห้องแชท?\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fh3>\u003Cp>\u003Cspan>ระบบห้องแชทช่วยให้ผู้ใช้สามารถแบ่งการสนทนาออกเป็นกลุ่มย่อยได้ โดยแต่ละห้องจะมีผู้ใช้ที่เกี่ยวข้องเท่านั้น ซึ่งเหมาะสำหรับ:\u003C\u002Fspan>\u003C\u002Fp>\u003Cul data-spread=\"false\">\u003Cli>\u003Cspan>\u003Cstrong>แชทกลุ่ม (Group Chat)\u003C\u002Fstrong> ที่สมาชิกสามารถเข้าร่วมหรือออกจากห้องได้\u003C\u002Fspan>\u003C\u002Fli>\u003Cli>\u003Cspan>\u003Cstrong>ห้องสนทนาเฉพาะเรื่อง (Topic-Based Chat Rooms)\u003C\u002Fstrong> เช่น ห้องสนทนาของทีมในบริษัท\u003C\u002Fspan>\u003C\u002Fli>\u003Cli>\u003Cspan>\u003Cstrong>ระบบสนับสนุนลูกค้า (Customer Support Chat)\u003C\u002Fstrong> ที่ตัวแทนสามารถเข้าห้องเพื่อตอบคำถามของลูกค้าได้\u003C\u002Fspan>\u003C\u002Fli>\u003C\u002Ful>\u003Ch3>\u003Cspan>\u003Cstrong>โครงสร้างของระบบจัดการห้องแชท\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fh3>\u003Col data-spread=\"false\">\u003Cli>\u003Cspan>\u003Cstrong>WebSocket Server\u003C\u002Fstrong> - จัดการการเชื่อมต่อของผู้ใช้และส่งข้อความไปยังห้องที่เกี่ยวข้อง\u003C\u002Fspan>\u003C\u002Fli>\u003Cli>\u003Cspan>\u003Cstrong>GraphQL API\u003C\u002Fstrong> - รองรับคำสั่งสร้างห้อง, เข้าร่วมห้อง และรับส่งข้อความ\u003C\u002Fspan>\u003C\u002Fli>\u003Cli>\u003Cspan>\u003Cstrong>Database (PostgreSQL \u002F MongoDB)\u003C\u002Fstrong> - ใช้เก็บข้อมูลห้องและข้อความ\u003C\u002Fspan>\u003C\u002Fli>\u003Cli>\u003Cspan>\u003Cstrong>GraphQL Subscription\u003C\u002Fstrong> - ส่งข้อความไปยังสมาชิกในห้องแบบเรียลไทม์\u003C\u002Fspan>\u003C\u002Fli>\u003C\u002Fol>\u003Ch3>\u003Cspan>\u003Cstrong>ติดตั้งไลบรารีที่จำเป็น\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-plaintext\">go get github.com\u002F99designs\u002Fgqlgen\ngo get github.com\u002Fgorilla\u002Fwebsocket\ngo get github.com\u002Fjmoiron\u002Fsqlx\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch3 data-pm-slice=\"1 1 []\">\u003Cspan>\u003Cstrong>การตั้งค่าฐานข้อมูลสำหรับระบบห้องแชท\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fh3>\u003Cp>\u003Cspan>ไฟล์ \u003C\u002Fspan>\u003Ccode>\u003Cspan>schema.sql\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">CREATE TABLE chat_rooms (\n    id SERIAL PRIMARY KEY,\n    name TEXT NOT NULL UNIQUE\n);\n\nCREATE TABLE messages (\n    id SERIAL PRIMARY KEY,\n    room_id INTEGER REFERENCES chat_rooms(id),\n    content TEXT NOT NULL,\n    sender TEXT NOT NULL,\n    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch3 data-pm-slice=\"1 1 []\">\u003Cspan>\u003Cstrong>การสร้าง GraphQL Schema สำหรับระบบห้องแชท\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fh3>\u003Cp>\u003Cspan>ไฟล์ \u003C\u002Fspan>\u003Ccode>\u003Cspan>schema.graphql\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">type Query {\n  rooms: [ChatRoom!]!\n  messages(roomID: ID!): [Message!]!\n}\n\ntype Mutation {\n  createRoom(name: String!): ChatRoom!\n  joinRoom(roomID: ID!): String!\n  sendMessage(roomID: ID!, content: String!, sender: String!): Message!\n}\n\ntype Subscription {\n  messageAdded(roomID: ID!): Message!\n}\n\ntype ChatRoom {\n  id: ID!\n  name: String!\n}\n\ntype Message {\n  id: ID!\n  roomID: ID!\n  content: String!\n  sender: String!\n  timestamp: String!\n}\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch3 data-pm-slice=\"1 1 []\">\u003Cspan>\u003Cstrong>การสร้าง Resolver สำหรับระบบห้องแชท\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fh3>\u003Cp>\u003Cspan>ไฟล์ \u003C\u002Fspan>\u003Ccode>\u003Cspan>resolver.go\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\n\nimport (\n    \"context\"\n    \"database\u002Fsql\"\n    \"fmt\"\n    \"sync\"\n    \"time\"\n\n    _ \"github.com\u002Flib\u002Fpq\"\n)\n\ntype ChatRoom struct {\n    ID   int    `json:\"id\"`\n    Name string `json:\"name\"`\n}\n\ntype Message struct {\n    ID        int       `json:\"id\"`\n    RoomID    int       `json:\"roomID\"`\n    Content   string    `json:\"content\"`\n    Sender    string    `json:\"sender\"`\n    Timestamp time.Time `json:\"timestamp\"`\n}\n\ntype Resolver struct {\n    mu          sync.Mutex\n    db          *sql.DB\n    subscribers map[int]map[string]chan Message\n}\n\nfunc NewResolver(db *sql.DB) *Resolver {\n    return &amp;Resolver{\n        db:          db,\n        subscribers: make(map[int]map[string]chan Message),\n    }\n}\n\nfunc (r *Resolver) Query_rooms(ctx context.Context) ([]ChatRoom, error) {\n    rows, err := r.db.Query(\"SELECT id, name FROM chat_rooms\")\n    if err != nil {\n        return nil, err\n    }\n    defer rows.Close()\n\n    var rooms []ChatRoom\n    for rows.Next() {\n        var room ChatRoom\n        if err := rows.Scan(&amp;room.ID, &amp;room.Name); err != nil {\n            return nil, err\n        }\n        rooms = append(rooms, room)\n    }\n    return rooms, nil\n}\n\nfunc (r *Resolver) Mutation_createRoom(ctx context.Context, name string) (ChatRoom, error) {\n    var room ChatRoom\n    err := r.db.QueryRow(\"INSERT INTO chat_rooms (name) VALUES ($1) RETURNING id, name\", name).\n        Scan(&amp;room.ID, &amp;room.Name)\n    return room, err\n}\n\nfunc (r *Resolver) Mutation_sendMessage(ctx context.Context, roomID int, content string, sender string) (Message, error) {\n    var message Message\n    err := r.db.QueryRow(\"INSERT INTO messages (room_id, content, sender) VALUES ($1, $2, $3) RETURNING id, room_id, content, sender, timestamp\", roomID, content, sender).\n        Scan(&amp;message.ID, &amp;message.RoomID, &amp;message.Content, &amp;message.Sender, &amp;message.Timestamp)\n    \n    r.mu.Lock()\n    for _, ch := range r.subscribers[roomID] {\n        ch &lt;- message\n    }\n    r.mu.Unlock()\n    return message, err\n}\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch3 data-pm-slice=\"1 1 []\">\u003Cspan>\u003Cstrong>ท้าให้ลอง!\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fh3>\u003Cp>\u003Cspan>ลองเพิ่ม \u003Cstrong>ระบบแอดมินห้องแชท\u003C\u002Fstrong> ที่สามารถกำหนดสิทธิ์ผู้ใช้ เช่น \u003Cstrong>ลบข้อความ\u003C\u002Fstrong> หรือ \u003Cstrong>จำกัดการเข้าถึง\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fp>\u003Chr>\u003Ch3>\u003Cspan>\u003Cstrong>EP ถัดไป\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fh3>\u003Cp>\u003Cspan>ใน \u003Cstrong>EP.38\u003C\u002Fstrong>, เราจะเพิ่ม \u003Cstrong>การแจ้งเตือนแบบ Push Notification\u003C\u002Fstrong> ให้กับ WebSocket Chat 🚀\u003C\u002Fspan>\u003C\u002Fp>","45_11zon_v9z41ez85v.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fnohcp3shvopcnym\u002F45_11zon_v9z41ez85v.webp","2026-03-04 08:51:15.455Z","",{"keywords":15,"locale":68,"school_blog":78},[16,23,28,33,38,43,48,53,58,63],{"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:51:13.245Z","i4i3s8cyj52mx4l","Group Chat","2026-04-10 16:14:33.995Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:51:13.649Z","wcctbc65iq5n4bj","GraphQL API","2026-04-10 16:14:34.086Z",{"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:51:13.905Z","sha4b5kwtz2qx0r","Chat Room Management","2026-04-10 16:14:34.254Z",{"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:51:14.205Z","laiwsvubam25wcf","GraphQL","2026-04-10 16:14:34.380Z",{"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",{"collectionId":17,"collectionName":18,"created":64,"created_by":13,"id":65,"name":66,"updated":67,"updated_by":13},"2026-03-04 08:51:14.568Z","rlxotivysdr3gjh","Chat Room","2026-04-10 16:14:34.527Z",{"code":69,"collectionId":70,"collectionName":71,"created":72,"flag":73,"id":74,"is_default":75,"label":76,"updated":77},"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":79,"collectionId":80,"collectionName":81,"created":13,"expand":82,"id":96,"slug":97,"updated":13,"views":98},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs",{"category":83},{"blogIds":84,"collectionId":85,"collectionName":86,"created":87,"created_by":13,"id":79,"image":88,"image_alt":13,"image_path":89,"label":90,"name":91,"priority":92,"publish_at":93,"scheduled_at":13,"status":94,"updated":95,"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":91,"th":91},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-04-25 02:32:15.470Z","nhxtrdpa103lppi","websocket-chat-room-management-graphql",227,"nohcp3shvopcnym",[20,25,30,35,40,45,50,55,60,65],"2025-03-18 02:45:17.368Z","เรียนรู้วิธีสร้าง ระบบห้องแชท (Chat Room Management System) โดยใช้ WebSocket และ GraphQL รองรับการสร้างห้องแชท การเข้าร่วมห้อง และการส่งข้อความระหว่างผู้ใช้แบบเรียลไทม์","2026-04-22 07:11:45.448Z",{"th":97,"en":97}]