I am trying to insert a record inside database coming to a react socket server. I am lost on how to do my operation in a non blocking way
$loop = Factory::create();
$server = new Server('127.0.0.1:4040', $loop);
$database = new Database();
$server->on('connection', function(ConnectionInterface $conn) use ($database) {
$conn->write('Welcome, you can start writing your notes now...');
$conn->on('data', function($data) use ($conn, $database) {
$database->write($data);
$conn->write('I am supposed to execute before database write');
});
});
$loop->run();
The write
method in database has a sleep(10)
seconds before executing the sql statement. So I am expecting the next message I am supposed to..
should be printed immediately.
My expectation was that when ever there is a I/O operation, the operation will be moved to Event Table and don't block the call stack. As per the definition of event loop and non blocking.
How can I perform the same operation in non blocking way.
Thanks