This repository demonstrates a minimal yet production-ready gRPC integration using Laravel with RoadRunner and Spiral PHP gRPC. It contains two Laravel Sail applications:
publisher-app: Publishes gRPC messagesconsumer-app: Listens and processes gRPC messages
Both services communicate via gRPC using Protocol Buffers.
[publisher-app] Laravel β Spiral\GRPC\Client
|
| gRPC over TCP (default port 50051)
v
[consumer-app] Laravel + RoadRunner gRPC server
- Laravel Sail (Docker-based)
spiral/roadrunnerfor gRPC servernamely/protoc-allfor generating PHP stubs- No PECL compilation; all
.sofiles prebuilt and compressed - Protos are local per service, not shared
- PHP 8.4+ (via Sail)
- Docker + Docker Compose
- Composer
- Laravel Sail
- β
No need for local
grpc.so/protobuf.soor PECL tools
git clone https://github.com/rxcod9/laravel-grpc-example.git
cd laravel-grpc-examplecd publisher-app && composer install
cd consumer-app && composer installIf you do not have php8.4 in host and wants to use docker with php8.4 for installing composer.
cd publisher-app
docker run --rm \
--pull=always \
-v "$(pwd)":/opt \
-w /opt \
laravelsail/php84-composer:latest \
bash -c "composer install"
cd consumer-app
docker run --rm \
--pull=always \
-v "$(pwd)":/opt \
-w /opt \
laravelsail/php84-composer:latest \
bash -c "composer install"When you add.modify .proto files, you need to generate these files
# For publisher-app
cd publisher-app/
protoc --proto_path=./protos \
--plugin=protoc-gen-php=$(which protoc-gen-php) \
--plugin=protoc-gen-php-grpc=/usr/local/bin/protoc-gen-php-grpc \
--php_out=./app/Grpc \
--php-grpc_out=./app/Grpc \
./protos/messages.proto
# For consumer-app
cd consumer-app/
protoc --proto_path=./protos \
--plugin=protoc-gen-php=$(which protoc-gen-php) \
--plugin=protoc-gen-php-grpc=/usr/local/bin/protoc-gen-php-grpc \
--php_out=./app/Grpc \
--php-grpc_out=./app/Grpc \
./protos/messages.protocd publisher-app/
docker run --rm \
-v "$PWD:/workspace" \
-w /workspace namely/protoc-all \
-f protos/messages.proto \
-l php \
-o app/Grpc
cd consumer-app/
docker run --rm \
-v "$PWD:/workspace" \
-w /workspace namely/protoc-all \
-f protos/messages.proto \
-l php \
-o app/GrpcThis uses
namely/protoc-allto avoid installing Protobuf toolchains locally.
cd publisher-app
./vendor/bin/sail up -d
cd consumer-app
./vendor/bin/sail up -dThe .so files for grpc and protobuf are:
- Precompiled
- Compressed into
grpc-protobuf.tar.gz - Extracted into
/usr/lib/php/20240924inside the Sail containers - Auto-enabled in
/etc/php/8.3/cli/conf.d/99-grpc.ini
This avoids long PECL install times and keeps builds deterministic.
You can test the gRPC publisher:
cd publisher-app
./vendor/bin/sail artisan grpc:publish-message notifications '{"event":"user.registered"}'Expected output:
{
"success": true,
"message": "Message sent to consumer via gRPC"
}To tail consumer logs:
cd consumer-app
./vendor/bin/sail logs -fVerify gRPC extension is loaded:
./vendor/bin/sail php -m | grep grpc
./vendor/bin/sail php -m | grep protobufcd publisher-app && ./vendor/bin/sail down
cd consumer-app && ./vendor/bin/sail downlaravel-grpc-example/
βββ consumer-app/ # Laravel gRPC server with RoadRunner + Supervisor
β βββ app/Grpc/ # Generated gRPC stub code
β βββ protos/ # Local proto definitions
β βββ start-container # Entrypoint
βββ publisher-app/ # Laravel gRPC client
βββ app/Grpc/ # Generated gRPC stub code
βββ protos/ # Local proto definitions
βββ grpc:publish-message commandMake sure these .env variables are set:
consumer-app/.env
GRPC_PORT=50051publisher-app/.env
CONSUMER_PORT=50051- RoadRunner runs via Supervisor
- On container start:
bootstrap.phpis invoked- Registers
MessageService - Logs to stdout
- Use
.protosync between services (e.g., via@shared-protosgit submodule) - Add TLS support (gRPC + secure channels)
- Use GitHub Actions to generate
.sointo GitHub Releases - Implement streaming/bidirectional gRPC calls
- Integrate with Laravel Events/Queue workers
Feel free to open an issue or discuss ideas for enhancements. Contributions are welcome!
MIT β free to use, modify, and distribute.