---
title: OpenClient
project: Total.js V5
version: 0.0.20
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v5/0-0-20/reference/openclient/
updated: 2026-11-18T16:56:49.572Z
---

# OpenClient

> OPENCLIENT() and the methods of a client.

## API

### OPENCLIENT(url, [id])

Type: `method`

Opens a WebSocket client for request-response messages.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | The WebSocket URL (`ws://`, `wss://`, `http://` or `https://`). |
| `id` | `String` | no |  | 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.

Example: Connect
```js
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);
});
```

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

Type: `method`

Sends a message to the server.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `msg` | `Object` | yes |  | The message. |
| `callback` | `Function` | no |  | `function(err, response)`. |
| `timeout` | `Number` | no |  | 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.

Example: Send and wait
```js
client.send({ type: 'users' }, function(err, response) {
	console.log(err, response);
}, 5000);
```

Example: Command
```js
client.cmd({ type: 'refresh' });
```

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

Type: `method`

Sends a message and waits for the answer.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `msg` | `Object` | yes |  | The message. |
| `callback` | `Function` | no |  | `function(err, response)`. |
| `timeout` | `Number` | no |  | 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).

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

### client.message(fn)

Type: `method`

Sets the handler of incoming messages.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | `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.

Example: Messages
```js
client.message(function(msg) {
	if (msg.type === 'notify')
		console.log(msg.body);
});
```

### client.online(fn)

Type: `method`

Sets the handler of online and offline changes.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | `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.

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

### client.error(fn)

Type: `method`

Sets the handler of connection errors.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | `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.

Example: Errors
```js
client.error(function(err) {
	console.log('OPENCLIENT error', err);
});
```

### client.close()

Type: `method`

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.

Example: Close
```js
client.close();
```
