Class: H_websocket

H_websocket

Client of the helPHP WebSocket service: one socket, module calls, topic subscriptions.

The service is off by default (H_constants.websocket.enabled). This class does nothing until a page creates an instance, and creating one while the service is disabled only produces a closed connection: no error, no request.

Two uses, and they do not need the same credentials:
- subscribe(topic, callback) receives server events. Same origin, no token: the handshake is checked against the instance origins.
- request(module, action, data, callback) calls the REST API over the socket, exactly like api/index.php. It needs an OID, obtained the way any API client obtains one (the connection command); a browser page of the application does not carry one by default, so it uses subscriptions and leaves the module calls to ajax.

The socket is multiplexed: every request carries an id, and the answer with the same id is what the callback receives. A dropped connection is reopened with an exponential backoff and jitter, so a server restart does not bring every browser back at the same millisecond, and the subscriptions are sent again on the new socket — the server keeps them per connection, not per user.

The protocol ping/pong of RFC 6455 is answered by the browser itself; nothing here has to keep it alive. settings.watchdog is only for the cases where a network dies without closing the socket: it forces a reconnect when no frame arrived for that many milliseconds (0, the default, disables it).

The service is not always up: it starts with the first client and stops once nobody is connected. A browser cannot start a process, so after settings.start_after failed reconnections the client posts to H_constants.websocket.start, which starts it, then reconnects at once. That is the recovery path; the page that knows it will open a socket calls helPHP\libs\Websocket::ensure_running() while rendering, so the first connection does not have to fail.

new H_websocket()

websocket.js, line 26
See:
  • helPHP\libs\Websocket\Hub, helPHP\libs\Websocket\Bridge
Examples
let ws = new H_websocket({name: 'documents'});
     ws.subscribe('documents', (event) => {
         if (event.event === 'updated') reload(event.payload.id);
     });
let api = new H_websocket({name: 'api', url: 'wss://…', oid: my_oid});
     api.request('document', 'read', {id: 12}, (answer) => {
         if (answer.status === 200) display(answer.data);
     });

Members

staticH_websocket.instancesObject.<string, H_websocket>

Every connection of the page, by name: it is h.ws once init.js has run.

Methods

staticH_websocket.get(settings){H_websocket}

websocket.js, line 427
Returns the connection of a page, creating it if needed.
Name Type Description
settings Object optional
Returns:
Type Description
H_websocket

close(){H_websocket}

websocket.js, line 183
Closes the socket for good: no reconnection, nothing queued.
Returns:
Type Description
H_websocket this

connect(){H_websocket}

websocket.js, line 135
Opens the socket. Safe to call twice.
Returns:
Type Description
H_websocket this

dispatch(raw)

websocket.js, line 280
Routes one frame: an answer to a request first, then a topic event, then the catch-all callback of the settings.
Name Type Description
raw string

ensure_service()

websocket.js, line 355
Asks the instance to start the service, then reconnects at once. The service idle-stops itself, so a socket that was open yesterday is not open today; the reconnection alone would retry against a closed port for ever. The page cannot start a process, the server can. Bounded: one request every 5 s, and only after settings.start_after failed reconnections, so a network outage does not turn into a stream of requests.

fail_pending()

websocket.js, line 336
Answers every waiting request with a synthetic failure, rather than leaving a page waiting forever after a dropped connection.

flush_queue()

websocket.js, line 324
Sends what was queued while the socket was down.

is_open(){boolean}

websocket.js, line 271
Returns:
Type Description
boolean True when the socket can send right now.

request(module, action, data, callback){number}

websocket.js, line 221
Calls a module action over the socket, like api/index.php does over HTTP.
Name Type Default Description
module string
action string
data Object optional
callback function null optional Receives the answer, correlated by its id.
Returns:
Type Description
number The id of the call.

schedule_reconnect()

websocket.js, line 383
Schedules a reconnection with an exponential backoff and a jitter, so a server restart does not bring every tab back at the same moment.

send(payload){H_websocket}

websocket.js, line 203
Sends a raw message, or queues it until the socket is ready.
Name Type Description
payload Object
Returns:
Type Description
H_websocket this

send_subscriptions()

websocket.js, line 316
Sends the subscriptions of this connection again: the server keeps them per socket, so a new socket starts with none.

start_watchdog()

websocket.js, line 402
Starts the optional watchdog: no frame for settings.watchdog ms means the socket is dead even if the browser did not notice.

stop_watchdog()

websocket.js, line 415
Stops the watchdog timer.

subscribe(topic, callback){H_websocket}

websocket.js, line 237
Listens to a server event.
Name Type Description
topic string
callback function Receives {topic, event, payload}.
Returns:
Type Description
H_websocket this

unsubscribe(topic, callback){H_websocket}

websocket.js, line 254
Stops listening to a topic. The server forgets it as soon as no callback is left for it, so a page that re-subscribes a thousand times still receives one frame per event.
Name Type Description
topic string
callback function optional One callback, or every callback of the topic.
Returns:
Type Description
H_websocket this

H_websocket

new H_websocket(settings)

websocket.js, line 95
Name Type Description
settings Object
Name Type Default Description
name string default optional Name of this connection in h.ws.
url string optional Service URL, H_constants.websocket.url by default.
oid string optional Token of the API, for request().
reconnect boolean true optional Reopen the socket when it closes.
max_attempts number 0 optional Stop after this many reconnections (0 = never).
base_delay number 300 optional First delay of the backoff, in ms.
max_delay number 5000 optional Cap of the backoff, in ms.
watchdog number 0 optional Reconnect when no frame arrived for this many ms.
start_url string optional URL that starts the service, H_constants.websocket.start by default.
start_after number 2 optional Failed reconnections before asking for a start (0 = never).
on_open function optional Called when the socket is ready.
on_close function optional Called when it closes, before the reconnection.
on_error function optional Called on a socket error.
on_message function optional Called with every message no callback consumed.

Members

staticH_websocket.instancesObject.<string, H_websocket>

Every connection of the page, by name: it is h.ws once init.js has run.

Methods

staticH_websocket.get(settings){H_websocket}

websocket.js, line 427
Returns the connection of a page, creating it if needed.
Name Type Description
settings Object optional
Returns:
Type Description
H_websocket

close(){H_websocket}

websocket.js, line 183
Closes the socket for good: no reconnection, nothing queued.
Returns:
Type Description
H_websocket this

connect(){H_websocket}

websocket.js, line 135
Opens the socket. Safe to call twice.
Returns:
Type Description
H_websocket this

dispatch(raw)

websocket.js, line 280
Routes one frame: an answer to a request first, then a topic event, then the catch-all callback of the settings.
Name Type Description
raw string

ensure_service()

websocket.js, line 355
Asks the instance to start the service, then reconnects at once. The service idle-stops itself, so a socket that was open yesterday is not open today; the reconnection alone would retry against a closed port for ever. The page cannot start a process, the server can. Bounded: one request every 5 s, and only after settings.start_after failed reconnections, so a network outage does not turn into a stream of requests.

fail_pending()

websocket.js, line 336
Answers every waiting request with a synthetic failure, rather than leaving a page waiting forever after a dropped connection.

flush_queue()

websocket.js, line 324
Sends what was queued while the socket was down.

is_open(){boolean}

websocket.js, line 271
Returns:
Type Description
boolean True when the socket can send right now.

request(module, action, data, callback){number}

websocket.js, line 221
Calls a module action over the socket, like api/index.php does over HTTP.
Name Type Default Description
module string
action string
data Object optional
callback function null optional Receives the answer, correlated by its id.
Returns:
Type Description
number The id of the call.

schedule_reconnect()

websocket.js, line 383
Schedules a reconnection with an exponential backoff and a jitter, so a server restart does not bring every tab back at the same moment.

send(payload){H_websocket}

websocket.js, line 203
Sends a raw message, or queues it until the socket is ready.
Name Type Description
payload Object
Returns:
Type Description
H_websocket this

send_subscriptions()

websocket.js, line 316
Sends the subscriptions of this connection again: the server keeps them per socket, so a new socket starts with none.

start_watchdog()

websocket.js, line 402
Starts the optional watchdog: no frame for settings.watchdog ms means the socket is dead even if the browser did not notice.

stop_watchdog()

websocket.js, line 415
Stops the watchdog timer.

subscribe(topic, callback){H_websocket}

websocket.js, line 237
Listens to a server event.
Name Type Description
topic string
callback function Receives {topic, event, payload}.
Returns:
Type Description
H_websocket this

unsubscribe(topic, callback){H_websocket}

websocket.js, line 254
Stops listening to a topic. The server forgets it as soon as no callback is left for it, so a page that re-subscribes a thousand times still receives one frame per event.
Name Type Description
topic string
callback function optional One callback, or every callback of the topic.
Returns:
Type Description
H_websocket this