Hub
in package
uses
Logs
The event side of the service: who listens to what, and how a PHP script makes something happen for them.
A browser subscribes with ?topic=documents on the handshake, or later with
{"subscribe":["documents"]} (and {"unsubscribe":[...]}) on the API channel.
A PHP script publishes on the internal /php channel: one frame carrying the
shared key, the topics and the payload. The key is compared with
hash_equals(), is never in a URL, and an instance without a key has no
channel at all rather than an open one.
Delivery is per subscription, not per connection: a client subscribed to two of the three published topics receives two frames, each naming its topic. The client therefore never has to know which event matched which of its subscriptions.
Tags
Table of Contents
Constants
- FILTER_KEYS : mixed = array('user', 'group', 'groups', 'admin')
- Filter keys the hub evaluates itself, on the identity of the connection.
- MAX_TOPICS : mixed = 20
- Topics a single connection may hold.
Methods
- __construct() : mixed
- add_log_sink() : void
- Adds a destination. A sink that throws is reported and skipped: a logger must never be the reason a connection dies.
- attach() : array<int, string>
- Registers the topics read from a handshake target.
- authorize() : void
- Sets the access policy of subscriptions.
- can_subscribe() : bool
- channel_enabled() : bool
- deliver() : int
- Sends one frame per matching subscription.
- deliver_broadcast() : int
- Sends the same frame to every open connection, subscribed or not.
- filter() : void
- Declares how to answer a filter key the hub does not know.
- forget() : void
- Drops every subscription of a connection.
- handle_control() : bool
- Handles a subscribe/unsubscribe message.
- handle_publish() : array<string|int, mixed>
- Handles one publishing frame received on the /php channel.
- has_log_sinks() : bool
- initial_topics() : array<int, string>
- Reads the topics of a handshake target: `/?topic=documents&topic=news`, the `?topic=documents,news` form, and no topic at all are all accepted.
- key_matches() : bool
- Compares the key of a publishing frame with the configured one.
- matches() : bool
- Evaluates a delivery filter against one connection.
- reauthorize() : array<int, string>
- Drops the subscriptions the policy no longer allows.
- subscribe() : bool
- Adds one subscription, respecting the cap and the policy.
- topics() : array<int, string>
- unsubscribe() : void
- valid_topic() : bool
- A topic is a short name: letters, digits and `_`, `-`, `.`, `:` — nothing that could be confused with the protocol or with a path.
Constants
FILTER_KEYS
Filter keys the hub evaluates itself, on the identity of the connection.
public
mixed
FILTER_KEYS
= array('user', 'group', 'groups', 'admin')
MAX_TOPICS
Topics a single connection may hold.
public
mixed
MAX_TOPICS
= 20
Methods
__construct()
public
__construct([array<string|int, mixed> $settings = array() ]) : mixed
Parameters
- $settings : array<string|int, mixed> = array()
-
['key'=>string, 'log'=>callable]
add_log_sink()
Adds a destination. A sink that throws is reported and skipped: a logger must never be the reason a connection dies.
public
add_log_sink(mixed $callback) : void
Parameters
- $callback : mixed
attach()
Registers the topics read from a handshake target.
public
attach(Connection $connection, array<int, string> $topics) : array<int, string>
Parameters
- $connection : Connection
- $topics : array<int, string>
Return values
array<int, string> —Topics actually kept.
authorize()
Sets the access policy of subscriptions.
public
authorize(callable|null $callback) : void
The service authenticates nobody by itself: the bridge knows who a connection is (after a successful OID check) and the instance decides what that identity may listen to. Null restores "every topic is open".
Parameters
- $callback : callable|null
-
function (Connection $connection, string $topic): bool
can_subscribe()
public
can_subscribe(Connection $connection, mixed $topic) : bool
Parameters
- $connection : Connection
- $topic : mixed
Return values
bool —True when the policy accepts this topic for this connection.
channel_enabled()
public
channel_enabled() : bool
Return values
bool —True when the internal channel has a key and can be used.
deliver()
Sends one frame per matching subscription.
public
deliver(array<int, Connection> $connections, array<int, string> $topics, string $event, mixed $payload[, array<string|int, mixed> $only = array() ][, mixed &$skipped = 0 ]) : int
Parameters
- $connections : array<int, Connection>
- $topics : array<int, string>
-
Topics published.
- $event : string
-
Event name.
- $payload : mixed
-
Anything JSON encodable.
- $only : array<string|int, mixed> = array()
- $skipped : mixed = 0
Return values
int —Number of frames queued.
deliver_broadcast()
Sends the same frame to every open connection, subscribed or not.
public
deliver_broadcast(array<int, Connection> $connections, string $event, mixed $payload[, array<string|int, mixed> $only = array() ][, int &$skipped = 0 ][, Connection|null $publisher = null ]) : int
The frame carries no topic on purpose: it belongs to no channel, and the
client hands it to its catch-all handler (on_message). It is the only
delivery that ignores the subscription index, so it is meant for the few
messages addressed to the socket itself — a forced reload, a revoked
session, a maintenance notice — never for application content, which
belongs to a topic where the page chose to listen.
Parameters
- $connections : array<int, Connection>
-
Every open connection.
- $event : string
-
Event name.
- $payload : mixed
-
Anything JSON encodable.
- $only : array<string|int, mixed> = array()
-
Optional delivery filter, same keys as publish().
- $skipped : int = 0
-
Set to the number of connections the filter removed.
- $publisher : Connection|null = null
-
The /php connection that asked: it is not an audience, and reading its own broadcast as the answer of the channel would break the caller.
Return values
int —Number of frames queued.
filter()
Declares how to answer a filter key the hub does not know.
public
filter(callable|null $callback) : void
The instance keeps the rules that depend on its own data ("who may read document 51"): the hub hands it the identity and lets it decide. A key with no handler is refused at publication, never delivered to everybody.
Parameters
- $callback : callable|null
-
function (Connection, string $key, mixed $value): bool
forget()
Drops every subscription of a connection.
public
forget(Connection $connection) : void
Called when the socket closes: an id left in the index would make the next publish queue a frame for a connection that no longer exists. The reverse map is what keeps this proportional to the topics of that connection only.
Parameters
- $connection : Connection
handle_control()
Handles a subscribe/unsubscribe message.
public
handle_control(Connection $connection, string $payload) : bool
Parameters
- $connection : Connection
- $payload : string
-
Raw frame payload.
Return values
bool —True when the message was a subscription and has been answered.
handle_publish()
Handles one publishing frame received on the /php channel.
public
handle_publish(string $payload, array<int, Connection> $connections[, Connection|null $publisher = null ]) : array<string|int, mixed>
Parameters
- $payload : string
-
Raw frame payload of the sender.
- $connections : array<int, Connection>
-
Every open connection, keyed by id.
- $publisher : Connection|null = null
Return values
array<string|int, mixed> —Answer envelope to send back to the sender.
has_log_sinks()
public
has_log_sinks() : bool
Return values
bool —True when the lines go to an application sink rather than the instance log.
initial_topics()
Reads the topics of a handshake target: `/?topic=documents&topic=news`, the `?topic=documents,news` form, and no topic at all are all accepted.
public
initial_topics(string $path) : array<int, string>
Parameters
- $path : string
-
Request target of the handshake.
Return values
array<int, string> —Valid, deduplicated topic names.
key_matches()
Compares the key of a publishing frame with the configured one.
public
key_matches(string $key) : bool
Parameters
- $key : string
Return values
boolmatches()
Evaluates a delivery filter against one connection.
public
matches(Connection $connection, array<string|int, mixed> $only) : bool
Keys are ANDed: every one of them must hold. They only ever narrow the audience — a filter can never make a connection receive a topic it did not subscribe to.
Parameters
- $connection : Connection
- $only : array<string|int, mixed>
Return values
boolreauthorize()
Drops the subscriptions the policy no longer allows.
public
reauthorize(Connection $connection) : array<int, string>
Called after the identity has been revalidated: a connection keeps its socket for hours, and a right taken away in the database must reach it before it is closed.
Parameters
- $connection : Connection
Return values
array<int, string> —Topics removed.
subscribe()
Adds one subscription, respecting the cap and the policy.
public
subscribe(Connection $connection, string $topic) : bool
Parameters
- $connection : Connection
- $topic : string
Return values
bool —False when the topic is invalid, refused or the connection is full.
topics()
public
topics(Connection $connection) : array<int, string>
Parameters
- $connection : Connection
Return values
array<int, string> —Topics of this connection, in subscription order.
unsubscribe()
public
unsubscribe(Connection $connection, string $topic) : void
Parameters
- $connection : Connection
- $topic : string
valid_topic()
A topic is a short name: letters, digits and `_`, `-`, `.`, `:` — nothing that could be confused with the protocol or with a path.
public
static valid_topic(mixed $topic) : bool
Parameters
- $topic : mixed