HelPHP

websocket_install.php

Installs the WebSocket service of one instance: configuration, key, start, and a real end-to-end check.

php utils/websocket_install.php --instance=/home/default/ --public-url=wss://example.com/ws php utils/websocket_install.php --instance=/home/default/ --idle=300 php utils/websocket_install.php --instance=/home/default/ --dry-run

What it does, in order:

  1. checks the instance can host the service (runner, config, log directory, PHP),
  2. writes config/websocket.php (copying install/websocket.php.dist when absent), enabling it, keeping the values already there unless --force,
  3. generates the shared key when there is none — without a key the publishing channel is closed and publish() returns false in silence,
  4. writes IDLE_STOP and PHP_BIN: the first ends the process when nobody is connected, the second is the CLI binary a page may start (never PHP_BINARY, which is php-fpm under FPM),
  5. starts the service and asks it for its status,
  6. opens a real socket, subscribes, publishes, and checks the frame arrives.

No supervisor is installed: the service is started by the page that needs it (Websocket::ensure_running(), start_websocket.php) and stops by itself. Nothing has to survive a reboot, because nothing runs when nobody is connected.

The check runs in a child process: PHP constants cannot change in a running process, so the freshly written configuration is only visible to a new one.

Idempotent: running it again keeps the key and the values already set. Nothing is written with --dry-run.

Options: --instance=PATH instance root (default: $HELHP_INSTANCE) --host=ADDR listen address (default: kept, else 127.0.0.1) --port=N listen port (default: kept, else 8090) --public-url=URL URL a browser uses, e.g. wss://example.com/ws — required behind a proxy, which is the normal production setup --idle=SECONDS stop the service after that long without a client (default: kept, else 300; 0 keeps it up for ever) --php-bin=PATH PHP CLI binary a page starts the service with (default: kept, else PHP_BINDIR/php) --user=NAME account the service runs as (default: www-data) --force regenerate the key and overwrite host/port even if set --no-start configure only, do not start nor check --dry-run print what would change, write nothing --help

Exit codes: 0 done, 1 failure, 2 bad invocation.

Tags
see

install/websocket-deployment.md in the instance

Table of Contents

Functions

helphp_ws_say()  : mixed
helphp_ws_fail()  : mixed
helphp_ws_config_value()  : array<string|int, mixed>
Reads a constant value out of a config file, without including it (the class may already be loaded with older values).
helphp_ws_config_ensure()  : int
Adds the constants a previous installer did not know, before the closing brace.
helphp_ws_send()  : void
Sends one masked text frame, as a browser does.
helphp_ws_read()  : array<string|int, mixed>|null
Reads one frame, answering pings on the way. Returns [opcode, payload].
helphp_ws_probe()  : int
End-to-end check: a real socket subscribes, a real publish is delivered.

Functions

helphp_ws_say()

helphp_ws_say(mixed $status, mixed $text) : mixed
Parameters
$status : mixed
$text : mixed

helphp_ws_fail()

helphp_ws_fail(mixed $text) : mixed
Parameters
$text : mixed

helphp_ws_config_value()

Reads a constant value out of a config file, without including it (the class may already be loaded with older values).

helphp_ws_config_value(string $file, string $name) : array<string|int, mixed>
Parameters
$file : string
$name : string
Return values
array<string|int, mixed> —

[found, raw value]

helphp_ws_config_ensure()

Adds the constants a previous installer did not know, before the closing brace.

helphp_ws_config_ensure(string $file, array<string, string> $lines) : int

Utils::write_in_config() replaces an existing const NAME = …; line but never creates one: an instance configured before IDLE_STOP existed would keep a config the runner reads with defaults, and the file would lie about it.

Parameters
$file : string

Config file.

$lines : array<string, string>

Constant name => value, already rendered.

Return values
int —

Number of lines added.

helphp_ws_send()

Sends one masked text frame, as a browser does.

helphp_ws_send(resource $socket, string $text) : void
Parameters
$socket : resource
$text : string

helphp_ws_read()

Reads one frame, answering pings on the way. Returns [opcode, payload].

helphp_ws_read(resource $socket) : array<string|int, mixed>|null
Parameters
$socket : resource
Return values
array<string|int, mixed>|null —

Null when the socket is closed or silent.

helphp_ws_probe()

End-to-end check: a real socket subscribes, a real publish is delivered.

helphp_ws_probe(string $instance) : int
Parameters
$instance : string
Return values
int —

0 when the chain works.

On this page

Search results