$base = new EventBase(); $socket = stream_socket_server('tcp://0.0.0.0:9000', $errno, $errstr); $event = new Event($base, $socket, Event::READ | Event::PERSIST, function($socket) { $conn = stream_socket_accept($socket); //Read data from socket $data = fread($conn, 1024); echo "Received data: ".$data; }); $event->add(); $base->loop();
$base = new EventBase(); $fd = fopen('file.txt', 'r'); $event = new Event($base, fileno($fd), Event::READ | Event::PERSIST, function($fd) { //Read data from file $data = fread($fd, 1024); echo "Read data: ".$data; }); $event->add(); $base->loop();In this example, we open a file and attach the READ event to its file descriptor. When data is available to be read from the file, the READ event is triggered and the function attached to it reads the data from the file. Package Library: The PHP Event library.