Looking to hire Laravel developers? Try LaraJobs

pubsub-laravel maintained by thefreshuk

Description
Provides PubSub support for Laravel apps
Last update
2026/10/21 19:56 (dev-master)
License
Links
Downloads
2

Comments
comments powered by Disqus

PubSub for Laravel

This package integrates Laravel projects with Publish/Subscribe providers, using a content-based pub/sub model.

Supported Providers

  • AWS SNS

Install

via composer:

$ composer require thefreshuk/pubsub-laravel

Configuration

By default, this package looks for the following env variables:

PUBSUB_PROVIDER=sns
PUBSUB_TOPIC=<insert topic name>
PUBSUB_SNS_HOST_PATTERN=<regex string e.g /yourdomain.com/>
PUBSUB_BYPASS_VALIDATION=<true or false, optional>

PUBSUB_BYPASS_VALIDATION=true skips SNS signature validation (for local development only). If it is not set, validation is currently skipped when APP_ENV=local; this fallback is deprecated and will be removed in the next major version, so set the variable explicitly. Setting it to false enforces validation in every environment.

You may override this by running the following in your project:

php artisan vendor:publish

This package validates SNS message signatures using our fork of the AWS SNS message validator (thefreshuk/aws-php-sns-message-validator), which is not on Packagist. Add the repository to your composer.json:

"repositories": [
  {
    "type": "vcs",
    "url": "https://github.com/thefreshuk/aws-php-sns-message-validator.git"
  }
]

And then run:

$ composer update

Usage

Subscriptions

There are two ways to subscribe to a topic:

1. Extend SubscriptionServiceProvider:

use TheFresh\PubSub\SubscriptionServiceProvider as ServiceProvider;

class MockSubscriptionServiceProvider extends ServiceProvider
{
    public static $booted = false;

    /**
     * Like RouteServiceProvider, this namespace is applied to
     * the routes generated by this service provider.
     *
     * @var string $namespace
     */
    protected $namespace = 'App\Http\Controllers';

    /**
     * A mapping of message types to routes/controller actions that capture
     * messages.
     *
     * @var string $subscribe
     */
    protected $subscribe = [
        'test_type1' => ['/subscriptions/test-type', 'MockController@onTestType']
    ];

    public function boot()
    {
        parent::boot();

        static::$booted = true;
    }
}

This allows you to specify a mapping of 'types' to routes and controllers.

Types are our way of distinguishing between messages. Combined with content-based filtering, this provides a system that feels like a multi-topic system without the proliferation of topics in traditional SNS.

2. Inject Topic

The Topic class is available for use however you wish. By providing a ClientInterface object and a topic name, as a string, you can use multiple topics with this library.

Laravel will inject the default Topic into relevant type-hinted constructors. Since the library has been designed for use with a content-based Pub\Sub system, there must be a 'default' topic or else this will fail.

Publishing

The Topic class provides a publish method that accepts a message. It's as simple as calling this method for a given topic.

$topic->publish($message);

Messages

Messages are sent by publishers and received by subscribers. Every message has a 'type' and 'content':

{
  type: string,
  content: {}
}

The Pub\Sub system (preferably) filters based on the type property.

There are two ways to use Messages:

1. Separate classes

You can use the following command to generate message classes:

$ artisan make:message ExampleMessage

This generates a message class in <app_dir>/PubSub/Messages. You can instantiate and publish these messages.

2. DynamicMessage

You can also use the TheFresh\PubSub\Messages\DynamicMessage class. This accepts $type and $content constructor arguments. All messages received through subscriptions are DynamicMessages.