sevenlinx/pubsub-php

PubSub in PHP from Seven LinX Tech

dev-main / 1.x-dev 2021-04-22 15:53 UTC

This package is auto-updated.

Last update: 2024-10-22 23:55:37 UTC


README

Requirements

  • PHP ^8.0

Installation

composer require sevenlinx/pubsub-php

Driver

Implement your own driver with \SevenLinX\PubSub\PubSubDriverInterface

Example:

use SevenLinX\PubSub\Contracts\ChannelContract;
use SevenLinX\PubSub\Contracts\HasPriorityHandler;
use SevenLinX\PubSub\Contracts\MessageContract;
use SevenLinX\PubSub\PubSubDriverInterface;

final class MyOwnPubSubDriver implements PubSubDriverInterface
{
    public function publish(ChannelContract $channel, MessageContract $message): void
    {
        // How to publish message
    }

    public function publishBatch(ChannelContract $channel, MessageContract ...$messages): void
    {
        // The usual loop
        foreach ($messages as $message) {
            $this->publish($channel, $message);
        }
    }

    public function subscribe(ChannelContract $channel, callable $handler): void
    {
        // What to do when someone publish a message.
    }
}

Subscribe

Subscribe to a message.

Example:

$driver = new MyOwnPubSubDriver();
$channel = new MyOwnChannel();

// Using anonymous function
$driver->subscribe($channel, function ($message) {
    // Do whatever you want.
});
// Using class with __invoke method
$driver->subscribe($channel, new MyOwnHandler());

Publish

Publish a single message

Example:

...

$driver->publish($channel, new MyOwnMessage());

You can also publish multiple or in batch

...
$driver->publishBatch($channel, new MyOwnMessage(), new MyOtherMessage(), new GenericMessage());

Payload

You can create your own payload by implementing \SevenLinX\PubSub\Contracts\PayloadContract

Example:

use SevenLinX\PubSub\Contracts\PayloadContract;

final class Payload implements PayloadContract
{
    public function __construct(private string $message)
    {
    }
    
    public function getChannel() : string
    {
        return 'channel';    
    }
    
    public function payload() : mixed
    {
        return serialize($this->message); 
    }
}

You can pass this on your callable handler

use SevenLinX\PubSub\Contracts\ChannelContract;

...
public function subscribe(ChannelContract $channel, callable $handler): void
{
    $handler(new Payload('message'));
}

Generics

You can use generics for Channel, Message and Payload

use SevenLinX\PubSub\Generics\GenericChannel;use SevenLinX\PubSub\Generics\GenericMessage;

...
$driver->publish(new GenericChannel('my-own-channel'), new GenericMessage('my-own-message'));
// 

Example

You can the example in example/ directory or run:

php example/example.php

Testing

Just run:

composer run testing
Created under Seven LinX Incorporated