Skip to content

jasalt/phel-amphp-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Fibers & AMPHP testing with Phel

Testing concurrent programming with PHP 8.1+ Fibers and async libraries available via AMPHP by translating some of the examples to Phel code.

Related discussion in Phel repo where comments are welcome phel-lang/phel-lang#793.

Working examples

Tested on:

  • PHP 8.2.7 (Debian 12)
  • Phel v0.16.1 (dev-main)

Repository is created from https://github.com/phel-lang/cli-skeleton/ template which has more in-depth info on how to run Phel.

AMPHP docs Hello World

composer install
vendor/bin/phel run src/helloworld.phel
# => Hello World from the future!%

amphp/socket library

echo-server.php

vendor/bin/phel run src/socket/echo-server.phel

After startup, connect by running nc localhost 8888, then type something to send message and see it echoed back.

simple-http-server.php

vendor/bin/phel run src/socket/simple-http-server.phel

After startup, open http://127.0.0.1:8888 with web browser or: curl -vvv http://127.0.0.1:8888

amphp/http-server-router hello-world.php

More complete HTTP server example with routing, argument parsing, logging etc.

vendor/bin/phel run src/http-server-router/hello-world.phel

Starts server at http://localhost:1337 (demo route with argument http://localhost:1337/myname ).

amphp/http-server event-source.php

Example with server-sent event stream connection (SSE). Client keeps half-duplex HTTP connection open to server which pushes updates to client.

vendor/bin/phel run src/http-server/event-source.phel

TODO

Channels (amphp/sync)

[$left, $right] = createChannelPair();

$future1 = async(function () use ($left): void {
    echo "Coroutine 1 started\n";
    delay(1); // Delay to simulate I/O.
    $left->send(42);
    $received = $left->receive();
    echo "Received ", $received, " in coroutine 1\n";
});

Pipeline / ConcurrentIterator (amphp/pipeline)

Original template repo readme continues...

About

PHP fibers async network programming examples in Phel

Resources

License

Stars

Watchers

Forks