[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-th-1-1-all-ep-103-latency-optimization-with-binary-protocol-and-protobuf-all--*":3,"academy-blog-translations-mzlxqrxjs2fo9nb":79},{"data":4,"page":67,"perPage":67,"totalItems":67,"totalPages":67},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":73,"keywords":74,"locale":49,"published_at":75,"scheduled_at":13,"school_blog":71,"short_description":76,"slug":77,"status":69,"title":6,"updated":78,"updated_by":13,"views":72},"EP.103 การปรับปรุง Latency ด้วย Binary Protocol และ Protobuf","sclblg987654321","school_blog_translations","\u003Cp>ในระบบ WebSocket ที่ต้องสื่อสารข้อมูลแบบ \u003Cstrong>เรียลไทม์ (Real-time)\u003C\u002Fstrong> ความเร็วในการรับ–ส่งข้อมูลมีความสำคัญอย่างมาก โดยเฉพาะเมื่อจำนวนผู้ใช้เพิ่มขึ้น หรือปริมาณข้อมูลมากขึ้น\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>การใช้ \u003Cstrong>Binary Protocol\u003C\u002Fstrong> และ \u003Cstrong>Protocol Buffers (Protobuf)\u003C\u002Fstrong> จะช่วยให้เราลดขนาดข้อมูล, ลด latency และเพิ่ม throughput ของ WebSocket Server ได้อย่างมหาศาล 🚀\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>1. ทำไมต้องใช้ Binary Protocol?\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>โดยทั่วไป WebSocket มักส่งข้อมูลแบบ \u003Cstrong>JSON\u003C\u002Fstrong> ซึ่งแม้อ่านง่าย แต่มีข้อเสีย:\u003C\u002Fp>\u003Cul>\u003Cli>ขนาดข้อมูลใหญ่\u003C\u002Fli>\u003Cli>ต้อง parse ทุกครั้ง\u003C\u002Fli>\u003Cli>เปลือง \u003Cstrong>CPU\u003C\u002Fstrong> และ \u003Cstrong>Bandwidth\u003C\u002Fstrong>\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cstrong>Binary Protocol\u003C\u002Fstrong> เข้ามาช่วยลด overhead เพราะ:\u003C\u002Fp>\u003Cp>✅ ใช้ขนาดข้อมูลน้อยกว่า JSON ถึง \u003Cstrong>30–70%\u003C\u002Fstrong>\u003Cbr>✅ แปลงข้อมูลเป็น binary stream ที่ \u003Cstrong>parse เร็วกว่า\u003C\u002Fstrong>\u003Cbr>✅ ลด \u003Cstrong>latency\u003C\u002Fstrong> ในการส่งข้อมูลระหว่าง server ↔ client\u003C\u002Fp>\u003Cfigure class=\"table\">\u003Ctable>\u003Cthead>\u003Ctr>\u003Cth>รูปแบบ\u003C\u002Fth>\u003Cth>ขนาดข้อมูล (bytes)\u003C\u002Fth>\u003Cth>Latency\u003C\u002Fth>\u003C\u002Ftr>\u003C\u002Fthead>\u003Ctbody>\u003Ctr>\u003Ctd>JSON\u003C\u002Ftd>\u003Ctd>~120 bytes\u003C\u002Ftd>\u003Ctd>15 ms\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Binary (Protobuf)\u003C\u002Ftd>\u003Ctd>~40 bytes\u003C\u002Ftd>\u003Ctd>5 ms\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003C\u002Ffigure>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>2. Protocol Buffers (Protobuf) คืออะไร?\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cstrong>Protobuf\u003C\u002Fstrong> เป็น format สำหรับการ serialize ข้อมูลที่มีประสิทธิภาพสูงของ Google\u003Cbr>เหมาะมากสำหรับระบบที่ต้องการความเร็ว เช่น WebSocket, gRPC หรือ IoT\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch4>ตัวอย่างการประกาศไฟล์ \u003Ccode inline=\"\">.proto\u003C\u002Fcode>:\u003C\u002Fh4>\u003Cpre>\u003Ccode class=\"language-plaintext language-protobuf\">syntax = \"proto3\";\n\nmessage ChatMessage {\n  string sender = 1;\n  string content = 2;\n  int64 timestamp = 3;\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>จากนั้น generate โค้ด Go ด้วยคำสั่ง:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext language-bash\">protoc --go_out=. --go_opt=paths=source_relative chat.proto\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>3. ตัวอย่างการใช้ Protobuf ใน WebSocket ด้วย Golang\u003C\u002Fh2>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport (\n\t\"fmt\"\n\t\"log\"\n\t\"net\u002Fhttp\"\n\n\t\"github.com\u002Fgorilla\u002Fwebsocket\"\n\t\"google.golang.org\u002Fprotobuf\u002Fproto\"\n)\n\nvar upgrader = websocket.Upgrader{\n\tCheckOrigin: func(r *http.Request) bool { return true },\n}\n\nfunc handleConnection(w http.ResponseWriter, r *http.Request) {\n\tconn, err := upgrader.Upgrade(w, r, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer conn.Close()\n\n\tfor {\n\t\t_, data, err := conn.ReadMessage()\n\t\tif err != nil {\n\t\t\tbreak\n\t\t}\n\n\t\tvar msg ChatMessage\n\t\tif err := proto.Unmarshal(data, &amp;msg); err == nil {\n\t\t\tfmt.Printf(\"[%s]: %s\\n\", msg.Sender, msg.Content)\n\t\t}\n\t}\n}\n\nfunc main() {\n\thttp.HandleFunc(\"\u002Fws\", handleConnection)\n\tfmt.Println(\"WebSocket Server running on :8080\")\n\thttp.ListenAndServe(\":8080\", nil)\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>✅ ฝั่ง Client ก็สามารถใช้ Protobuf ในการ encode\u002Fdecode ข้อมูลได้เช่นกัน\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>4. ผลลัพธ์ที่ได้จากการใช้ Binary Protocol + Protobuf\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cul>\u003Cli>🔹 ลด latency ได้มากกว่า \u003Cstrong>60%\u003C\u002Fstrong>\u003C\u002Fli>\u003Cli>🔹 ประหยัด bandwidth โดยเฉพาะบน mobile\u003C\u002Fli>\u003Cli>🔹 ขยายระบบได้ง่ายขึ้นโดยไม่ต้องเปลี่ยน schema\u003C\u002Fli>\u003Cli>🔹 ใช้ CPU น้อยลงในการ parse message\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>5. Best Practices\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cul>\u003Cli>💡 ใช้ \u003Cstrong>gzip หรือ Brotli compression\u003C\u002Fstrong> คู่กับ Binary\u003C\u002Fli>\u003Cli>💡 ออกแบบ schema ของ Protobuf ให้ \u003Cstrong>ยืดหยุ่น\u003C\u002Fstrong> เผื่อรองรับ version อนาคต\u003C\u002Fli>\u003Cli>💡 ใช้ \u003Cstrong>connection pooling\u003C\u002Fstrong> เพื่อรองรับผู้ใช้จำนวนมาก\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>🚀 ท้าให้ลอง!\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ลองเปลี่ยนจาก JSON → Protobuf แล้ววัด latency, throughput และ memory usage คุณจะเห็น performance พุ่งขึ้นอย่างชัดเจน ⚡️\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Chr>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>🔜 Next EP:\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cstrong>EP.104 – การใช้ WebSocket Compression และ Delta Updates\u003C\u002Fstrong>\u003Cbr>เจาะลึกเทคนิคการ \u003Cstrong>บีบอัดข้อมูลแบบเรียลไทม์\u003C\u002Fstrong> และการอัปเดตเฉพาะ “ส่วนที่เปลี่ยนแปลง” เพื่อทำให้ WebSocket Server ของคุณเร็วและเบายิ่งกว่าเดิม! 🔥\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cstrong>อ่านบทความ Series อื่นๆ\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul>\u003Cli>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FGolang\">\u003Cstrong>Golang The Series\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fli>\u003Cli>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FJS2GO\">\u003Cstrong>JS2GO\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fli>\u003Cli>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FTailwind%20CSS\">\u003Cstrong>10 Ep ที่จะให้คุณเป็นมือโปร Tailwind CSS ในชั่วข้ามคืน\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fli>\u003C\u002Ful>\u003Cp>\u003Cstrong>🔵 Facebook: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.facebook.com\u002Fsuperdev.academy.th\">\u003Cstrong>https:\u002F\u002Fwww.facebook.com\u002Fsuperdev.academy.th\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🔴 YouTube : \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.youtube.com\u002Fchannel\u002FUC2eI2RxcA2zbTqZyHyWIfRg\">\u003Cstrong>Superdev Academy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>📸 Instagram: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.instagram.com\u002Fsuperdevschool\u002F\">\u003Cstrong>Superdev Academy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🎬 TikTok: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\">\u003Cstrong>https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🌐 Website: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdevacademy.com\u002F\">\u003Cstrong>https:\u002F\u002Fwww.superdevacademy.com\u002F\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>","177_11zon_a3pr20tex9.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Ffmld0ap7ivkk3lf\u002F177_11zon_a3pr20tex9.webp","2026-03-04 08:45:54.126Z","",{"keywords":15,"locale":43,"school_blog":53},[16,23,28,33,38],{"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:44:49.054Z","vbjgbr5ah0kh43p","Real-time System","2026-04-10 16:12:50.311Z",{"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:45:53.123Z","oisa8tvym61ft7v","Latency Optimization","2026-04-10 16:13:05.538Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:45:52.242Z","fjcmfms1mw4u0hd","Binary Protocol","2026-04-10 16:13:05.403Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:34:00.920Z","ecac9y661or1xka","WebSocket","2026-04-10 16:08:05.227Z",{"code":44,"collectionId":45,"collectionName":46,"created":47,"flag":48,"id":49,"is_default":50,"label":51,"updated":52},"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":54,"collectionId":55,"collectionName":56,"expand":57,"id":71,"views":72},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs",{"category":58},{"blogIds":59,"collectionId":60,"collectionName":61,"created":62,"created_by":13,"id":54,"image":63,"image_alt":13,"image_path":64,"label":65,"name":66,"priority":67,"publish_at":68,"scheduled_at":13,"status":69,"updated":70,"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":66,"th":66},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-04-25 02:32:15.470Z","mzlxqrxjs2fo9nb",241,"fmld0ap7ivkk3lf",[20,25,30,35,40],"2025-10-27 02:19:50.115Z","เรียนรู้วิธีเพิ่มความเร็วของ WebSocket Server ด้วยการใช้ Binary Protocol และ Protocol Buffers (Protobuf) เพื่อให้ระบบรับ–ส่งข้อมูลได้เร็วขึ้น ลด latency และประหยัด bandwidth สำหรับระบบ real-time ขนาดใหญ่","ep-103-latency-optimization-with-binary-protocol-and-protobuf","2026-04-25 02:47:58.072Z",{"th":77}]