[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-grpc-api-websocket-chat-all--*":3,"academy-blog-translations-yhcouzcawidh41e":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.69 Using gRPC for API Development in WebSocket Chat","sclblg987654321","school_blog_translations","\u003Cp>In EP.69, we will explore how to use gRPC to develop API for WebSocket Chat to enhance communication between WebSocket Server and Client with more efficiency. gRPC is a framework developed by Google that uses Protocol Buffers (protobuf) for data serialization, making the communication process faster and more efficient compared to traditional REST APIs.\u003C\u002Fp>\u003Cp>gRPC helps in enabling faster real-time communication, which is essential for applications like WebSocket Chat, where you need continuous and low-latency communication for message exchange. We will learn how to set up gRPC and integrate it with WebSocket Chat for optimized communication.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>Why Use gRPC for WebSocket Chat?\u003C\u002Fh2>\u003Cp>gRPC offers several benefits that can improve the performance and scalability of your WebSocket Chat application:\u003C\u002Fp>\u003Col>\u003Cli>High Performance: gRPC uses Protocol Buffers, which is more compact and efficient than JSON, reducing the size of the data being transferred and improving speed.\u003C\u002Fli>\u003Cli>Real-time Communication: gRPC supports bidirectional streaming, making it ideal for real-time chat applications like WebSocket where messages are constantly sent and received.\u003C\u002Fli>\u003Cli>Low Latency: gRPC is designed for low latency and high throughput, which ensures faster communication and responsiveness.\u003C\u002Fli>\u003Cli>Cross-platform support: gRPC supports multiple languages, allowing you to develop both server and client in different programming languages.\u003C\u002Fli>\u003Cli>Security: gRPC comes with SSL\u002FTLS encryption by default, making communication secure.\u003C\u002Fli>\u003C\u002Fol>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>Setting Up gRPC for WebSocket Chat\u003C\u002Fh2>\u003Cp>In this section, we will set up gRPC and integrate it with WebSocket Chat to send and receive messages in real-time.\u003C\u002Fp>\u003Ch3>1. Creating the .proto File for gRPC API\u003C\u002Fh3>\u003Cp>To start, we will define the service and message types for communication in WebSocket Chat using a \u003Ccode inline=\"\">.proto\u003C\u002Fcode> file. This will allow us to define the structure for sending and receiving messages.\u003C\u002Fp>\u003Cp>chat.proto:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext language-proto\">syntax = \"proto3\";\n\npackage chat;\n\nservice ChatService {\n    rpc SendMessage (MessageRequest) returns (MessageResponse);\n    rpc ReceiveMessages (MessageRequest) returns (stream MessageResponse);\n}\n\nmessage MessageRequest {\n    string user = 1;\n    string message = 2;\n}\n\nmessage MessageResponse {\n    string user = 1;\n    string message = 2;\n    string timestamp = 3;\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch3>2. Generating Go Code from the .proto File\u003C\u002Fh3>\u003Cp>Once the .proto file is defined, we can generate Go code using the protoc compiler.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext language-bash\">protoc --go_out=plugins=grpc:. chat.proto\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>Now that we have generated the necessary code, let’s create the gRPC Server for handling message sending and receiving.\u003C\u002Fp>\u003Ch3>3. Creating the gRPC Server for WebSocket Chat\u003C\u002Fh3>\u003Cp>Here is the Go code for creating the gRPC Server to handle the SendMessage and ReceiveMessages API requests:\u003C\u002Fp>\u003Cp>server.go:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport (\n    \"fmt\"\n    \"log\"\n    \"net\"\n    \"time\"\n    \"google.golang.org\u002Fgrpc\"\n    pb \"path\u002Fto\u002Fgenerated\u002Fproto\"\n    \"context\"\n)\n\ntype server struct{}\n\nfunc (s *server) SendMessage(ctx context.Context, in *pb.MessageRequest) (*pb.MessageResponse, error) {\n    return &amp;pb.MessageResponse{\n        User:     in.User,\n        Message:  in.Message,\n        Timestamp: fmt.Sprintf(\"%v\", time.Now()),\n    }, nil\n}\n\nfunc (s *server) ReceiveMessages(in *pb.MessageRequest, stream pb.ChatService_ReceiveMessagesServer) error {\n    for {\n        time.Sleep(1 * time.Second)\n        err := stream.Send(&amp;pb.MessageResponse{\n            User:    \"Server\",\n            Message: \"Hello, \" + in.User,\n            Timestamp: fmt.Sprintf(\"%v\", time.Now()),\n        })\n        if err != nil {\n            return err\n        }\n    }\n}\n\nfunc main() {\n    lis, err := net.Listen(\"tcp\", \":50051\")\n    if err != nil {\n        log.Fatalf(\"failed to listen: %v\", err)\n    }\n    s := grpc.NewServer()\n    pb.RegisterChatServiceServer(s, &amp;server{})\n    if err := s.Serve(lis); err != nil {\n        log.Fatalf(\"failed to serve: %v\", err)\n    }\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch3>4. Creating the WebSocket Client for gRPC\u003C\u002Fh3>\u003Cp>Now that we have the gRPC Server set up, let’s create a WebSocket Client that will communicate with the gRPC Server to send and receive messages.\u003C\u002Fp>\u003Cp>client.go:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport (\n    \"fmt\"\n    \"log\"\n    \"google.golang.org\u002Fgrpc\"\n    pb \"path\u002Fto\u002Fgenerated\u002Fproto\"\n    \"context\"\n)\n\nfunc main() {\n    conn, err := grpc.Dial(\":50051\", grpc.WithInsecure())\n    if err != nil {\n        log.Fatalf(\"Did not connect: %v\", err)\n    }\n    defer conn.Close()\n\n    c := pb.NewChatServiceClient(conn)\n\n    \u002F\u002F SendMessage Example\n    resp, err := c.SendMessage(context.Background(), &amp;pb.MessageRequest{User: \"John\", Message: \"Hello!\"})\n    if err != nil {\n        log.Fatalf(\"Could not send message: %v\", err)\n    }\n    fmt.Println(\"Message Response:\", resp.Message)\n\n    \u002F\u002F ReceiveMessages Example\n    stream, err := c.ReceiveMessages(context.Background(), &amp;pb.MessageRequest{User: \"John\"})\n    if err != nil {\n        log.Fatalf(\"Could not receive messages: %v\", err)\n    }\n\n    for {\n        msg, err := stream.Recv()\n        if err != nil {\n            log.Fatalf(\"Error receiving message: %v\", err)\n        }\n        fmt.Println(\"Received Message:\", msg.Message)\n    }\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>Testing the gRPC API\u003C\u002Fh2>\u003Cp>Once you’ve developed the gRPC API, it’s important to test that everything works correctly.\u003C\u002Fp>\u003Ch3>Tests to perform:\u003C\u002Fh3>\u003Col>\u003Cli>Test sending messages:\u003Cbr>Ensure that the Client can send a message to the Server and receive the correct response.\u003C\u002Fli>\u003Cli>Test receiving messages:\u003Cbr>Verify that the Server sends messages to the Client in real-time.\u003C\u002Fli>\u003Cli>Test performance:\u003Cbr>Measure the latency and throughput of using gRPC for communication and compare it to traditional REST APIs.\u003C\u002Fli>\u003C\u002Fol>\u003Cp>&nbsp;\u003C\u002Fp>\u003Chr>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>Challenge!\u003C\u002Fh3>\u003Cp>Try implementing \u003Cstrong>Authentication\u003C\u002Fstrong> using \u003Cstrong>JWT (JSON Web Tokens)\u003C\u002Fstrong> in your gRPC communication to make it more secure!\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cstrong>Next EP:\u003C\u002Fstrong>\u003Cbr>In \u003Cstrong data-start=\"22\" data-end=\"31\">EP.70\u003C\u002Fstrong>, we will explore \u003Cstrong data-start=\"49\" data-end=\"118\">Improving the Performance of WebSocket Servers with Load Balancer\u003C\u002Fstrong> to efficiently distribute traffic across multiple servers and ensure high availability and scalability for real-time WebSocket communication!\u003C\u002Fp>","110_11zon_bhmdfpumwt.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fljoiaqb0e2r60i5\u002F110_11zon_bhmdfpumwt.webp","2026-03-04 08:48:06.570Z","",{"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:48:01.927Z","vcknw4qrrifzlfg","WebSocket scalability","2026-04-10 16:13:39.152Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:48:05.350Z","zusuffvqm0gmvif","API development with gRPC","2026-04-10 16:13:39.966Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:48:05.655Z","jbilbz5mzbvhi7v","WebSocket performance","2026-04-10 16:13:40.132Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:48:05.933Z","u3p6qwqfjlbecdp","gRPC WebSocket","2026-04-10 16:13:40.266Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:48:01.322Z","qwms9vhmllldxj2","WebSocket Load Balancing","2026-04-10 16:13:38.951Z",{"code":44,"collectionId":45,"collectionName":46,"created":47,"flag":48,"id":49,"is_default":50,"label":51,"updated":52},"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":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","yhcouzcawidh41e",206,"ljoiaqb0e2r60i5",[20,25,30,35,40],"2025-07-28 10:12:34.491Z","Learn how to use gRPC to develop an API for WebSocket Chat to improve real-time communication between the server and client, making your chat system more efficient and faster.","grpc-api-websocket-chat","2026-04-22 07:10:08.189Z",{"en":77}]