---
title: Message Service (TMS)
project: Total.js V5
version: 0.0.20
section: guides
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v5/0-0-20/guides/message-service-and-apis/
updated: 2026-11-16T15:32:52.498Z
---

# Message Service (TMS)

> Publish events, receive messages and answer calls over TMS.

Total.js 5 has two small layers for talking to other services:

- **TMS** (Total.js Message Service) opens a WebSocket endpoint in your application. Connected apps receive the events you publish, send you messages and call your procedures. Every message has a name and a JSON schema that validates it.
- **APIs** give a name to a remote service. You write the code that talks to the service once with `NEWAPI()` and then call it anywhere with `API()`. The built-in `TAPI` service calls Total.js API.

## Enabling TMS

TMS is off by default. Turn it on in the configuration and set a token:

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

The endpoint is `$tmsurl` (default `/$tms/`) and the maximum message size is `$tmsmaxsize` kB (default `256`). A client must send the token in the `x-token` header. A wrong token closes the connection with code `4001`; an IP address with more than five wrong attempts is refused until the blocked list is cleared, every `$tmsclearblocked` minutes (default `60`). When `secret_tms` changes through `LOADCONFIG()`, all connected clients are disconnected.

After the connection opens, the server sends a `meta` message: the name of the app (`CONF.name`) and the lists of publishers, subscribers and calls, each with its JSON schema. The list is sent again (500 ms after the last change) whenever you register or remove a name. A message `{ type: 'ping' }` is answered with `{ type: 'pong' }`.

## Publishing events

`NEWPUBLISH()` declares an event and its schema. `PUBLISH()` sends data to every connected client that subscribed to that event:

```js
// definitions/tms.js
NEWPUBLISH('orders_created', 'id:UID, email:Email, total:Number');

// anywhere in the app
PUBLISH('orders_created', { id: order.id, email: order.email, total: order.total });
```

`PUBLISH()` does nothing when TMS is off, when no client is connected or when the name was not declared with `NEWPUBLISH()`. The server does not validate the data; the receiving `TMSCLIENT()` cleans it with the schema from `meta`. `NEWPUBLISH(name, null)` removes the event.

## Receiving messages

`NEWSUBSCRIBE()` declares a message that clients may send to your app, with its schema. `SUBSCRIBE()` adds a handler for it:

```js
NEWSUBSCRIBE('users_updated', 'id:UID, *name:String, email:Email');

SUBSCRIBE('users_updated', function(msg, client) {
	// msg.response is the validated data
	var user = msg.response;
	console.log('From', client.ip, user.id, user.name);
});
```

A message that does not pass the schema is dropped. A handler receives the result of the validation (`msg.response` holds the data, `msg.error` is `null`) and the WebSocket client that sent the message. You can pass the handler as the third argument of `NEWSUBSCRIBE()` instead of calling `SUBSCRIBE()`. `UNSUBSCRIBE(name, [handler])` removes one handler, or all handlers of the name.

## Remote calls

`NEWCALL()` declares a procedure that clients can call and wait for its answer:

```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', price: 49 });
});
```

`next(err, response)` answers the client. An `ErrorBuilder` is sent as its output; any other error becomes `[{ error: '...' }]`. Data that does not pass the schema is answered with the validation errors without running the callback, and an unknown message type is answered with a `404` error. `NEWCALL(name, null)` removes the call.

## Connecting to TMS

`TMSCLIENT()` connects your app to the TMS endpoint of another Total.js app. An `http(s)://` address is changed to `ws(s)://`, and the client reconnects automatically:

```js
TMSCLIENT('https://orders.example.com/$tms/', CONF.orders_token, function(err, client, meta) {

	console.log('Connected to', meta.name);

	// Events published by the server with PUBLISH()
	client.subscribe('orders_created', function(order) {
		console.log('New order', order.id);
	});

	// Messages for the server's NEWSUBSCRIBE()
	client.publish('users_updated', { id: user.id, name: user.name });

	// Procedures declared with NEWCALL()
	client.call('products_detail', { id: '123' }, function(err, product) {
		console.log(err, product);
	});
});
```

The callback runs once: with the client when the first `meta` message arrives, or with the close code as `err` when the connection closes before that. The client then fires the `ready` and `meta` events after every (re)connection and keeps the last message in `client.meta`. `client.call()` returns a promise when you omit the callback, fails with `TMS is offline` when the client is not connected and with `408: Timeout` after 10 seconds (or your timeout). Subscriptions you add are sent to the server, so it only pushes the events you want. `client.destroy()` closes the connection and fails the pending calls.
