matthiasnoback/tail-event-stream

A simple, tail-based event stream

v0.1.1 2022-01-25 12:57 UTC

This package is auto-updated.

Last update: 2024-11-25 19:36:38 UTC


README

An event stream library based on tail.

Note: I don't think you should use this library in a real project, but it's great for educational purposes. I use it in workshops only."

Getting started

Install using Composer:

composer require matthiasnoback/tail-event-stream 

Usage

Adding messages to the stream:

use TailEventStream\Producer;

$streamFilePath = __DIR__ . '/var/stream.txt';

$producer = new Producer($streamFilePath);
$producer->produce('hello_world', ['Hello' => 'World!']);

The stream.txt file contains one message per line:

{"messageType":"hello_world","data":{"Hello":"World!"}}

Using tail -f a consumer can read each message from the stream, and it will keep consuming messages until you quit the process:

use TailEventStream\Consumer;

$streamFilePath = __DIR__ . '/var/stream.txt';

$consumer = new Consumer($streamFilePath);

$consumer->consume(function (string $messageType, array $data) {
    // $messageType will be 'hello_world'
    // $data will  be ['Hello' => 'World!']
});

consume() accepts a second argument, which is the index (or line) at which to start.