Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Grpc Gcp Laravel Package

google/grpc-gcp

GCP-specific extensions for gRPC, providing components and tooling to enhance gRPC clients when accessing Google Cloud APIs. Includes source for extensions plus infrastructure for end-to-end tests and benchmarks for cloud API access.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps to First Use

  1. Install Dependencies:

    # On Ubuntu/Debian
    sudo apt-get install build-essential autoconf libtool pkg-config php php-dev
    curl -sS https://getcomposer.org/installer | php
    sudo mv composer.phar /usr/local/bin/composer
    
  2. Install gRPC-PHP Extensions:

    sudo pecl install protobuf grpc
    

    Add to php.ini:

    extension=grpc.so
    extension=protobuf.so
    
  3. Generate Protobuf Classes: Clone the googleapis repo and generate PHP classes:

    git clone https://github.com/googleapis/googleapis.git
    cd googleapis
    make LANGUAGE=php OUTPUT=./generated
    

    Move generated files to your Laravel project (e.g., app/Generated/Google).

  4. Set Up Laravel Service Provider: Create a provider (e.g., app/Providers/GcpGrpcServiceProvider.php):

    use Google\Auth\ApplicationDefaultCredentials;
    use Grpc\ChannelCredentials;
    
    class GcpGrpcServiceProvider extends ServiceProvider
    {
        public function register()
        {
            $credentials = ApplicationDefaultCredentials::getCredentials();
            $sslCreds = ChannelCredentials::createSsl();
    
            $this->app->singleton('grpc.channel', function () use ($sslCreds, $credentials) {
                return new \Grpc\Channel(
                    'firestore.googleapis.com',
                    [
                        'credentials' => $sslCreds,
                        'update_metadata' => $credentials->getUpdateMetadataFunc(),
                    ]
                );
            });
        }
    }
    
  5. First gRPC Call in Laravel:

    use Google\Cloud\Firestore\V1beta1\FirestoreClient;
    use Google\Cloud\Firestore\V1beta1\ListDocumentsRequest;
    
    public function listFirestoreDocuments()
    {
        $channel = app('grpc.channel');
        $client = new FirestoreClient($channel);
    
        $request = new ListDocumentsRequest();
        $request->setParent("projects/{PROJECT_ID}/databases/(default)/documents");
    
        $response = $client->ListDocuments($request)->wait();
        return $response->getDocuments();
    }
    
  6. Configure Credentials: Set the GOOGLE_APPLICATION_CREDENTIALS environment variable in .env:

    GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/service-account.json
    

Implementation Patterns

1. Service Provider Pattern

Leverage Laravel’s service providers to centralize gRPC client initialization:

// app/Providers/GcpGrpcServiceProvider.php
public function register()
{
    $this->app->bind('firestore.client', function () {
        $channel = $this->app->make('grpc.channel');
        return new \Google\Cloud\Firestore\V1beta1\FirestoreClient($channel);
    });
}

Usage:

$documents = app('firestore.client')->ListDocuments($request)->wait();

2. Repository Pattern for GCP Services

Create repositories to abstract gRPC calls (e.g., FirestoreRepository):

class FirestoreRepository
{
    protected $client;

    public function __construct(FirestoreClient $client)
    {
        $this->client = $client;
    }

    public function getDocument(string $documentPath): ?Document
    {
        $request = new GetDocumentRequest();
        $request->setName($documentPath);

        $response = $this->client->GetDocument($request)->wait();
        return $response->getDocument() ?? null;
    }
}

Register in AppServiceProvider:

$this->app->bind(FirestoreRepository::class, function ($app) {
    return new FirestoreRepository($app->make('firestore.client'));
});

3. Streaming Workflows

Handle gRPC streaming (e.g., Pub/Sub subscriptions) with Laravel events or queues:

// Subscribe to Pub/Sub stream
$stream = $pubSubClient->Pull($request);
$stream->onReceive(function ($message) {
    event(new PubSubMessageReceived($message));
});

Listen in Laravel:

Event::listen(PubSubMessageReceived::class, function ($event) {
    dispatch(new ProcessPubSubMessage($event->message));
});

4. Protobuf Model Binding

Map gRPC responses to Laravel models or collections:

public function syncFirestoreToLocal()
{
    $documents = app('firestore.client')->ListDocuments($request)->wait();
    return collect($documents->getDocuments())
        ->map(fn ($doc) => new LocalDocumentModel([
            'id' => $doc->getName(),
            'data' => $doc->getFields(),
        ]));
}

5. Error Handling

Wrap gRPC calls in Laravel exceptions:

public function safeGrpcCall(callable $callback)
{
    try {
        return $callback();
    } catch (\Grpc\RpcException $e) {
        throw new GcpGrpcException(
            "gRPC Error: {$e->getCode()}: {$e->getMessage()}",
            $e->getCode(),
            $e
        );
    }
}

6. Configuration Management

Use Laravel’s config system for gRPC settings:

// config/gcp.php
return [
    'services' => [
        'firestore' => [
            'project_id' => env('GCP_FIRESTORE_PROJECT_ID'),
            'database' => '(default)',
        ],
    ],
];

Access in code:

$projectId = config('gcp.services.firestore.project_id');

7. Testing with Mocks

Use Google\Cloud\Testing\MockGrpc for unit tests:

public function testFirestoreListDocuments()
{
    $mock = new MockFirestoreClient();
    $mock->expects($this)
        ->method('ListDocuments')
        ->willReturn(new ListDocumentsResponse());

    $repository = new FirestoreRepository($mock);
    $this->assertNotNull($repository->getDocument('test-path'));
}

Gotchas and Tips

Pitfalls

  1. Protobuf Generation Issues:

    • Problem: Missing dependencies in .proto files cause compilation errors.
    • Fix: Use the make LANGUAGE=php script from googleapis to auto-resolve dependencies.
    • Tip: Store generated files in vendor/google/protobuf to avoid version conflicts.
  2. Credential Management:

    • Problem: ApplicationDefaultCredentials fails silently if GOOGLE_APPLICATION_CREDENTIALS is misconfigured.
    • Fix: Validate credentials early in your service provider:
      $credentials = ApplicationDefaultCredentials::getCredentials();
      if (!$credentials) {
          throw new RuntimeException("GCP credentials not found. Set GOOGLE_APPLICATION_CREDENTIALS.");
      }
      
  3. Shared Memory Leaks:

    • Problem: Long-running Laravel processes (e.g., queues) may leak file descriptors.
    • Fix: Use google/grpc-gcp:^0.4.0 (fixed in v0.1.2+) and ensure processes are short-lived or use pcntl_fork() for workers.
  4. PHP 8.x Compatibility:

    • Problem: Protobuf v5+ may conflict with older google/cloud SDKs.
    • Fix: Pin dependencies in composer.json:
      "require": {
          "protobuf/php": "^5.0",
          "google/cloud-firestore": "^1.0"
      }
      
  5. TLS Certificate Validation:

    • Problem: Self-signed certificates or custom CA paths break gRPC.
    • Fix: Configure custom credentials:
      $sslCreds = ChannelCredentials::createSsl([
          'ca_file' => '/path/to/custom-ca.pem',
      ]);
      
  6. Streaming Timeouts:

    • Problem: Long-running streams (e.g., Pub/Sub) may time out in Laravel’s request lifecycle.
    • Fix: Use Laravel Queues to process streams asynchronously:
      $stream->onReceive(function ($message) {
          ProcessPubSubMessage::dispatch($message);
      });
      
  7. Protobuf Serialization:

    • Problem: Custom protobuf messages may not serialize/deserialize correctly.
    • Fix: Implement Serializable or use google/protobuf:^3.21 for backward compatibility.

Debugging Tips

  1. Enable gRPC Logging: Add to php.ini:

    grpc.verbose_logging = 1
    

    Check logs at /var/log/php-grpc.log.

  2. Inspect Metadata: Log gRPC metadata for debugging:

    $metadata = $response->getTrailingMetadata();
    \Log::debug('gRPC Metadata:', $metadata->toArray());
    

3

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder
psr/simple-cache-implementation
uri-template/tests