PHPackages                             stk2k/eventstream - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. stk2k/eventstream

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

stk2k/eventstream
=================

A simple PHP pub-sub library

0.9.1(5y ago)24.5k9MITPHPPHP &gt;=7.2CI failing

Since Jun 27Pushed 5y ago1 watchersCompare

[ Source](https://github.com/stk2k/eventstream)[ Packagist](https://packagist.org/packages/stk2k/eventstream)[ Docs](https://github.com/stk2k/eventstream)[ RSS](/packages/stk2k-eventstream/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (2)Versions (20)Used By (9)

PHP simple pub-sub library
==========================

[](#php-simple-pub-sub-library)

[![Latest Version on Packagist](https://camo.githubusercontent.com/c4c3a64c979da240b96c01436b6264b70bee1dc9a7cc406a41de820a1411764b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73746b326b2f6576656e7473747265616d2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/stk2k/eventstream)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/a8ed69257f7eb01cbdcfae6a509ef45ecf3166c52dd523fc4ddcdf4cb5d3fc4d/68747470733a2f2f7472617669732d63692e6f72672f73746b326b2f6576656e7473747265616d2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/stk2k/eventstream)[![Coverage Status](https://camo.githubusercontent.com/900fcdc49b50a67453142b929e4b43570dc0412d7f8e09d91ca2db524eb2fe50/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f73746b326b2f6576656e7473747265616d2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/stk2k/eventstream?branch=master)[![Code Climate](https://camo.githubusercontent.com/34c1bb9f80494fb468710adc0c7e8a10e6ff8f78f78f562ac9e0ae6a3433e7d2/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f73746b326b2f6576656e7473747265616d2f6261646765732f6770612e737667)](https://codeclimate.com/github/stk2k/eventstream)[![Total Downloads](https://camo.githubusercontent.com/3a0ceab6420c1a3bdc23c4bf28504e5f284d67aa5a35fd7aaafc15c96f0a310a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73746b326b2f6576656e7473747265616d2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/stk2k/eventstream)

Description
-----------

[](#description)

You can use this library to build an application using simple pub-sub pattern. This library consists of three main objects below:

- EventStream

event stream is frontend facade of this system. you can push events to event source, register event listeners to event emitter, and flush events.

- EventSource

event source is an event provider. it has a role to provide events when event stream request. you have to declare event source, and call EventStream#source() method to attach source.

- EventEmitter

event emitter is an event dispatcher. it has a role to manage user callback functions. use Emitter/SimpleEventEmitter class for normal use, and pass it to EventStream#emitter() method.

Demo
----

[](#demo)

### 01. event source

[](#01-event-source)

```
use stk2k\eventstream\EventStream;
use stk2k\eventstream\EventSourceInterface;
use stk2k\eventstream\emitter\SimpleEventEmitter;
use stk2k\eventstream\exception\EventSourceIsNotPushableException;
use Stk2k\EventStream\Event;

class NumberEventSource implements EventSourceInterface
{
    protected $numbers;

    public function __construct() {
        $this->numbers = ['one', 'two', 'three'];
    }
    public function canPush() : bool {
        return false;
    }
    public function push(Event $event) : EventSourceInterface {
        return $this;
    }
    public function next() {
        $number = array_shift($this->numbers);
        return $number ? new Event('number',$number) : null;
    }
}

// create event stream and setup callback, then flush all events
(new EventStream())
    ->channel('my channel', new NumberEventSource(), new SimpleEventEmitter())
    ->listen('number', function(Event $e){
        echo 'received number='.$e->getPayload(), PHP_EOL;
    })
    ->flush();

// received number=one
// received number=two
// received number=three

// you can not push event to unpushable event source
try{
    (new NumberEventSource())->push(new Even('number','four'));   // throws EventSourceIsNotPushableException
}
catch(EventSourceIsNotPushableException $e){
    echo 'Event not publishable.';
}

class PushableNumberEventSource extends NumberEventSource
{
    public function canPush() : bool {
        return true;
    }
    public function push(Event $event) : EventSourceInterface {
        if ($event->getName() === 'number'){
            $this->numbers[] = $event->getPayload();
        }
        return $this;
    }
}

// you acn push event to pushable event source
try{
    (new EventStream())
        ->channel('my channel')
        ->source((new PushableNumberEventSource())->push('number','four'))
        ->emitter(new SimpleEventEmitter())
        ->listen('number', function(Event $e){
                echo 'received number='.$e->getPayload(), PHP_EOL;
            })
        ->flush()
        ->push('number', 'five')
        ->flush();
}
catch(EventSourceIsNotPushableException $e){
    echo 'Event not publishable.';
}

// received number=one
// received number=two
// received number=three
// received number=four
// received number=five
```

Usage
-----

[](#usage)

1. create event stream object
2. define your own event source class and attach new instance to stream.
3. define your own event emitter or use bundled emitter classes and attach new instance to stream.
4. define your own callback(s) and attach it(them) to stream or emitter.
5. flush events in event source via EventStream#flush() method.

More Examples
-------------

[](#more-examples)

- numbers.php: simple emitter and source sample
- multi\_channel.php: listen different channel(event)s.
- regular\_expression.php: single bind but listen multi channels by regular expression.
- wild\_card.php: single bind but listen multi channels by wild card.

Requirement
-----------

[](#requirement)

PHP 7.2 or later

Installing stk2k/eventstream
----------------------------

[](#installing-stk2keventstream)

The recommended way to install Calgamo/Bench is through [Composer](http://getcomposer.org).

```
composer require stk2k/eventstream
```

After installing, you need to require Composer's autoloader:

```
require 'vendor/autoload.php';
```

License
-------

[](#license)

[MIT](https://github.com/stk2k/eventstream/blob/master/LICENSE)

Author
------

[](#author)

[stk2k](https://github.com/stk2k)

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~80 days

Recently: every ~143 days

Total

19

Last Release

1850d ago

PHP version history (5 changes)0.1.1PHP &gt;=5.4.0

0.2.0PHP &gt;=5.3.0

0.5.0PHP &gt;=7.0

0.7.0PHP &gt;=7.1

0.8.0PHP &gt;=7.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/985640?v=4)[stk2k](/maintainers/stk2k)[@stk2k](https://github.com/stk2k)

---

Top Contributors

[![stk2k](https://avatars.githubusercontent.com/u/985640?v=4)](https://github.com/stk2k "stk2k (31 commits)")

---

Tags

phppub-subPublish-subscribe pattern

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/stk2k-eventstream/health.svg)

```
[![Health](https://phpackages.com/badges/stk2k-eventstream/health.svg)](https://phpackages.com/packages/stk2k-eventstream)
```

###  Alternatives

[imanghafoori/laravel-anypass

A minimal yet powerful package to help you in development.

21422.6k](/packages/imanghafoori-laravel-anypass)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
