Spanish Total.js
English
Documentation For AI agents
Contents

OpenClient

OPENCLIENT() and the methods of a client.

.md GitHub
On this page

API

method

OPENCLIENT(url, [id])

Opens a WebSocket client for request-response messages.
ParameterTypeDescription
url String The WebSocket URL (ws://, wss://, http:// or https://).
id? String A client identifier; a random one is generated when omitted.

Returns Object The client.

Connects to a WebSocket endpoint (http in the URL is changed to ws). All clients with the same URL share one connection, which reconnects when the server goes away. A client created with an id that already exists replaces the old client.

When the server sends a message { type: 'init', id: '...' }, every client gets client.meta (the message), client.type (its id) and its online() handler runs. client.connected is true while the connection is open. Messages that are not answers to rpc() go to the message() handler.

Connect
var client = OPENCLIENT('wss://service.example.com/$client/', 'myservice');

client.online(function(meta) {
	console.log(meta ? 'Connected to ' + meta.id : 'Offline');
});

client.message(function(msg) {
	console.log(msg);
});
method

client.send(msg, [callback], [timeout])

Sends a message to the server.
ParameterTypeDescription
msg Object The message.
callback? Function function(err, response).
timeout? Number Milliseconds to wait for the answer.

Returns Object The client, for chaining.

Sends msg (an object, as JSON). With callback the message gets a callbackid and the callback runs with (err, response) when the server answers { callbackid, error, response }, with 'timeout' after timeout milliseconds, or with 'offline' when there is no connection. While the connection is closed the message is retried every 500 ms, up to ten times.

client.cmd(msg) sends a message without waiting for an answer.

Send and wait
client.send({ type: 'users' }, function(err, response) {
	console.log(err, response);
}, 5000);
Command
client.cmd({ type: 'refresh' });
method

client.rpc(msg, [callback], [timeout])

Sends a message and waits for the answer.
ParameterTypeDescription
msg Object The message.
callback? Function function(err, response).
timeout? Number Milliseconds to wait.

Returns Promise|Object A promise without a callback.

The same as send() with a callback. Without a callback it returns a promise that resolves with the response and rejects with the error ('timeout', 'offline' or the error of the answer).

await
var users = await client.rpc({ type: 'users', page: 1 }, 5000);
method

client.message(fn)

Sets the handler of incoming messages.
ParameterTypeDescription
fn Function function(msg).

Returns Object The client, for chaining.

Runs for every message from the server that is not an answer to send() or rpc() and is not the init message.

Messages
client.message(function(msg) {
	if (msg.type === 'notify')
		console.log(msg.body);
});
method

client.online(fn)

Sets the handler of online and offline changes.
ParameterTypeDescription
fn Function function(meta).

Returns Object The client, for chaining.

Runs with the init message of the server when the service is ready, and with null when the connection closes.

Online state
client.online(function(meta) {
	console.log(meta ? 'online' : 'offline');
});
method

client.error(fn)

Sets the handler of connection errors.
ParameterTypeDescription
fn Function function(err).

Returns Object The client, for chaining.

Runs with the error message when the connection fails, and with (code, message) when the server closes it with a code of 4000 or more. Pending rpc() calls are rejected with the error.

Errors
client.error(function(err) {
	console.log('OPENCLIENT error', err);
});
method

client.close()

Removes the client.

client.remove() is an alias. Pending callbacks of this client receive 'offline'. The shared connection is closed when it has no other clients.

Close
client.close();
Made in Europe