---
title: OpenClient
project: Total.js V4
version: 0.0.99
section: reference
language: es
license: MIT
url: https://totaljs.es/docs/es/total-js-v4/0-0-99/reference/openclient/
updated: 2026-11-18T16:57:39.578Z
---

# OpenClient

> OPENCLIENT(): un cliente WebSocket para mensajes a otros servicios.

## API

### OPENCLIENT(url, [id])

Type: `method`

Abre un cliente WebSocket compartido para mensajes de petición y respuesta.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | La dirección del servidor (`ws://`, `wss://`, `http://` o `https://`). |
| `id` | `String` | no | `GUID(10)` | El identificador del cliente. |

Returns: `Object` El cliente.

Se conecta a `url` por WebSocket (`http` pasa a ser `ws`, `https` pasa a ser `wss`) y se reconecta automáticamente. Los clientes con la misma URL comparten una conexión; la conexión se cierra cuando se elimina su último cliente. Pasar un `id` existente elimina primero el cliente anterior con ese id.

El cliente tiene estos métodos:

- `send(msg, [callback], [timeout])` envía un mensaje JSON. Con un callback, el mensaje recibe un `callbackid`, y el callback `function(err, response)` se ejecuta cuando el servidor responde con el mismo `callbackid` (campos `error` y `response`). Mientras la conexión está caída, reintenta cada 500 ms, hasta diez veces, y luego llama al callback con `offline`. Un timeout da `timeout`.
- `rpc(msg, [timeout])` lo mismo que `send()`, pero devuelve una promesa.
- `cmd(msg, [timeout])` envía sin esperar respuesta.
- `message(fn)`, `error(fn)` y `online(fn)` definen manejadores. `online(meta)` se ejecuta con el mensaje `init` del servidor, y con `null` cuando se cierra la conexión.
- `close()` o `remove()` elimina el cliente.

Propiedades: `id`, `url`, `connected`, `meta` (el último mensaje `init`).

Example: Preguntar al servidor
```js
var client = OPENCLIENT('https://service.example.com/');

client.message(function(msg) {
	console.log('Message:', msg);
});

client.send({ type: 'ping' }, function(err, response) {
	console.log(err, response);
}, 5000);

var stats = await client.rpc({ type: 'stats' }, 5000);
```
