PHP
Create a Laravel command,
php artisan make:command SaleServer --command=bidserver:sale
This command will be a daemon that runs a ReactPHP server.
The command is called with a HTTP post
from a Livewire component,
Http::asForm()->post(config('auctions.SALE_SERVER_URL') . ':'
. config('auctions.SALE_SERVER_PORT') . '/buy',
[
'auction_id' => $this->auction->id,
'item_id' => $this->item->id,
'user' => $this->user->id,
'price' => $bid_received['price'],
]
);
The command creates a ReactPHP server, that receives these calls.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use React\Http\Server;
use React\Http\Message\Response;
use React\EventLoop\Factory;
use Psr\Http\Message\ServerRequestInterface;
use React\Socket\SocketServer;
class SaleServer extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'bidserver:sale';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Sale bid server';
/**
* Execute the console command.
*/
public function handle()
{
$loop = Factory::create();
$server = new Server(function (ServerRequestInterface $request) {
$path = $request->getUri()->getPath();
$method = $request->getMethod();
//Check if the path and method of the call are correct
if ($path == '/buy' && $method === 'POST') {
//Extract the call parameters
$auction_id = $request->getParsedBody()['auction_id'] ?? null;
$item_id = $request->getParsedBody()['item_id'] ?? null;
$user = $request->getParsedBody()['user'] ?? null;
$bid_price = $request->getParsedBody()['price'] ?? null;
//Broadcast a response
broadcast(new BuyAccepted($auction_id, $item_id, $user))->toOthers();
return Response::plaintext('bid processed')->withStatus(Response::STATUS_OK);
}
return Response::plaintext('Not found')->withStatus(Response::STATUS_NOT_FOUND);
});
$socket = new SocketServer('0.0.0.0:' . config('auctions.SALE_SERVER_PORT'));
$server->listen($socket);
$loop->run();
}
}
Add a daemon running the command:
php artisan bidserver:sale
Yoram Kornatzky