Localzet Server and related projects documentation

Usage and API

Lifecycle

GatewayServer provides these protected callbacks:

  • onClientConnect(string $id, ConnectionInterface $connection) after registration;
  • onClientMessage(string $id, ConnectionInterface $connection, mixed $message) for incoming messages;
  • onClientClose(string $id, ConnectionInterface $connection) after the record is removed.

IDs are random 16-byte values encoded as hex. Registering the same connection object again preserves its ID. connections() returns the ConnectionManager owned by this handler.

UIDs and groups

$manager = $this->connections();
$manager->bindUid($connection, 'user-42');
$manager->joinGroup($connection, 'room-7');

$manager->sendToUid('user-42', ['event' => 'notice']);
$manager->sendToGroup('room-7', ['event' => 'message']);
$manager->sendToAll(['event' => 'maintenance']);

ConnectionManager also provides idOf(), get(), unbindUid(), leaveGroup(), and sendToClient(). UID/group/all sends return the number of successful send() calls in this process; sending to one ID returns bool. Bind/join require a registered connection; a UID/group that is empty after trimming raises InvalidArgumentException. Closing a connection removes its UID and group memberships.

Distributed delivery with Cluster

Install localzet/cluster as a separate dependency and configure its Register, Gateway, and Business processes with matching network settings and secret. The adapter then delegates to Cluster's public client:

use Triangle\MultiConnect\ClusterConnectionManager;

$cluster = new ClusterConnectionManager();
$cluster->sendToUid('user-42', ['event' => 'notice']);
$cluster->sendToGroup('room-7', ['event' => 'message']);

The adapter forwards send/bind/unbind/join/leave operations to localzet\Cluster\Client. sendToClient() returns Cluster's result, sendToGroup() preserves its nullable bool, and sendToUid()/sendToAll() return nothing. These return values do not confirm that a client read a message.