Member-only story

Integrating a ReactPHP Server in Laravel

Run the server as a daemon

Yoram Kornatzky
2 min readMay 7, 2024
Integrate ReactPHP and Laravel

Create a Laravel command,

php artisan make:command SaleServer --command=bidserver:sale

This command will be a daemon that runs a ReactPHP server.

Calling the 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 Server

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

--

--

Yoram Kornatzky
Yoram Kornatzky

Written by Yoram Kornatzky

Entrepreneur, Auctibles: https://auctibles.com, 25 years of development experience, Ph.D. Computer Science

No responses yet