0Pricing
gRPC & High Performance APIs · درس

دمج gRPC-Web

تعلّم كيفية تمكين اتصال gRPC مباشرة من متصفحات الويب باستخدام وكيل gRPC-Web

دمج gRPC-Web درس مجاني في gRPC & High Performance APIs على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في gRPC & High Performance APIs، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة gRPC & High Performance APIs 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Bridging Browsers to gRPC

Welcome to gRPC-Web Integration!

You've learned about gRPC's power for high-performance communication. But what if you want your web browser applications to talk to gRPC services directly? That's where gRPC-Web comes in.

This lesson explores how gRPC-Web enables browser-based clients to interact with gRPC backends.

Why Browsers Need gRPC-Web

Modern browsers don't fully expose the low-level HTTP/2 features that native gRPC relies on, especially for advanced streaming capabilities.

  • No full HTTP/2 control: Browsers abstract away direct HTTP/2 frame manipulation.
  • CORS restrictions: Cross-Origin Resource Sharing (CORS) policies can complicate direct gRPC connections.
  • Limited header access: Some gRPC-specific headers might not be accessible or modifiable.

These limitations prevent a standard gRPC client from running directly in a browser.

Introducing the gRPC-Web Proxy

The core idea behind gRPC-Web is to use a proxy. This proxy sits between your browser client and your gRPC server.

  • The browser client sends requests in a gRPC-Web compatible format (often HTTP/1.1 or a restricted HTTP/2).
  • The gRPC-Web proxy translates these requests into native gRPC (full HTTP/2) and forwards them to your gRPC server.
  • It also translates the gRPC server's responses back into a format the browser client understands.

Think of it as a translator for your browser to speak gRPC.

Common Proxy Solutions

While you could write your own, several robust proxies are available:

  • Envoy Proxy: A popular, high-performance open-source edge and service proxy. It has built-in support for gRPC-Web.
  • grpcwebproxy: A dedicated proxy written in Go, specifically designed for gRPC-Web.

These tools handle the complex translation layer, allowing your gRPC server to remain a standard gRPC server.

The Communication Flow

Here's a simplified view of how a gRPC-Web call works:

  1. Browser Client: Makes an HTTP/1.1 (or restricted HTTP/2) request to the proxy.
  2. gRPC-Web Proxy: Receives the request, converts it to a standard HTTP/2 gRPC request.
  3. gRPC Server: Processes the native gRPC request and sends back a native gRPC response.
  4. gRPC-Web Proxy: Receives the gRPC response, converts it back to an HTTP/1.1 (or restricted HTTP/2) response.
  5. Browser Client: Receives and processes the converted response.

Protobuf for Web Clients

Just like with regular gRPC, you define your service and messages using Protocol Buffers (Protobuf).

To generate client-side code for browsers, you use a special Protobuf plugin, typically protoc-gen-grpc-web. This generates JavaScript or TypeScript code that works with browser environments.

This generated code knows how to format requests for the gRPC-Web proxy and parse its responses.

Example: Web Client Call

After generating your client stubs, making a gRPC-Web call from JavaScript/TypeScript looks familiar. Here's a conceptual snippet for a GreeterService:

(Note: This code is illustrative and not runnable without a full gRPC-Web setup including proto compilation, a running proxy, and server.)

import {HelloRequest} from './helloworld_pb';
import {GreeterClient} from './HelloworldServiceClientPb';

const client = new GreeterClient(
  'http://localhost:8080', // Proxy address
  null, null
);

const request = new HelloRequest();
request.setName('CoddyKit User');

client.sayHello(request, {}, (err, response) => {
  if (err) {
    console.error(err.code, err.message);
    return;
  }
  console.log(response.getMessage());
});

Server-Side Remains Standard

One of the great advantages of gRPC-Web is that your gRPC server doesn't need to know anything about it!

The server simply receives standard gRPC requests (HTTP/2) from the proxy and sends back standard gRPC responses. This means you don't need to change your existing gRPC service implementations to support browser clients.

This separation of concerns simplifies development and maintenance.

Streaming Limitations in gRPC-Web

While gRPC-Web is powerful, it has some limitations compared to native gRPC:

  • Unary RPCs: Fully supported (request-response).
  • Server Streaming: Generally supported (server sends multiple responses to one request).
  • Client Streaming: Not typically supported due to browser limitations on streaming requests.
  • Bidirectional Streaming: Not typically supported for the same reason.

For applications heavily relying on client or bidirectional streaming, native gRPC clients (e.g., in Node.js, mobile apps) are usually required.

Quick Check: gRPC-Web Purpose

Which of the following statements accurately describe the primary purpose or characteristics of gRPC-Web?

Recap: Bridging Browsers & gRPC

You've successfully learned about gRPC-Web!

  • It's a solution to enable web browser clients to interact with gRPC services.
  • It works by using a proxy (like Envoy or grpcwebproxy) to translate requests and responses.
  • Your gRPC server remains standard, unaware of the gRPC-Web layer.
  • While it supports unary and server streaming, client and bidirectional streaming are typically not supported.

This powerful tool bridges the gap, bringing the benefits of gRPC to your web applications!

الأسئلة الشائعة

هل درس «دمج gRPC-Web» مجاني؟

نعم — نص درس «دمج gRPC-Web» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة gRPC & High Performance APIs، انتقل إلى CoddyKit PRO. تتضمن دورة gRPC & High Performance APIs 4 دروس في المجموع.

ماذا ستتعلم في «دمج gRPC-Web»؟

تعلّم كيفية تمكين اتصال gRPC مباشرة من متصفحات الويب باستخدام وكيل gRPC-Web تتمرن على gRPC & High Performance APIs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ gRPC & High Performance APIs؟

لا تُشترط خبرة سابقة. gRPC & High Performance APIs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «دمج gRPC-Web»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس gRPC & High Performance APIs هذا؟

نعم. كل درس في gRPC & High Performance APIs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. دمج gRPC-Web
  2. gRPCurl وBloomRPC
  3. أنماط اكتشاف الخدمات
  4. انعكاس الخادم والعملاء الديناميكيون
← العودة إلى gRPC & High Performance APIs