Easy way for connecting RoadRunner and Laravel applications (community integration).
Why Use This Package?This package provides complete Laravel integration with RoadRunner, offering:
Tip
There is an article that explains all the RoadRunner plugins.
Table of Contents Get Started InstallationFirst, install the Laravel Bridge package via Composer:
composer require roadrunner-php/laravel-bridge
Publish the configuration file:
php artisan vendor:publish --provider='Spiral\RoadRunnerLaravel\ServiceProvider' --tag=config
Download and install RoadRunner binary using DLoad:
./vendor/bin/dload get rrConfiguration
Create a .rr.yaml
configuration file in your project root:
version: '3' rpc: listen: 'tcp://127.0.0.1:6001' server: command: 'php vendor/bin/rr-worker start' http: address: 0.0.0.0:8080 middleware: [ "static", "headers", "gzip" ] pool: #max_jobs: 64 # feel free to change this supervisor: exec_ttl: 60s headers: response: X-Powered-By: "RoadRunner" static: dir: "public" forbid: [ ".php" ]Starting the Server
Start the RoadRunner server with:
./rr serveHow It Works
RoadRunner creates a worker pool by executing the command specified in the server configuration:
server: command: 'php vendor/bin/rr-worker start'
When RoadRunner creates a worker pool for a specific plugin, it sets the RR_MODE
environment variable to indicate which plugin is being used. The Laravel Bridge checks this variable to determine which Worker class should handle the request based on your configuration.
The selected worker then listens for requests from the RoadRunner server and handles them using the Octane worker, which clears the application state after each task (request, command, etc.).
Supported Plugins HTTP PluginThe HTTP plugin enables serving HTTP requests with your Laravel application through RoadRunner.
ConfigurationEnsure your .rr.yaml
has the HTTP section configured:
http: address: 0.0.0.0:8080 middleware: [ "static", "headers", "gzip" ] pool: max_jobs: 64 static: dir: "public" forbid: [ ".php" ]
Tip
Read more about the HTTP plugin in the RoadRunner documentation.
Jobs (Queue) PluginThe Queue plugin allows you to use RoadRunner as a queue driver for Laravel without additional services like Redis or a database.
ConfigurationFirst, add the Queue Service Provider in config/app.php
:
'providers' => [ // ... other providers Spiral\RoadRunnerLaravel\Queue\QueueServiceProvider::class, ],
Then, configure a new connection in config/queue.php
:
'connections' => [ // ... other connections 'roadrunner' => [ 'driver' => 'roadrunner', 'queue' => env('RR_QUEUE', 'default'), 'retry_after' => (int) env('RR_QUEUE_RETRY_AFTER', 90), 'after_commit' => false, ], ],
Update your .rr.yaml
file to include the Jobs section:
jobs: pool: num_workers: 4 pipelines: default: driver: memory config: { }
Set the QUEUE_CONNECTION
environment variable in your .env
file:
QUEUE_CONNECTION=roadrunner
That's it! You can now dispatch jobs to the RoadRunner queue without any additional services like Redis or Database.
Tip
Read more about the Jobs plugin in the RoadRunner documentation.
gRPC PluginThe gRPC plugin enables serving gRPC services with your Laravel application.
ConfigurationConfigure gRPC in your .rr.yaml
:
grpc: listen: 'tcp://0.0.0.0:9001' proto: - "proto/service.proto"
Then, add your gRPC services to config/roadrunner.php
:
return [ // ... other configuration 'grpc' => [ 'services' => [ \App\GRPC\EchoServiceInterface::class => \App\GRPC\EchoService::class, ], ], ];gRPC Client Usage
The package also allows your Laravel application to act as a gRPC client, making requests to external gRPC services.
Client ConfigurationAdd your gRPC client configuration to config/roadrunner.php
:
return [ // ... other configuration 'grpc' => [ // ... server config 'clients' => [ 'services' => [ [ 'connection' => '127.0.0.1:9001', // gRPC server address 'interfaces' => [ \App\Grpc\EchoServiceInterface::class, ], // 'tls' => [ ... ] // Optional TLS configuration ], ], // 'interceptors' => [ ... ] // Optional interceptors ], ], ];Using the gRPC Client in Laravel
You can inject Spiral\Grpc\Client\ServiceClientProvider
into your services or controllers to obtain a gRPC client instance:
use Spiral\Grpc\Client\ServiceClientProvider; use App\Grpc\EchoServiceInterface; use App\Grpc\EchoRequest; class GrpcController extends Controller { public function callService(ServiceClientProvider $provider) { /** @var EchoServiceInterface $client */ $client = $provider->get(EchoServiceInterface::class); $request = new EchoRequest(); $request->setMessage('Hello from client!'); $response = $client->Echo($request); return $response->getMessage(); } }
TemporalNote:
- Make sure you have generated the PHP classes from your
.proto
files (usingprotoc
).- The
connection
andinterfaces
must match the service you want to call.- You can configure multiple gRPC client services as needed.
Temporal is a workflow engine that enables orchestration of microservices and provides sophisticated workflow mechanisms.
ConfigurationFirst, configure Temporal in your .rr.yaml
:
temporal: address: 127.0.0.1:7233 activities: num_workers: 10
Then, configure your workflows and activities in config/roadrunner.php
:
return [ // ... other configuration 'temporal' => [ 'address' => env('TEMPORAL_ADDRESS', '127.0.0.1:7233'), 'namespace' => 'default', 'declarations' => [ \App\Temporal\Workflows\MyWorkflow::class, \App\Temporal\Activities\MyActivity::class, ], ], ];
Download Temporal binary for development:
./vendor/bin/dload get temporal
Start the Temporal dev server:
./temporal server start-dev --log-level error --color alwaysUseful Links Custom Workers
The RoadRunner Laravel Bridge comes with several predefined workers for common plugins, but you can easily create your own custom workers for any RoadRunner plugin. This section explains how to create and register custom workers in your application.
Understanding WorkersWorkers are responsible for handling requests from the RoadRunner server and processing them in your Laravel application. The predefined workers are configured in the config/roadrunner.php
file:
return [ // ... other configuration options ... 'workers' => [ Mode::MODE_HTTP => HttpWorker::class, Mode::MODE_JOBS => QueueWorker::class, Mode::MODE_GRPC => GrpcWorker::class, Mode::MODE_TEMPORAL => TemporalWorker::class, ], ];Creating Custom Workers
To create a custom worker, you need to implement the Spiral\RoadRunnerLaravel\WorkerInterface
. This interface has a single method, start()
, which is called when the worker is started by the RoadRunner server:
namespace App\Workers; use Spiral\RoadRunnerLaravel\WorkerInterface; use Spiral\RoadRunnerLaravel\WorkerOptionsInterface; class CustomWorker implements WorkerInterface { public function start(WorkerOptionsInterface $options): void { // Your worker implementation goes here // This method should handle requests from the RoadRunner server } }Registering Custom Workers
After creating your custom worker, you need to register it in the config/roadrunner.php
file:
return [ // ... other configuration options ... 'workers' => [ // Existing workers Mode::MODE_HTTP => HttpWorker::class, Mode::MODE_JOBS => QueueWorker::class, // Your custom worker for a custom or built-in plugin 'custom_plugin' => \App\Workers\CustomWorker::class, ], ];
The key in the workers
array should match the value of the RR_MODE
environment variable set by the RoadRunner server for your plugin.
If you find this package helpful, please consider giving it a star on GitHub. Your support helps make the project more visible to other developers who might benefit from it!
If you find any package errors, please, make an issue in a current repository.
You can also sponsor this project to help ensure its continued development and maintenance.
LicenseMIT License (MIT). Please see LICENSE
for more information.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4