Message Service (TMS)
TMS functions, the TMS client and their configuration keys.
On this page
The functions and configuration keys of TMS (Total.js Message Service), including the TMS client. The guide Message Service (TMS) 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.
API
config
$tms : true
Opens the TMS WebSocket endpoint.
$tms : trueReturns 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.
// /config
$tms : true
secret_tms : a-long-random-token
config
$tmsurl : /$tms/
The path of the TMS endpoint.
$tmsurl : /$tms/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.
config
$tmsmaxsize : 256
The maximum size of one TMS message in kB.
$tmsmaxsize : 256Returns Number
Default 256. The value is used when the TMS route is registered.
config
secret_tms : token
The token TMS clients must send.
secret_tms : tokenReturns 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.
config
$tmsclearblocked : 60
How often, in minutes, the list of blocked TMS clients is cleared.
$tmsclearblocked : 60Returns Number
Default 60. IP addresses blocked for wrong tokens can connect again after the list is cleared.
method
NEWPUBLISH(name, schema)
Declares an event that the app publishes to TMS clients.
NEWPUBLISH(name, schema)| Parameter | Type | Description |
|---|---|---|
name |
String |
The name of the event. |
schema |
String|Object |
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.
NEWPUBLISH('orders_created', 'id:UID, email:Email, total:Number');
PUBLISH('orders_created', { id: UID(), email: 'a@b.com', total: 100 });
method
PUBLISH(name, data)
Sends data of a declared event to subscribed TMS clients.
PUBLISH(name, data)| Parameter | Type | Description |
|---|---|---|
name |
String |
The name of an event declared with NEWPUBLISH(). |
data |
Object |
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.
NEWACTION('orders_create', {
input: '*email:Email, *total:Number',
action: function($, model) {
model.id = UID();
PUBLISH('orders_created', model);
$.success(model.id);
}
});
method
NEWSUBSCRIBE(name, schema, [callback])
Declares a message that TMS clients may send to the app.
NEWSUBSCRIBE(name, schema, [callback])| Parameter | Type | Description |
|---|---|---|
name |
String |
The name of the message. |
schema |
String|Object |
An inline schema, '@name' of a registered JSON schema, or null. |
callback? |
Function |
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.
NEWSUBSCRIBE('users_updated', 'id:UID, *name:String', function(msg, client) {
console.log(msg.response.name);
});
method
SUBSCRIBE(name, callback)
Adds a handler for a message sent by TMS clients.
SUBSCRIBE(name, callback)| Parameter | Type | Description |
|---|---|---|
name |
String |
The name of the message. |
callback |
Function |
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.
SUBSCRIBE('users_updated', function(msg, client) {
var user = msg.response;
console.log('User', user.id, 'is now', user.name);
});
method
UNSUBSCRIBE(name, [callback])
Removes handlers added with SUBSCRIBE().
UNSUBSCRIBE(name, [callback])| Parameter | Type | Description |
|---|---|---|
name |
String |
The name of the message. |
callback? |
Function |
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.
function onuser(msg) {
console.log(msg.response);
}
SUBSCRIBE('users_updated', onuser);
// later
UNSUBSCRIBE('users_updated', onuser);
method
NEWCALL(name, schema, callback)
Declares a procedure that TMS clients can call.
NEWCALL(name, schema, callback)| Parameter | Type | Description |
|---|---|---|
name |
String |
The name of the call. |
schema |
String|Object |
An inline schema, '@name' of a registered JSON schema, or null to remove the call. |
callback |
Function |
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.
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' });
});
method
TMSCLIENT(url, [token], [callback])
Connects the app to a TMS endpoint.
TMSCLIENT(url, [token], [callback])| Parameter | Type | Description |
|---|---|---|
url |
String |
The address of the endpoint, e.g. https://app.example.com/$tms/. |
token? |
String |
The secret_tms of the server. |
callback? |
Function |
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.
TMSCLIENT('https://orders.example.com/$tms/', CONF.orders_token, function(err, client, meta) {
client.subscribe('orders_created', function(order) {
console.log(order);
});
});
method
client.subscribe(name, callback)
Receives events that the server publishes with PUBLISH().
client.subscribe(name, callback)| Parameter | Type | Description |
|---|---|---|
name |
String |
The name of the event. |
callback |
Function |
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.
var client = TMSCLIENT('https://orders.example.com/$tms/', CONF.orders_token);
client.subscribe('orders_created', function(order) {
console.log(order.id, order.total);
});
method
client.publish(name, data)
Sends a message to a NEWSUBSCRIBE() of the server.
client.publish(name, data)| Parameter | Type | Description |
|---|---|---|
name |
String |
The name declared with NEWSUBSCRIBE() on the server. |
data |
Object |
The data. |
The message is sent only while the client is connected; otherwise it is dropped. The server validates it with its schema.
client.publish('users_updated', { id: user.id, name: user.name });
method
client.call(name, data, [callback], [timeout])
Calls a NEWCALL() procedure of the server.
client.call(name, data, [callback], [timeout])| Parameter | Type | Description |
|---|---|---|
name |
String |
The name declared with NEWCALL(). |
data |
Object |
The data of the call. |
callback? |
Function |
function(err, response). |
timeout? |
Number |
The timeout in milliseconds. Default: 10000 |
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.
var product = await client.call('products_detail', { id: '123' });
method
client.destroy()
Closes the TMS client for good.
client.destroy()Stops the reconnecting and fails every pending client.call() with TMS has been destroyed.
client.destroy();
property
client.ready
true while the TMS client is connected and has the meta of the server.
client.readyReturns Boolean
It becomes true when a meta message arrives and false when the connection closes.
if (client.ready)
client.publish('users_updated', user);
property
client.meta
The last meta message received from the server.
client.metaReturns 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).
client.meta.call.forEach(function(item) {
console.log(item.id);
});
event
client.on('ready', callback)
Fires every time the TMS client is connected and ready.
client.on('ready', callback)| Parameter | Type | Description |
|---|---|---|
callback |
Function |
function(). |
Fires after each meta message, so also after a reconnection.
client.on('ready', function() {
console.log('TMS ready');
});
event
client.on('meta', callback)
Fires when the server sends its meta message.
client.on('meta', callback)| Parameter | Type | Description |
|---|---|---|
callback |
Function |
function(meta). |
Fires after each connection and whenever the server changes its publishers, subscribers or calls.
client.on('meta', function(meta) {
console.log(meta.name, meta.publish.length);
});