Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend async I/O API by being able to pass other sockets #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

thekid
Copy link
Member

@thekid thekid commented Dec 3, 2023

This makes it possible to asynchronously execute the following code:

$res->header('Content-Type', 'text/event-stream');
$res->header('Cache-Control', 'no-store');

$stream= $res->stream();

$s= new Socket('127.0.0.1', 8888);
$s->connect();
while (!$s->eof()) {

  // The following line will resume once the socket to become readable,
  // handling other requests while doing so!
  yield 'read' => $s;
  $bytes= addcslashes($s->read(), "\0..\37");

  $stream->write("event: message\n");
  $stream->write("data: Message {$bytes}\n\n");
  $stream->flush();
}

$s->close();
$stream->close();

Instead of a socket, we could imagine using Redis pub/sub with https://github.com/xp-forge/redis:

$sub= new RedisProtocol($dsn)->connect();
$sub->command('SUBSCRIBE', $channel);

$s= $sub->socket();
while (!$s->eof()) {
  yield 'read' => $s;
  [$type, $channel, $message]= $sub->receive();

  // TBI
}

This allows for low-level asynchronous processing. A more elaborate API may be implemented using https://wiki.php.net/rfc/fibers in the future, however, that would require PHP 8.1+, and we still support everything from 7.0 - 8.4!

thekid added a commit to xp-forge/web that referenced this pull request Dec 3, 2023
thekid added a commit to xp-forge/web that referenced this pull request Dec 3, 2023
@thekid
Copy link
Member Author

thekid commented Dec 3, 2023

Forward compatibility in the web API released in https://github.com/xp-forge/web/releases/tag/v3.12.0

@thekid
Copy link
Member Author

thekid commented Dec 9, 2023

Seeing weird timeout errors, including what might be a segfault:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant