A full-stack gRPC application demonstrating PHP 7.3 compatibility with gRPC using Spiral PHP gRPC, featuring a React frontend that communicates with a PHP backend through gRPC-Web.
- Backend: PHP 7.3 gRPC server using Spiral PHP gRPC
- Frontend: React application with gRPC-Web client
- Proxy: Envoy proxy for gRPC-Web support
- Testing: Comprehensive tests for both backend and frontend
- Docker: Complete containerized setup
- PHP 7.3 Workaround: Demonstrates gRPC compatibility with older PHP versions
- Docker and Docker Compose
- Node.js 16+ (for local development)
- PHP 7.3+ (for local development)
- Composer (for local development)
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β React App βββββΆβ Envoy Proxy βββββΆβ PHP 7.3 gRPC β
β (Port 3000) β β (Port 8080) β β (Port 9001) β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
-
Clone and navigate to the project:
git clone <repository-url> cd grpc-app
-
Start all services:
docker-compose up --build
-
Access the application:
- Frontend: http://localhost:3000
- Backend gRPC: localhost:9001
- Envoy Admin: http://localhost:9901
-
Navigate to backend directory:
cd backend
-
Install dependencies:
composer install
-
Generate protobuf files:
protoc --php_out=src/ --plugin=protoc-gen-php-grpc=/usr/local/bin/protoc-gen-php-grpc --php-grpc_out=src/ proto/echo.proto
-
Run tests:
./vendor/bin/phpunit tests/ --verbose
-
Start the server:
php src/server.php
-
Navigate to frontend directory:
cd frontend
-
Install dependencies:
npm install
-
Run tests:
npm test
-
Start development server:
npm start
The backend includes comprehensive tests covering:
- Unit Tests: EchoService functionality
- Integration Tests: PHP 7.3 compatibility and extension requirements
- Edge Cases: Empty messages, special characters, unicode, long messages
Run backend tests:
cd backend
./vendor/bin/phpunit tests/ --verbose
The frontend includes React component tests covering:
- Component Rendering: UI elements and state
- User Interactions: Button clicks, input changes, keyboard events
- gRPC Communication: Successful requests and error handling
- Loading States: Request progress indicators
Run frontend tests:
cd frontend
npm test
- PHP Version: 7.3 (Alpine Linux)
- gRPC Library: Spiral PHP gRPC v1.4.0
- Server: RoadRunner v1.8.2
- Port: 9001
- React Version: 17.0.2
- gRPC-Web: v1.3.1
- Protocol Buffers: v3.19.4
- Port: 3000
- Version: v1.21-latest
- gRPC-Web Port: 8080
- Admin Port: 9901
- CORS: Enabled for all origins
grpc-app/
βββ backend/
β βββ src/
β β βββ Myecho/ # Generated protobuf classes
β β βββ EchoService.php # Main service implementation
β β βββ server.php # Server entry point
β βββ proto/
β β βββ echo.proto # Protocol buffer definition
β βββ tests/ # Backend tests
β βββ composer.json # PHP dependencies
β βββ phpunit.xml # PHPUnit configuration
β βββ Dockerfile # Backend container
βββ frontend/
β βββ src/
β β βββ proto/ # Generated gRPC-Web stubs
β β βββ App.js # Main React component
β β βββ App.test.js # Frontend tests
β βββ package.json # Node.js dependencies
β βββ Dockerfile # Frontend container
βββ envoy/
β βββ envoy.yaml # Envoy proxy configuration
βββ docker-compose.yaml # Service orchestration
βββ README.md # This file
Service: myecho.GreeterService
Method: Ping
Request:
message PingRequest {
string message = 1;
}
Response:
message PingResponse {
string message = 1;
}
Example Usage:
const request = new PingRequest();
request.setMessage("Hello from React!");
client.ping(request, {}, (err, response) => {
if (err) {
console.error('Error:', err);
} else {
console.log('Response:', response.getMessage());
}
});
-
Port Conflicts:
- Ensure ports 3000, 8080, and 9001 are available
- Check
docker-compose ps
for running containers
-
gRPC Connection Issues:
- Verify Envoy proxy is running on port 8080
- Check backend service is accessible on port 9001
- Review Envoy logs:
docker-compose logs envoy
-
PHP 7.3 Compatibility:
- The application uses Spiral PHP gRPC for PHP 7.3 support
- Custom protobuf classes are used instead of generated ones
- RoadRunner handles the gRPC server implementation
-
Frontend Build Issues:
- Clear node_modules:
rm -rf node_modules && npm install
- Check protobuf file generation
- Verify gRPC-Web client configuration
- Clear node_modules:
View logs for all services:
docker-compose logs -f
View logs for specific service:
docker-compose logs -f backend
docker-compose logs -f frontend
docker-compose logs -f envoy
Access Envoy admin interface:
- URL: http://localhost:9901
- View cluster health and configuration
-
Security:
- Update CORS configuration in Envoy
- Use environment variables for sensitive data
- Enable HTTPS/TLS
-
Performance:
- Configure proper resource limits in Docker
- Use production-optimized PHP settings
- Enable gRPC compression
-
Monitoring:
- Add health check endpoints
- Configure logging and metrics
- Set up monitoring dashboards
This project demonstrates how to use gRPC with PHP 7.3 by:
- Using Spiral PHP gRPC library instead of the official gRPC extension
- Creating custom protobuf message classes compatible with PHP 7.3
- Using RoadRunner as the gRPC server runtime
- Implementing proper namespace structure for autoloading
- Manual protobuf class implementation
- Custom service interface definitions
- RoadRunner-based server instead of native gRPC server
- Compatibility layer for older PHP features