Websocket
in package
The publishing side of the WebSocket service, for PHP code.
A module that just saved something, a cron job, a CLI utility: anything that runs on the server can notify the connected browsers with one call.
Websocket::publish(array('documents'), array('id' => 42), 'updated');
The call speaks the internal /php channel of the service: connect, handshake,
one frame carrying the shared key and the topics, one answer, close. The key is
compared by the service with hash_equals() and never travels in a URL, where
every proxy log would keep it.
A page must never wait for this: the whole exchange is bounded by self::TIMEOUT, and every failure returns false while the caller goes on. When the service is not enabled, or has no key, the call does not even open a socket.
Tags
Table of Contents
Constants
- DEADLINE : mixed = 1.5
- Seconds granted to the whole exchange.
- TIMEOUT : mixed = 0.5
- Seconds granted to the connection and to each read.
- TIMEOUT_USEC : mixed = 500000
- Microseconds granted to each read: stream_set_timeout() wants an integer.
Methods
- broadcast() : int|false
- Sends an event to every open connection, whether it subscribed to anything or not.
- enabled() : bool
- ensure_running() : bool
- Starts the service when nobody uses it, and returns once it answers.
- host() : string
- is_running() : bool
- Tells whether the service answers, without publishing anything.
- ping_interval() : int
- port() : int
- public_url() : string
- Public URL a browser must use, with its scheme: `wss://…` when the page is served over https or when Config_websocket::PUBLIC_URL says so, `ws://host:port/` otherwise. Empty when the service is disabled.
- publish() : int|false
- Publishes an event to the clients subscribed to one of the topics.
- scrub() : string
- Masks credentials in a log line.
- start_url() : string
- URL a page posts to when it needs the service started.
- url_from() : string
- Computes the URL a browser must use, from the instance base URL.
Constants
DEADLINE
Seconds granted to the whole exchange.
public
mixed
DEADLINE
= 1.5
TIMEOUT
Seconds granted to the connection and to each read.
public
mixed
TIMEOUT
= 0.5
TIMEOUT_USEC
Microseconds granted to each read: stream_set_timeout() wants an integer.
public
mixed
TIMEOUT_USEC
= 500000
Methods
broadcast()
Sends an event to every open connection, whether it subscribed to anything or not.
public
static broadcast([mixed $payload = null ][, string $event = 'message' ][, array<string|int, mixed> $only = array() ]) : int|false
The frame carries no topic: the client hands it to its catch-all handler
(on_message). It is meant for the few messages addressed to the socket itself
— a forced reload, a revoked session, a maintenance notice — not for application
content, which belongs to a topic where the page chose to listen. There is no
queue: a client that connects after the call never receives it.
Parameters
- $payload : mixed = null
-
Anything JSON encodable; delivered as is.
- $event : string = 'message'
-
Event name, 'message' by default.
- $only : array<string|int, mixed> = array()
-
Same delivery filter as publish(), so a broadcast can still be restricted to the administrators or to one group.
Return values
int|false —Number of frames queued by the service, false on failure.
enabled()
public
static enabled() : bool
Return values
bool —True when the instance runs the service.
ensure_running()
Starts the service when nobody uses it, and returns once it answers.
public
static ensure_running([float $timeout = self::DEADLINE ]) : bool
The service does not run permanently: it is started by whichever page is
about to open a socket, and it stops by itself after IDLE_STOP seconds
without a client. This method is that trigger. It probes first (one TCP
connection, no process), so a running service costs nothing; otherwise it
runs the runner with --start --daemon and waits for the port.
Deliberately synchronous: the runner's --daemon forks and the parent
returns at once, so the call costs the CLI bootstrap (~100 ms) and the
exit code is the real one — 0 started, 3 already running (both a success:
two pages racing must not be an error), 4 disabled by configuration.
Never throws and never breaks a page: a service that cannot be started
leaves the caller with false and one line in the log.
Parameters
- $timeout : float = self::DEADLINE
-
Seconds granted to the startup, probe included.
Return values
bool —True when the port answers.
host()
public
static host() : string
Return values
string —Host to connect to; a service listening on 0.0.0.0 is reachable on the loopback.
is_running()
Tells whether the service answers, without publishing anything.
public
static is_running() : bool
Return values
boolping_interval()
public
static ping_interval() : int
Return values
int —Heartbeat interval, published to the page so it can time its own liveness check on the same rhythm as the service.
port()
public
static port() : int
Return values
int —Port of the service.
public_url()
Public URL a browser must use, with its scheme: `wss://…` when the page is served over https or when Config_websocket::PUBLIC_URL says so, `ws://host:port/` otherwise. Empty when the service is disabled.
public
static public_url() : string
One place computes it, and everything that needs it (the JS constants and the
Content-Security-Policy) reads this method: an instance whose page is served
over https and whose service is announced in ws:// would have its sockets
refused as mixed content, with no error in the PHP log.
Return values
stringpublish()
Publishes an event to the clients subscribed to one of the topics.
public
static publish(string|array<int, string> $topics[, mixed $payload = null ][, string $event = 'message' ][, array<string|int, mixed> $only = array() ]) : int|false
Parameters
- $topics : string|array<int, string>
-
Topic name, or list of names.
- $payload : mixed = null
-
Anything JSON encodable; delivered as is.
- $event : string = 'message'
-
Event name, 'message' by default.
- $only : array<string|int, mixed> = array()
-
Delivery filter, ANDed: ['group'=>5], ['user'=>42], ['groups'=>[3,7]] (all of them), ['admin'=>true], or any key the instance answers through Hub::filter().
Return values
int|false —Number of frames queued by the service, false on failure.
scrub()
Masks credentials in a log line.
public
static scrub(string $line) : string
The service logs addresses, paths and reasons, never a credential by design; an exception message is the one place where one can slip through (a SQL error carrying an OID, a library quoting a header). Masking here rather than at each call site means a new log line cannot undo that rule.
Parameters
- $line : string
Return values
stringstart_url()
URL a page posts to when it needs the service started.
public
static start_url() : string
Empty when the service is disabled, so the JS client has nothing to call
and stays silent instead of firing requests at a file that is not served.
The file is <instance>/start_websocket.php, shipped with the instance
like run_websocket.php.
The name must not begin with the proxy prefix: a router on PathPrefix(/ws)
catches /ws_start.php as well and sends it to the socket listener, where
it answers 502 with nothing in the log. Hence start_…, never ws….
Return values
stringurl_from()
Computes the URL a browser must use, from the instance base URL.
public
static url_from(string $base_url, int $port[, string $public = '' ]) : string
Pure function, so the rule is tested without an instance: an explicit public
URL wins (it is the only way to announce wss:// behind a proxy), otherwise
the scheme follows the page (https → wss) and the port is the service one.
Parameters
- $base_url : string
-
Config::BASE_URL.
- $port : int
-
Listen port of the service.
- $public : string = ''
-
Config_websocket::PUBLIC_URL, empty to derive.