folk-plugin-grpc
gRPC plugin for Folk — accepts gRPC calls via tonic/HTTP2 and dispatches to PHP workers. No Rust protobuf codegen required — all protobuf handling happens in PHP.
Configuration
Add to folk.build.toml:
[[]]
= "folk-plugin-grpc"
= "0.1"
= "grpc"
Configure in folk.toml:
[]
= "0.0.0.0:50051"
Settings
| Key | Type | Default | Description |
|---|---|---|---|
listen |
SocketAddr |
"0.0.0.0:50051" |
Address and port to listen on for gRPC connections. |
reflection |
bool |
false |
Planned. gRPC server reflection (allows grpcurl without -proto flag). Not yet implemented. |
Full example:
[]
= "0.0.0.0:50051"
Currently, clients must provide the .proto file for service discovery:
How it works
gRPC client ──HTTP/2──> folk-plugin-grpc (Rust)
│
├── extracts service, method, payload, metadata from request
├── encodes as MessagePack {service, method, payload, metadata}
└── dispatches to PHP worker via grpc.call RPC
│
▼
PHP handler
│
├── decodes protobuf request (google/protobuf PHP)
├── executes business logic
├── encodes protobuf response
└── returns raw bytes
│
▼
folk-plugin-grpc
│
└── wraps in gRPC frame, sends HTTP/2 response
The Rust plugin handles HTTP/2, gRPC framing, and metadata extraction. PHP handles protobuf serialization and business logic.
Metadata
gRPC metadata (the equivalent of HTTP headers) is extracted from the request and passed to PHP handlers via Context. Standard HTTP/2 and gRPC internal headers are filtered out — everything else is available:
Usage with folk-sdk (plain PHP)
1. Define your proto
syntax = "proto3";
package greeter;
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply);
}
message HelloRequest { string name = 1; }
message HelloReply { string message = 1; }
2. Generate PHP classes
This generates message classes (HelloRequest, HelloReply) but not service interfaces. You write the handler manually.
3. Write the handler (raw bytes mode)
4. Register
Usage with Laravel (folk-laravel)
Laravel integration uses typed handlers with auto-detection of protobuf parameters.
1. Define your proto and generate PHP classes
2. Create a service interface
The NAME constant must match the protobuf package.Service name — this is how the router maps incoming gRPC calls to your handler.
3. Implement the service
The GrpcRouter auto-detects protobuf-typed parameters and handles mergeFromString() / serializeToString() automatically. You work with typed objects, not raw bytes.
4. Register in config
That's it. FolkServiceProvider reads the config, resolves services from the Laravel container, and registers them with the GrpcRouter.
Accessing metadata in typed handlers
Add Context as the first parameter:
The router detects Context parameters automatically and injects the metadata.
Multiple services
Register as many services as you need:
Compatibility with Spiral/RoadRunner
If you have services generated by protoc-gen-php-grpc (Spiral), they work with Folk:
The NAME constant is read from any interface the handler implements. Context parameters (regardless of type) receive Folk's Context instance.
Testing with curl
# Encode HelloRequest { name: "Folk" } as protobuf
# Field 1 (string): 0x0a + length + bytes
|
# Decode response (skip 5-byte gRPC frame)
# Output: Hello, Folk!
Testing with grpcurl
# List services (requires -proto flag)
# Describe a service
# Note: grpcurl RPC calls require HTTP/2 trailers support (in progress)
Requirements
- PHP:
google/protobufpackage (composer require google/protobuf) protocfor generating PHP message classes
License
MIT