Search by

sandermuller / laravel-go-bridge

SanderMuller

Dispatch jobs from Laravel PHP to a Laravel Go queue worker, and handle its callbacks

Package info

github.com/SanderMuller/laravel-go-bridge

Homepage

pkg:composer/sandermuller/laravel-go-bridge

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

v0.1.0 2026-12-02 23:42 UTC

This package is auto-updated.

Last update: 2026-12-03 00:28:30 UTC


README

Latest Version on Packagist Tests Total Downloads License

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.com v0.1.x, with a redis or sqs queue 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');
  • job is the name the Go job's JobName() method returns. Always give the Go job a JobName(): without one, the name is the type's package path and name, such as app/jobs.ExportStatistics, and moving or renaming the type breaks every job PHP sends. The job type must also be listed in jobs.All, or the worker can't find it.
  • data becomes the Go job struct's exported fields. Laravel Go decodes it with encoding/json/v2, which matches names case-sensitively: without a json tag, the key is the Go field name (ExportID), not export_id. Give each field a json tag 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::$data before 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 INSERT on go_callbacks (plus USAGE on its id sequence on PostgreSQL).
  • SQS: PHP needs SendMessage on the Go queues, and ReceiveMessage, DeleteMessage and ChangeMessageVisibility on the callback queue. Go needs those three on its own queues, SendMessage on the callback queue, and GetQueueUrl if it looks queues up by name.
  • Redis ACL: PHP needs EVAL, HSET and RPUSH on queues:{<your Go queues>}*, plus SELECT for 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.