simple-ipc/php-symplib

Synchronous Message Passing Framework for PHP

v1.0.1 2024-08-03 14:02 UTC

This package is auto-updated.

Last update: 2024-09-07 10:09:21 UTC


README

User-friendly, object-oriented API for message passing between PHP processes.

Stream sockets are used as message passing channels. A single server can listen for and handle messages on multiple channels, which can include any mix of Unix domain sockets and TCP/IP (v4 and v6).

Protocol

Messages use a simple wire protocol, where the first 8 bytes of the message specify the length of the rest of the message (unsigned, big endian).

The payload of the message is provided by the user. Serialization format etc. is left to user’s choice.

Getting started

See full working examples in the test/ directory.

Simple server

class MyMessageHandler implements MessageHandler {
    public function handleMessage(string $msg): string {
        return "Hello, client, I received: $msg";
    }
}

$handler = new MyMessageHandler();
$address = new InetSocketAddress('127.0.0.1', 1389, AF_INET);
$server = new SocketStreamsServer([
    new SocketData($address, $handler)
]);

$server->listen();
while (true)
    $server->checkMessages(1);

Simple client

$address = new InetSocketAddress('127.0.0.1', 1389);
$client = new SocketStreamClient($address);

$client->connect();
$client->sendMessage("Hello, server!");
$response = $client->receiveMessage();
echo $response . PHP_EOL;
$client->disconnect();