PHP Luminova: Run an HTTP Socket Server
Start the HTTP socket server and register handlers for local HTTP or WebSocket communication.
Run this example as a local CLI process in its own terminal. Use a loopback address for the first test and choose an available port. Start the server before testing a handler from another terminal or a browser. See the Real-Time Communication using HTTP Socket Server.
Getting Started
Prerequisites
- Luminova Framework: Make sure you've installed the framework and its dependencies.
- PHP CLI with PCNTL. Check that
php -mlistspcntlbefore you start.
Setting Up the Server
- Autoload Dependencies: Before running the server, you'll need to load the composer dependencies. This is handled automatically by Luminova's
Boot.php.
use Luminova\Boot;
require __DIR__ . '/system/Boot.php';
Boot::cli(); // Initialize CLI environment- Create the Server: Instantiate the
Http Serverclass by specifying theIPaddress andport. For example, we’re binding it to127.0.0.1on port8081.
use Luminova\Http\Client\Server;
$server = new Server('127.0.0.1', 8081, false);Note: Here we passed the third parameter
$autostarttofalse, so we can manually handle start and shutdown with commands.
- Server Control Commands: In this example the script supports options to start, stop, or kill the server via command line flags.
# Start the server
php server --start
# Stop the server
php server --stop
# Force stop the server and kill the port process
php server --stop --killCreate the Server Script
Save the example as server in the project root. In a PHP file, place <?php before the code below.
// /server
use Luminova\Boot;
use Luminova\Http\Client\Server;
use Luminova\Http\Message\Response;
use Luminova\Interface\RequestInterface;
/**
* Autoload composer dependencies
*/
require __DIR__ . '/system/Boot.php';
// Initialize the CLI environment
Boot::cli();
// Create a new instance of the HTTP server
$server = new Server('127.0.0.1', 8081, false);
$options = getopt("", [
"start::",
"stop::",
"kill::"
]);
// Enable debug mode
$server->debug();
// Enable WebSocket support
$server->websocket();
// Set up connection handler
$server->connect(function(array $client) {
logger('debug', "Connected: ", $client);
});
// Set up disconnection handler
$server->disconnect(function(array $client) {
logger('debug', "Disconnected: ", $client);
});
// Set up error handler
$server->error(function(RequestInterface $request, string $type) {
return new Response(
body: "Form submitted!",
statusCode: 200,
headers: ['Content-Type' => 'text/plain']
);
});
// Set up GET route for the root URL
$server->get('/', function(RequestInterface $request) {
return new Response(
body: "<h1>Hello, World!</h1>",
statusCode: 200,
headers: ['Content-Type' => 'text/html']
);
});
// Set up GET route for user endpoint
$server->get('/user', function(RequestInterface $request) {
$name = $request->getGet('name', 'Guest');
return new Response(
body: "Hello, {$name}",
statusCode: 200,
headers: ['Content-Type' => 'text/plain']
);
});
// Set up POST route for form submission
$server->post('/submit', function(RequestInterface $request) {
return new Response(
body: "Form submitted!",
statusCode: 200,
headers: ['Content-Type' => 'text/plain']
);
});
$server->get('/foo', function(RequestInterface $request) {
return "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\Response string from foo!";
});
// Set up WebSocket message acceptance handler
$server->accept('/ws(?:/.*)?', function(string $message, array $payload) {
logger('debug', "Websocket Message: {$message}", $payload);
});
// Set up socket.io message acceptance handler
$server->accept('/socket.io(?:/.*)?', function(string $message, array $payload) {
logger('debug', "SocketIO Message: {$message}", $payload);
});
// Start the server
if(isset($options['start'])){
$server->start()->run(sleep: 0);
}
if(isset($options['stop'])){
$server->shutdown(isset($options['kill']));
}
// Exit the script
exit(0);Test the GET Route
With the server running, open a second terminal and make a request.
curl 'http://127.0.0.1:8081/user?name=Sam'The response body is Hello, Sam. Stop the foreground server with Ctrl+C.