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 settingsObject 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 rawstring -
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 modulestring actionstring dataObject optional callbackfunction 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 payloadObject 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 topicstring callbackfunction 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 topicstring callbackfunction optional One callback, or every callback of the topic. Returns:
Type Description H_websocket this