---
title: Message Service (TMS)
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/message-service-and-apis/
updated: 2026-11-19T16:17:07.153Z
---

# Message Service (TMS)

> TMS functions, the TMS client and their configuration keys.

The functions and configuration keys of TMS (Total.js Message Service), including the TMS client. The guide [Message Service (TMS)](../../guides/message-service-and-apis/) explains how they work together with examples; this page is the exact reference.

Named APIs and the Total.js API service have their own page: [APIs](../apis/).

## API

### $tms : true

Type: `config`

Opens the TMS WebSocket endpoint.

Returns: `Boolean`

Default `false`. When `true`, the app registers a WebSocket route on `$tmsurl` that accepts TMS clients. Turning it on with `LOADCONFIG()` adds the route, and turning it off removes it.

Example: Enable TMS
```js
// /config
$tms           : true
secret_tms     : a-long-random-token
```

### $tmsurl : /$tms/

Type: `config`

The path of the TMS endpoint.

Returns: `String`

Default `/$tms/`. Clients connect to this path with `ws://` or `wss://`. When it changes through `LOADCONFIG()`, the old route is removed and a new one is registered.

### $tmsmaxsize : 256

Type: `config`

The maximum size of one TMS message in kB.

Returns: `Number`

Default `256`. The value is used when the TMS route is registered.

### secret_tms : token

Type: `config`

The token TMS clients must send.

Returns: `String`

Default empty (no token required). A client sends it in the `x-token` header (`TMSCLIENT()` does this for you). A wrong token closes the connection with code `4001` and counts as a failed attempt for that IP address; after more than five attempts the IP address is refused until `$tmsclearblocked` clears the list. When the value changes through `LOADCONFIG()`, all connected clients are disconnected.

### $tmsclearblocked : 60

Type: `config`

How often, in minutes, the list of blocked TMS clients is cleared.

Returns: `Number`

Default `60`. IP addresses blocked for wrong tokens can connect again after the list is cleared.

### NEWPUBLISH(name, schema)

Type: `method`

Declares an event that the app publishes to TMS clients.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the event. |
| `schema` | `String|Object` | yes |  | An inline schema (`'id:UID, name:String'`), `'@name'` of a registered JSON schema, or `null` to remove the event. |

The schema is announced to clients in the `meta` message, and `TMSCLIENT()` uses it to clean the data it receives. Pass `null` as the schema to remove the event. The list of names is sent to connected clients 500 ms after the last change.

Example: Declare and publish
```js
NEWPUBLISH('orders_created', 'id:UID, email:Email, total:Number');

PUBLISH('orders_created', { id: UID(), email: 'a@b.com', total: 100 });
```

### PUBLISH(name, data)

Type: `method`

Sends data of a declared event to subscribed TMS clients.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of an event declared with `NEWPUBLISH()`. |
| `data` | `Object` | yes |  | The data to send. |

The message goes only to connected clients that subscribed to `name`. Nothing is sent when TMS is off, when no client is connected or when `name` was not declared with `NEWPUBLISH()`. The server does not validate `data`.

Example: Publish after a change
```js
NEWACTION('orders_create', {
	input: '*email:Email, *total:Number',
	action: function($, model) {
		model.id = UID();
		PUBLISH('orders_created', model);
		$.success(model.id);
	}
});
```

### NEWSUBSCRIBE(name, schema, [callback])

Type: `method`

Declares a message that TMS clients may send to the app.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the message. |
| `schema` | `String|Object` | yes |  | An inline schema, `'@name'` of a registered JSON schema, or `null`. |
| `callback` | `Function` | no |  | `function(msg, client)`, the same as a `SUBSCRIBE()` handler. |

A message is accepted only when its name has a schema and the data passes it; other messages are dropped. Accepted messages go to the handlers added with `SUBSCRIBE()`. The optional `callback` is added as a handler, the same as calling `SUBSCRIBE(name, callback)`. Pass `null` as the schema to stop accepting the message.

Example: Accept a message
```js
NEWSUBSCRIBE('users_updated', 'id:UID, *name:String', function(msg, client) {
	console.log(msg.response.name);
});
```

### SUBSCRIBE(name, callback)

Type: `method`

Adds a handler for a message sent by TMS clients.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the message. |
| `callback` | `Function` | yes |  | `function(msg, client)`. |

The handler runs for every valid message of `name`. Its first argument is the result of the validation: `msg.response` is the validated data and `msg.error` is `null`. The second argument is the WebSocket client that sent the message. The name is announced to clients in the `meta` message. Declare the schema with `NEWSUBSCRIBE()`, otherwise no message arrives.

Example: Handle a message
```js
SUBSCRIBE('users_updated', function(msg, client) {
	var user = msg.response;
	console.log('User', user.id, 'is now', user.name);
});
```

### UNSUBSCRIBE(name, [callback])

Type: `method`

Removes handlers added with SUBSCRIBE().

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the message. |
| `callback` | `Function` | no |  | The handler to remove. |

Returns: `Boolean` `true` when something was removed.

With `callback`, removes that handler only; without it, removes all handlers of the name. When no handler is left, the name is removed from the `meta` message.

Example: Remove one handler
```js
function onuser(msg) {
	console.log(msg.response);
}

SUBSCRIBE('users_updated', onuser);
// later
UNSUBSCRIBE('users_updated', onuser);
```

### NEWCALL(name, schema, callback)

Type: `method`

Declares a procedure that TMS clients can call.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the call. |
| `schema` | `String|Object` | yes |  | An inline schema, `'@name'` of a registered JSON schema, or `null` to remove the call. |
| `callback` | `Function` | yes |  | `function(data, next, client)`. |

The data of a call is validated first; invalid data is answered with the validation errors and the callback does not run. `next(err, response)` answers the client: an `ErrorBuilder` is sent as its output and any other error as `[{ error: '...' }]`. Pass `null` as the schema to remove the call.

Example: Answer a call
```js
NEWCALL('products_detail', 'id:UID', function(data, next, client) {
	if (data.id === '404')
		next('Product not found');
	else
		next(null, { id: data.id, name: 'Keyboard' });
});
```

### TMSCLIENT(url, [token], [callback])

Type: `method`

Connects the app to a TMS endpoint.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | The address of the endpoint, e.g. `https://app.example.com/$tms/`. |
| `token` | `String` | no |  | The `secret_tms` of the server. |
| `callback` | `Function` | no |  | `function(err, client, meta)`. |

Returns: `WebSocketClient` The client, with the TMS methods below.

Connects to the TMS endpoint of another app and returns a WebSocket client. An `http://` or `https://` address is changed to `ws://` or `wss://`. The token is sent in the `x-token` header. The client reconnects automatically. The callback runs once: with `(null, client, meta)` when the first `meta` message arrives, or with the close code as `err` when the connection closes before that.

Example: Connect
```js
TMSCLIENT('https://orders.example.com/$tms/', CONF.orders_token, function(err, client, meta) {
	client.subscribe('orders_created', function(order) {
		console.log(order);
	});
});
```

### client.subscribe(name, callback)

Type: `method`

Receives events that the server publishes with PUBLISH().

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the event. |
| `callback` | `Function` | yes |  | `function(data)`. |

The list of names is sent to the server (30 ms after the last call and after each connection), so the server pushes only these events. The data is cleaned with the schema the server declared with `NEWPUBLISH()`; events not declared by the server are ignored. You can call it before the connection is ready.

Example: Listen to an event
```js
var client = TMSCLIENT('https://orders.example.com/$tms/', CONF.orders_token);

client.subscribe('orders_created', function(order) {
	console.log(order.id, order.total);
});
```

### client.publish(name, data)

Type: `method`

Sends a message to a NEWSUBSCRIBE() of the server.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name declared with `NEWSUBSCRIBE()` on the server. |
| `data` | `Object` | yes |  | The data. |

The message is sent only while the client is connected; otherwise it is dropped. The server validates it with its schema.

Example: Send a message
```js
client.publish('users_updated', { id: user.id, name: user.name });
```

### client.call(name, data, [callback], [timeout])

Type: `method`

Calls a NEWCALL() procedure of the server.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name declared with `NEWCALL()`. |
| `data` | `Object` | yes |  | The data of the call. |
| `callback` | `Function` | no |  | `function(err, response)`. |
| `timeout` | `Number` | no | `10000` | The timeout in milliseconds. |

Returns: `Promise` Only when the callback is omitted.

Without a callback, the method returns a promise. It fails with `TMS is offline` when the client is not connected, with `408: Timeout` when no answer arrives in time, and with an `ErrorBuilder` when the server answers with an error.

Example: Call with a promise
```js
var product = await client.call('products_detail', { id: '123' });
```

### client.destroy()

Type: `method`

Closes the TMS client for good.

Stops the reconnecting and fails every pending `client.call()` with `TMS has been destroyed`.

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

### client.ready

Type: `property`

`true` while the TMS client is connected and has the meta of the server.

Returns: `Boolean`

It becomes `true` when a `meta` message arrives and `false` when the connection closes.

Example: Check
```js
if (client.ready)
	client.publish('users_updated', user);
```

### client.meta

Type: `property`

The last meta message received from the server.

Returns: `Object`

An object with `name` (the `CONF.name` of the server), `publish`, `subscribe` and `call` (arrays of `{ id, schema }`) and `subscribers` (the names with `SUBSCRIBE()` handlers).

Example: List the calls
```js
client.meta.call.forEach(function(item) {
	console.log(item.id);
});
```

### client.on('ready', callback)

Type: `event`

Fires every time the TMS client is connected and ready.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `callback` | `Function` | yes |  | `function()`. |

Fires after each `meta` message, so also after a reconnection.

Example: Ready
```js
client.on('ready', function() {
	console.log('TMS ready');
});
```

### client.on('meta', callback)

Type: `event`

Fires when the server sends its meta message.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `callback` | `Function` | yes |  | `function(meta)`. |

Fires after each connection and whenever the server changes its publishers, subscribers or calls.

Example: Meta
```js
client.on('meta', function(meta) {
	console.log(meta.name, meta.publish.length);
});
```
