enqueue/gps
Google Pub/Sub transport for Enqueue, implementing the Queue Interop specification. Send and consume messages via Google Cloud Pub/Sub with a compatible PHP queue client. Part of the Enqueue ecosystem; docs and support available via the project site and Gitter.
Install the package:
composer require enqueue/gps
Configure Google Cloud credentials:
GOOGLE_APPLICATION_CREDENTIALS environment variable to point to your service account JSON key file.Google\Cloud\Core\Grpc\CredentialsPool in your code.Basic usage with Enqueue:
use Enqueue\Client\Producer;
use Enqueue\Client\Consumer;
use Enqueue\Gps\GpsConnectionFactory;
$connectionFactory = new GpsConnectionFactory('your-project-id');
$connection = $connectionFactory->createConnection(['topic' => 'your-topic-name']);
// Producer
$producer = new Producer($connection);
$producer->send(new \Enqueue\Message\Message(['data' => 'Hello Pub/Sub!']));
// Consumer
$consumer = new Consumer($connection);
$consumer->consume(function (\Enqueue\Message\Message $message) {
echo $message->getBody();
return \Enqueue\Message\Message::ACK;
});
$producer->send(new \Enqueue\Message\Message(['data' => 'Delayed!'], [
'delay' => 60, // 60 seconds delay
]));
Topic-Based Routing:
orders, notifications).Batch Processing:
$connectionFactory->createConnection(['topic' => 'high-volume-topic', 'batchSettings' => [
'maxMessages' => 100,
'maxBytes' => 1024 * 1024, // 1MB
'maxDelay' => 10, // 10 seconds
]]);
Error Handling:
$connectionFactory->createConnection(['subscription' => 'your-subscription', 'ackDeadline' => 30]);
Integration with Laravel:
enqueue/laravel alongside enqueue/gps for seamless Laravel queue integration:
$connection = new \Enqueue\Gps\GpsConnectionFactory('project-id');
$queue = new \Enqueue\Laravel\LaravelQueue($connection);
Environment Configuration:
Store project ID, topic/subscription names, and credentials in .env:
QUEUE_GPS_PROJECT_ID=your-project-id
QUEUE_GPS_TOPIC=your-topic
QUEUE_GPS_SUBSCRIPTION=your-subscription
Dependency Injection: Bind the connection factory in Laravel's service provider:
$this->app->bind(\Enqueue\Gps\GpsConnectionFactory::class, function ($app) {
return new \Enqueue\Gps\GpsConnectionFactory(
$app['config']['queue.gps.project_id']
);
});
Monitoring:
Use Pub/Sub's built-in metrics (e.g., subscription/backlog_bytes) to monitor queue health.
Credentials Handling:
GOOGLE_APPLICATION_CREDENTIALS or providing invalid credentials will throw Google\Auth\Exception\InvalidCredentialsException.Google\Cloud\Core\Grpc\CredentialsPool for dynamic credential loading or ensure the environment variable is set.Subscription vs. Topic:
Ack Deadlines:
ackDeadline (default: 10 seconds) to process a message.ackDeadline (e.g., 30 seconds for long-running tasks) or use modifyAckDeadline to extend it dynamically.Message Size Limits:
Enable Debug Logging:
$connectionFactory = new GpsConnectionFactory('project-id', [
'logger' => new \Monolog\Logger('gps', [
new \Monolog\Handler\StreamHandler('php://stderr', \Monolog\Logger::DEBUG),
]),
]);
Check Pub/Sub Metrics: Use Google Cloud Console to verify message flow:
publish_requests, publish_errors).pull_requests, ack_deadline_exceeded).Custom Message Serialization: Override the default JSON serializer for complex objects:
$connectionFactory = new GpsConnectionFactory('project-id', [
'serializer' => new \Enqueue\Gps\Serializer\CustomSerializer(),
]);
Retry Logic: Implement custom retry logic for transient failures:
$connectionFactory->createConnection(['subscription' => 'your-subscription', 'retrySettings' => [
'maxRetries' => 3,
'initialBackoff' => 100, // ms
]]);
Middleware: Use Enqueue's middleware system to add preprocessing/postprocessing:
$producer = new Producer($connection);
$producer->withMiddleware(new \Enqueue\Middleware\Middleware\LoggingMiddleware());
Default Topic/Subscription:
If neither topic nor subscription is provided, the connection will default to the project ID as the topic name (not recommended for production).
Region Selection:
Explicitly set the region in the connection factory if not using the default (us-central1):
$connectionFactory = new GpsConnectionFactory('project-id', ['region' => 'europe-west1']);
How can I help you explore Laravel packages today?