decodelabs / eventful
Asynchronous IO event dispatcher
Installs: 10 171
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- decodelabs/coercion: ^0.2
- decodelabs/deliverance: ^0.2
- decodelabs/exceptional: ^0.4
Requires (Dev)
- ext-event: *
- decodelabs/phpstan-decodelabs: ^0.6
Suggests
- ext-event: Faster and more reliable event loops
README
Asynchronous IO event dispatcher for PHP
Eventful provides an extensible IO event dispatcher for use in interactive and asynchronous processes.
Get news and updates on the DecodeLabs blog.
Installation
Install the library via composer:
composer require decodelabs/eventful
Usage
Listen for events on IO, Signals and Timers and respond accordingly.
If php's Event extension is available, that will be used, otherwise a basic select()
loop fills in the gaps.
use DecodeLabs\Deliverance; use DecodeLabs\Eventful\Factory; $broker = Deliverance::newCliBroker(); $eventLoop = Factory::newDispatcher() // Run every 2 seconds ->bindTimer('timer1', 2, function() use($broker) { $broker->writeLine('Timer 1'); }) // Listen for reads, but frozen - won't activate until unfrozen ->bindStreamReadFrozen($input = $broker->getFirstInputReceiver(), function() use($broker) { $broker->writeLine('You said: '.$broker->readLine()); }) // Run once after 1 second ->bindTimerOnce('timer2', 1, function($binding) use($broker, $input) { $broker->writeLine('Timer 2'); // Unfreeze io reads $binding->eventLoop->unfreeze($intput); }) // Check if we want to bail every second ->setCycleHandler(function(int $cycles) { if($cycles > 10) { return false; } }); /* Outputs something like: Timer 2 Timer 1 Timer 1 You said: Hello world Timer 1 */
Licensing
Eventful is licensed under the MIT License. See LICENSE for the full license text.