sandermuller / laravel-go-bridge
Dispatch jobs from Laravel PHP to a Laravel Go queue worker, and handle its callbacks
Requires
- php: ^8.4
- illuminate/contracts: ^12.0||^13.0
- illuminate/events: ^12.0||^13.0
- illuminate/queue: ^12.0||^13.0
- illuminate/redis: ^12.0||^13.0
- illuminate/support: ^12.0||^13.0
Requires (Dev)
- aws/aws-sdk-php: ^3.411
- driftingly/rector-laravel: ^2.7.3
- larastan/larastan: ^3.10
- laravel/boost: ^2.0||^3.0
- laravel/pao: ^1.0
- laravel/pint: ^1.29
- nunomaduro/collision: ^8.0
- orchestra/testbench: ^11.0
- pestphp/pest: ^5.0
- pestphp/pest-plugin-agent: ^5.0
- pestphp/pest-plugin-arch: ^5.0
- pestphp/pest-plugin-laravel: ^5.0
- pestphp/pest-plugin-phpstan: ^5.0
- pestphp/pest-plugin-rector: ^5.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-mockery: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- predis/predis: ^3.8
- rector/rector: ^2.8
- sandermuller/boost-skills: ^2.27.0
- sandermuller/package-boost-laravel: ^1.0
- sandermuller/rector-preset: ~2.8.0
- spaze/phpstan-disallowed-calls: ^4.13
- symplify/phpstan-rules: ^14.19
- tomasvotruba/cognitive-complexity: ^1.2
- tomasvotruba/type-coverage: ^2.3
Suggests
- ext-redis: For the Redis transport (or predis/predis)
- aws/aws-sdk-php: For the SQS transport and the SQS callback connection
- predis/predis: For the Redis transport (or ext-redis)
Provides
None
Conflicts
None
Replaces
None
README
Queue jobs from a Laravel PHP app for a Laravel Go worker, and handle the jobs that worker sends back.
Laravel Go's queue workers don't run PHP's jobs. A PHP job holds a serialize()d object, and Laravel Go reads its own JSON payload. This package writes that payload, so a PHP app can hand one piece of work to Go and keep everything else in PHP.
To run the work on a Laravel Rust worker instead, use laravel-rust-bridge. It has the same API and the same callback flow.
use SanderMuller\LaravelGoBridge\Facades\GoBridge; GoBridge::dispatch('exports.statistics', ['export_id' => $export->id]);
// jobs/export_statistics.go type ExportStatistics struct { ExportID int64 `json:"export_id"` } func (ExportStatistics) JobName() string { return "exports.statistics" } func (j ExportStatistics) Handle(ctx context.Context) error { // ... return nil }
Why not send plain JSON with pushRaw() to a worker you write yourself? A simple worker that pops jobs off a Redis list with BLPOP loses a job when it crashes mid-run, has no retries and no failed jobs, and can read only one job type per queue. The bridge writes Laravel Go's own payload, so the Laravel Go worker reserves each job, retries it, records it when it fails, and finds the job type by name. The package also handles the callbacks back to PHP.
Warning
Laravel Go is in beta, and its payload format is internal to the framework. This package writes payload version 1, which go.laravel.com v0.1.x reads. Keep the exact version in go.mod (go.laravel.com v0.1.0) and upgrade both sides together. If the format changes, the worker fails the job with "queued in payload format 1, which this application can't read" instead of misreading it.
Requirements
- PHP 8.4+, Laravel 12 or 13
- A Laravel Go app on
go.laravel.comv0.1.x, with aredisorsqsqueue connection - For Redis: phpredis or Predis
- For SQS:
aws/aws-sdk-php
Installation
composer require sandermuller/laravel-go-bridge php artisan vendor:publish --tag=laravel-go-bridge-config
Configuration
Redis
Laravel Go reads its queue from unprefixed keys (queues:{name}), and Laravel PHP prefixes Redis keys by default. Add a connection without a prefix to config/database.php, on the Redis database the worker reads:
'redis' => [ // ... 'go' => [ 'url' => env('GO_REDIS_URL'), 'host' => env('REDIS_HOST', '127.0.0.1'), 'port' => env('REDIS_PORT', '6379'), 'database' => env('GO_REDIS_DB', '5'), 'options' => ['prefix' => ''], ], ],
The bridge asks the Redis client for its prefix before every dispatch, and throws a MisconfiguredBridge exception if there is one, because the worker would never see those jobs. Laravel takes the prefix from the connection's prefix key, then its options.prefix, then the global database.redis.options.prefix. Set options.prefix to '' on this connection and don't give it a prefix key; the global prefix can stay.
On the Go side, point REDIS_URL and REDIS_DB at the same database and set QUEUE_CONNECTION=redis. Set the database with REDIS_DB, not in REDIS_URL: a database in the URL wins over both REDIS_DB and REDIS_CACHE_DB, so the Go app's jobs and cache would share one database, and a cache flush would delete queued jobs. If the Go app gives its queues a Redis connection of their own (REDIS_QUEUE_CONNECTION), point that connection at this database instead. The Go worker reads the queue REDIS_QUEUE names, default unless you set it, so when you change GO_BRIDGE_QUEUE, set REDIS_QUEUE to the same name or start the worker with --queue.
SQS
Set GO_BRIDGE_TRANSPORT=sqs. The bridge sends through your PHP app's sqs queue connection, so queue names resolve against its prefix and suffix like any other job.
Give the Go worker its own queue. Laravel PHP, Laravel Go and the bridge all default to the queue default, and each worker fails any job of the other side's that it receives. The bridge throws a MisconfiguredBridge exception when a job would go to the connection's default queue, which PHP workers read. Set GO_BRIDGE_QUEUE to the Go queue, for example go, set QUEUE_CONNECTION=sqs in the Go app, and set its SQS_QUEUE to the same name. Both apps must resolve that name to the same queue URL, so give them the same SQS_PREFIX and SQS_SUFFIX, and use the name without the suffix: Laravel Go always appends the suffix, and Laravel PHP appends it only when the name lacks it. The PHP connection needs the prefix: without one, Laravel PHP can't build the queue URL. An SQS connection that only the bridge uses needs a queue option other than the Go queue too. The bridge checks only the connection it sends through. If PHP workers read another SQS connection, make sure that connection's queue is not the Go queue either.
Laravel's SQS overflow (overflow.enabled on the connection) rewrites large payloads into a cache pointer that Laravel Go can't read. Keep job data small: send ids, not records.
Options
| Key | Env | Default |
|---|---|---|
transport |
GO_BRIDGE_TRANSPORT |
redis |
queue |
GO_BRIDGE_QUEUE |
default |
redis.connection |
GO_BRIDGE_REDIS_CONNECTION |
go |
sqs.connection |
GO_BRIDGE_SQS_CONNECTION |
sqs |
Dispatching jobs to Go
$id = GoBridge::dispatch('exports.statistics', ['export_id' => 42]); $id = GoBridge::dispatch('imports.viewers', ['import_id' => 7], queue: 'go-imports');
jobis the name the Go job'sJobName()method returns. Always give the Go job aJobName(): without one, the name is the type's package path and name, such asapp/jobs.ExportStatistics, and moving or renaming the type breaks every job PHP sends. The job type must also be listed injobs.All, or the worker can't find it.databecomes the Go job struct's exported fields. Laravel Go decodes it withencoding/json/v2, which matches names case-sensitively: without ajsontag, the key is the Go field name (ExportID), notexport_id. Give each field ajsontag with the key PHP sends, and make sure the values decode into the field types.dispatch()returns the job id, a UUIDv7, which is the id the Go worker logs.
You can also inject SanderMuller\LaravelGoBridge\GoBridge instead of using the facade.
Not supported yet: delays, chains, batches and SQS FIFO queues. The bridge sends no message group, and SQS refuses a FIFO message without one.
Handling jobs from Go
The Go app writes a small JSON message to a callback queue, and the package fires a GoCallbackReceived event for it:
use SanderMuller\LaravelGoBridge\Events\GoCallbackReceived; Event::listen(function (GoCallbackReceived $event): void { if ($event->name === 'export.finished') { ExportFinished::dispatch(Export::findOrFail($event->data['export_id'])); } });
The callback connection
Never let the Go app write to one of your ordinary queues. Laravel's worker trusts what it reads from a queue: it builds the class a message names, and it unserializes the job a message carries, so anything that can write to a queue can run code in your PHP app.
Callbacks get a queue connection of their own instead, with the go-callbacks driver. On this connection every message is rewritten into a GoCallback job, holding only a name and a data array, before Laravel reads it. A message can't name another class, and nothing in it is unserialized: not when it runs, not when it fails, and not when queue:retry picks it up again.
Add the connection to config/queue.php, on the database:
'go-callbacks' => [ 'driver' => 'go-callbacks', 'via' => 'database', 'connection' => env('DB_CONNECTION'), 'table' => 'go_callbacks', 'queue' => 'default', 'retry_after' => 90, ],
php artisan vendor:publish --tag=laravel-go-bridge-migrations php artisan migrate
Or on SQS, with the same options as Laravel's sqs driver:
'go-callbacks' => [ 'driver' => 'go-callbacks', 'via' => 'sqs', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'prefix' => env('SQS_PREFIX'), 'queue' => env('GO_CALLBACKS_SQS_QUEUE', 'go-callbacks'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), ],
SQS overflow (overflow.enabled) is refused on this connection: a message could point the worker at any key in the overflow cache store.
Then run a worker for it, next to your other workers:
php artisan queue:work go-callbacks
Sending a callback from Go
The message the Go app writes:
{
"data": { "name": "export.finished", "data": { "export_id": 42 } },
"maxTries": 3
}
The package reads only these fields:
data.name: letters, digits,.,_,:and-, up to 255 characters.data.data: a JSON object. Leave it out to send an empty one.maxTries: a whole number from 1 to 10. 0 or less becomes 1, because Laravel reads 0 as "retry forever", and more than 10 becomes 10. Leave it out to use the worker's--tries.
Every other field is ignored, and each message gets a new UUID. A message larger than 1 MiB fails without being decoded, and so does a message that grows past 1 MiB when the package rewrites it. A message that breaks these rules fails on its first try and lands in PHP's failed jobs. The exception names the problem, never the message's values.
On the database, insert it into go_callbacks. Laravel PHP stores Unix-second timestamps there, and Laravel Go's own jobs table stores times, so write the integers yourself. php is a connection in the Go app's cfg.Database.Connections that points at the PHP app's database:
import ( "context" "encoding/json" "go.laravel.com/date" "go.laravel.com/db" ) func ExportFinished(ctx context.Context, exportID int64) error { payload, err := json.Marshal(map[string]any{ "data": map[string]any{"name": "export.finished", "data": map[string]any{"export_id": exportID}}, "maxTries": 3, }) if err != nil { return err } now := date.Now(ctx).Unix() return db.Table("go_callbacks").Insert(db.Connection(ctx, "php"), db.Values{ "queue": "default", "payload": string(payload), "attempts": 0, "available_at": now, "created_at": now, }) }
On SQS, send the same JSON as the message body to the callback queue.
Security
- Authorize in PHP, before you dispatch. A Go job doesn't know which user asked for it. Pass ids you have already checked, never raw request input.
- Treat callback data as input. Validate
GoCallbackReceived::$databefore you act on it. - Send ids, not secrets. Payloads sit unencrypted in Redis or SQS, and a failed callback's data stays in
failed_jobs.
If you restrict permissions, this is what each side needs:
- Database: the Go app needs
INSERTongo_callbacks(plusUSAGEon its id sequence on PostgreSQL). - SQS: PHP needs
SendMessageon the Go queues, andReceiveMessage,DeleteMessageandChangeMessageVisibilityon the callback queue. Go needs those three on its own queues,SendMessageon the callback queue, andGetQueueUrlif it looks queues up by name. - Redis ACL: PHP needs
EVAL,HSETandRPUSHonqueues:{<your Go queues>}*, plusSELECTfor a database other than 0.
Report security issues as described in SECURITY.md.
Testing
composer test
The Redis tests need a Redis server on 127.0.0.1:6379 and use database 15. Set REDIS_HOST, REDIS_PORT or GO_BRIDGE_TEST_REDIS_DB to change that.
License
MIT. See LICENSE.