Coder Social home page Coder Social logo

reactphp-input-stream's Introduction

reactphp-input-stream

What is this?

A compact library that can wrap a non-blocking input to behave like a stream in a ReactPHP EventLoop.

How do I use this?

Check out the examples folder. It contains a very basic example of a non-blocking input implementation, as well as a very (very) lame joke generator.

Why is this useful?

I needed to respond to incoming AMQP messages in my event loop and I was feeling adventurous.
Admittedly, I could have just used a periodic timer directly (that is what this library uses under the hood), but where's the fun in that?

I hear you ask: "Where's the code that deals with the AMQP messages?"
While I was writing this, I felt like it would be useful to separate the logic of dealing with input in a stream-like manner from the logic that deals with AMQP messages.
This means you can reuse this library to map practically anything to a readable stream.
An extra-lame example of this can be found in examples/jokes_example.php.

When the code for AMQP consumption as stream is finished, I will link it here.

Where are the unit tests?

Errrrrr...

How do I install this?

This should do it once it's available on Packagist:

composer require joopschilder/reactphp-input-stream

Mandatory block of example code

// Say, we have an event loop...
$loop = Factory::create();

// And say, we have a non-blocking input called $input...
$input = new DemoNonBlockingInput();

// Then we can create a ReadableStream from it like so:
$stream = new ReadableNonBlockingInputStream($input, $loop);

// If your 'select()' method takes a long time to execute, or you just don't
// feel like polling the input availability that often, you can 
// set a custom polling interval by supplying an instance of PollingInterval
// as the third constructor parameter:
$lowPollingStream = new ReadableNonBlockingInputStream($input, $loop, new PollingInterval(5));

// Of course, the stream emits all expected events (except end)
$stream->on('data', fn() => print('m'));
$stream->on('error', fn() => print('e'));
$stream->on('close', fn() => print('c'));

// If you know what data your input returns, you may type-hint it:
$stream->on('data', fn(Joke $joke) => print($joke . PHP_EOL));

// Add a periodic timer for demonstration purposes
$loop->addPeriodicTimer(0.2, fn() => print('.'));

// And kick 'er off.
$loop->run();

reactphp-input-stream's People

Contributors

joopschilder avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.