[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-go-websocket-real-time-communication-all--*":3,"academy-blog-translations-28kxu8pedsqn8si":134},{"data":4,"page":122,"perPage":122,"totalItems":122,"totalPages":122},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":129,"keywords":130,"locale":104,"published_at":131,"scheduled_at":13,"school_blog":126,"short_description":132,"status":124,"title":6,"updated":133,"updated_by":13,"slug":127,"views":128},"Ep.22 Go and WebSocket: Modern Real-Time Communication!","sclblg987654321","school_blog_translations","\u003Cp class=\"p1\">\u003Cstrong>Go and WebSocket: Modern Real-Time Communication!\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p2\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>What is WebSocket?\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">WebSocket is a protocol that enables real-time communication between the server and the client without needing to send a new request each time, unlike HTTP.\u003Cbr>Advantages of WebSocket :\u003C\u002Fp>\u003Cul class=\"ul1\">\u003Cli class=\"li3\">Two-way Communication (Full Duplex) : Both the server and the client can send data to each other at any time.\u003C\u002Fli>\u003Cli class=\"li3\">Reduced Latency : Decreases the delay in data updates.\u003C\u002Fli>\u003Cli class=\"li3\">Ideal for Applications Requiring Live Data : Such as online games or chat systems.\u003C\u002Fli>\u003C\u002Ful>\u003Cp class=\"p2\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Using WebSocket in Go\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">In Go, we have a popular package, \u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"http:\u002F\u002Fgithub.com\u002Fgorilla\u002Fwebsocket\">github.com\u002Fgorilla\u002Fwebsocket\u003C\u002Fa> , which makes it easier to create WebSocket servers and clients.\u003C\u002Fp>\u003Cp class=\"p3\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Installation Steps:\u003C\u002Fstrong>\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">go get -u github.com\u002Fgorilla\u002Fwebsocket\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp class=\"p2\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Creating a Simple WebSocket Server\u003C\u002Fstrong> \u003Cstrong>Example Code\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">In this example, the Upgrader is used to convert an HTTP connection to a WebSocket.\u003Cbr>The handleConnections function is used to receive messages from clients and send them back.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"net\u002Fhttp\"\r\n\r\n    \"github.com\u002Fgorilla\u002Fwebsocket\"\r\n)\r\n\r\nvar upgrader = websocket.Upgrader{\r\n    CheckOrigin: func(r *http.Request) bool {\r\n        return true \u002F\u002F อนุญาตการเชื่อมต่อจากทุกที่\r\n    },\r\n}\r\n\r\nfunc handleConnections(w http.ResponseWriter, r *http.Request) {\r\n    conn, err := upgrader.Upgrade(w, r, nil) \u002F\u002F อัปเกรดการเชื่อมต่อเป็น WebSocket\r\n    if err != nil {\r\n        fmt.Println(\"Error upgrading connection:\", err)\r\n        return\r\n    }\r\n    defer conn.Close()\r\n\r\n    for {\r\n        messageType, msg, err := conn.ReadMessage()\r\n        if err != nil {\r\n            fmt.Println(\"Error reading message:\", err)\r\n            break\r\n        }\r\n        fmt.Printf(\"Received: %s\\n\", msg)\r\n\r\n        \u002F\u002F ส่งข้อความกลับไปยังไคลเอนต์\r\n        if err := conn.WriteMessage(messageType, msg); err != nil {\r\n            fmt.Println(\"Error writing message:\", err)\r\n            break\r\n        }\r\n    }\r\n}\r\n\r\nfunc main() {\r\n    http.HandleFunc(\"\u002Fws\", handleConnections)\r\n\r\n    fmt.Println(\"WebSocket server started at :8080\u002Fws\")\r\n    http.ListenAndServe(\":8080\", nil)\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Client Connection (HTML\u002FJavaScript)\u003C\u002Fstrong> \u003Cstrong>Example Client-Side Code\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">In this example, we use the JavaScript WebSocket API to connect to the WebSocket server. When the connection is successful, it sends the message \"Hello from client!\" to the server and waits for a response.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;title&gt;WebSocket Example&lt;\u002Ftitle&gt;\r\n&lt;\u002Fhead&gt;\r\n&lt;body&gt;\r\n    &lt;h1&gt;WebSocket Example&lt;\u002Fh1&gt;\r\n    &lt;script&gt;\r\n        const socket = new WebSocket(\"ws:\u002F\u002Flocalhost:8080\u002Fws\");\r\n\r\n        socket.onopen = () =&gt; {\r\n            console.log(\"Connected to server\");\r\n            socket.send(\"Hello from client!\");\r\n        };\r\n\r\n        socket.onmessage = (event) =&gt; {\r\n            console.log(\"Received from server:\", event.data);\r\n        };\r\n\r\n        socket.onerror = (error) =&gt; {\r\n            console.error(\"WebSocket error:\", error);\r\n        };\r\n    &lt;\u002Fscript&gt;\r\n&lt;\u002Fbody&gt;\r\n&lt;\u002Fhtml&gt;\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Handling Multiple Clients Simultaneously\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">A WebSocket server can support connections from multiple clients by storing the connections in a map or slice.\u003Cbr>Example of Managing Multiple Clients\u003C\u002Fp>\u003Cp class=\"p3\">In this example, clients stores all client connections. The broadcastMessage function is used to send messages to all clients.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">var clients = make(map[*websocket.Conn]bool)\r\n\r\nfunc broadcastMessage(message []byte) {\r\n    for client := range clients {\r\n        if err := client.WriteMessage(websocket.TextMessage, message); err != nil {\r\n            fmt.Println(\"Error broadcasting message:\", err)\r\n            client.Close()\r\n            delete(clients, client)\r\n        }\r\n    }\r\n}\r\n\r\nfunc handleConnections(w http.ResponseWriter, r *http.Request) {\r\n    conn, err := upgrader.Upgrade(w, r, nil)\r\n    if err != nil {\r\n        fmt.Println(\"Error upgrading connection:\", err)\r\n        return\r\n    }\r\n    defer conn.Close()\r\n\r\n    clients[conn] = true\r\n\r\n    for {\r\n        _, msg, err := conn.ReadMessage()\r\n        if err != nil {\r\n            fmt.Println(\"Error reading message:\", err)\r\n            delete(clients, conn)\r\n            break\r\n        }\r\n        broadcastMessage(msg)\r\n    }\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>In Summary\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul class=\"ul1\">\u003Cli class=\"li3\">WebSocket is suitable for real-time communication.\u003C\u002Fli>\u003Cli class=\"li3\">Use the gorilla\u002Fwebsocket package to create a WebSocket server.\u003C\u002Fli>\u003Cli class=\"li3\">Manage multiple clients by storing connections in a map and sending messages back to everyone.\u003C\u002Fli>\u003C\u002Ful>","14_11zon_42cpyl03j9.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Ff7lh405jf8kt5ip\u002F14_11zon_42cpyl03j9.webp","2026-03-04 08:34:21.641Z","",{"keywords":15,"locale":98,"school_blog":108},[16,23,28,33,38,43,48,53,58,63,68,73,78,83,88,93],{"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:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:20:14.253Z","ah6lvy4x8qe08l5","Golang","2026-04-10 16:07:26.172Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:33:59.315Z","btmgtfwmgpke1aa","Go language","2026-04-10 16:08:04.625Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:34:00.920Z","ecac9y661or1xka","WebSocket","2026-04-10 16:08:05.227Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:34:20.677Z","nc3jss8p56k1630","real-time communication","2026-04-10 16:08:12.487Z",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-03-04 08:34:21.070Z","3615sc755h8vrnk","gorilla\u002Fwebsocket","2026-04-10 16:08:12.574Z",{"collectionId":17,"collectionName":18,"created":49,"created_by":13,"id":50,"name":51,"updated":52,"updated_by":13},"2026-03-04 08:19:55.412Z","hz7yzm54i2o6cl7","web development","2026-04-10 16:07:24.402Z",{"collectionId":17,"collectionName":18,"created":54,"created_by":13,"id":55,"name":56,"updated":57,"updated_by":13},"2026-03-04 08:34:21.282Z","licmie4nk809see","online applications","2026-04-10 16:08:12.735Z",{"collectionId":17,"collectionName":18,"created":59,"created_by":13,"id":60,"name":61,"updated":62,"updated_by":13},"2026-03-04 08:32:15.843Z","m0x7wo77i8iycf1","Programming Education","2026-04-10 16:07:51.675Z",{"collectionId":17,"collectionName":18,"created":64,"created_by":13,"id":65,"name":66,"updated":67,"updated_by":13},"2026-03-04 08:32:51.346Z","tmzmy6jyz1n35rr","Go Programming","2026-04-10 16:08:01.434Z",{"collectionId":17,"collectionName":18,"created":69,"created_by":13,"id":70,"name":71,"updated":72,"updated_by":13},"2026-03-04 08:31:22.575Z","lfjse4xivbgg5wu","Practice programming","2026-04-10 16:07:39.541Z",{"collectionId":17,"collectionName":18,"created":74,"created_by":13,"id":75,"name":76,"updated":77,"updated_by":13},"2026-03-04 08:20:33.316Z","ln1ntwattzmxo0o","programming","2026-04-10 16:07:27.299Z",{"collectionId":17,"collectionName":18,"created":79,"created_by":13,"id":80,"name":81,"updated":82,"updated_by":13},"2026-03-04 08:32:26.073Z","vnvj1oaxje9m1q8","programming for beginners","2026-04-10 16:07:54.133Z",{"collectionId":17,"collectionName":18,"created":84,"created_by":13,"id":85,"name":86,"updated":87,"updated_by":13},"2026-03-04 08:31:49.362Z","2m9vv13etpn6zkx","programming language","2026-04-10 16:07:45.606Z",{"collectionId":17,"collectionName":18,"created":89,"created_by":13,"id":90,"name":91,"updated":92,"updated_by":13},"2026-03-04 08:31:54.955Z","264sfjffyhspetq","programmers","2026-04-10 16:07:47.221Z",{"collectionId":17,"collectionName":18,"created":94,"created_by":13,"id":95,"name":96,"updated":97,"updated_by":13},"2026-03-04 08:26:59.195Z","gab60xd583s3qaw","Superdev School","2026-04-10 16:07:37.087Z",{"code":99,"collectionId":100,"collectionName":101,"created":102,"flag":103,"id":104,"is_default":105,"label":106,"updated":107},"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":109,"collectionId":110,"collectionName":111,"created":13,"expand":112,"id":126,"slug":127,"updated":13,"views":128},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs",{"category":113},{"blogIds":114,"collectionId":115,"collectionName":116,"created":117,"created_by":13,"id":109,"image":118,"image_alt":13,"image_path":119,"label":120,"name":121,"priority":122,"publish_at":123,"scheduled_at":13,"status":124,"updated":125,"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":121,"th":121},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-04-25 02:32:15.470Z","28kxu8pedsqn8si","go-websocket-real-time-communication",213,"f7lh405jf8kt5ip",[20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95],"2025-01-27 04:39:39.240Z","Learn about WebSocket and its implementation in Go for efficient real-time communication.","2026-04-25 02:47:31.577Z",{"th":127,"en":127}]