Classes

Classes

Client
Document
InsightsItem
InstantQuery
List
ListItem
LiveQuery
Map
MapItem
Paginator
Stream
StreamMessage

Events


connectionError

Fired when connection is interrupted by unexpected reason
Properties:
Name Type Description
error Object connection error details
Properties
Name Type Argument Description
terminal Boolean twilsock will stop connection attempts
message String root cause
httpStatusCode Number <optional>
http status code if available
errorCode Number <optional>
Twilio public error code if available
Example
syncClient.on('connectionError', function(connectionError) {
  console.log('Connection was interrupted: ' + connectionError.message +
    ' (isTerminal: ' + connectionError.terminal')');
});

connectionStateChanged

Fired when connection state has been changed.
Parameters:
Name Type Description
connectionState Client#ConnectionState Contains current service connection state.
Example
syncClient.on('connectionStateChanged', function(newState) {
  console.log('Received new connection state: ' + newState);
});

tokenAboutToExpire

Fired when the access token is about to expire and needs to be updated. The trigger takes place three minutes before the JWT access token expiry. For long living applications, you should refresh the token when either tokenAboutToExpire or tokenExpired events occur; handling just one of them is sufficient.
Type: void
Example

The following example illustrates access token refresh

syncClient.on('tokenAboutToExpire', function() {
  // Obtain a JWT access token: https://www.twilio.com/docs/sync/identity-and-access-tokens
  var token = '<your-access-token-here>';
  syncClient.updateToken(token);
});

tokenExpired

Fired when the access token is expired. In case the token is not refreshed, all subsequent Sync operations will fail and the client will disconnect. For long living applications, you should refresh the token when either tokenAboutToExpire or tokenExpired events occur; handling just one of them is sufficient.
Type: void

removed

Fired when the document is removed, whether the remover was local or remote.
Parameters:
Name Type Description
args Object Arguments provided with the event.
Properties
Name Type Description
isLocal Boolean Equals 'true' if document was removed by local actor, 'false' otherwise.
Example
document.on('removed', function(args) {
  console.log('Document ' + document.sid + ' was removed');
  console.log('args.isLocal:', args.isLocal);
});

updated

Fired when the document's contents have changed, whether the updater was local or remote.
Parameters:
Name Type Description
args Object Arguments provided with the event.
Properties
Name Type Description
value Object A snapshot of the document's new contents.
isLocal Boolean Equals 'true' if document was updated by local actor, 'false' otherwise.
Example
document.on('updated', function(args) {
  console.log('Document ' + document.sid + ' was updated');
  console.log('args.value: ', args.value);
  console.log('args.isLocal: ', args.isLocal);
});

searchResult

Fired when a search result is ready.
Parameters:
Name Type Description
items LiveQuery#ItemsSnapshot A snapshot of items matching current query expression.
Example
instantQuery.on('searchResult', function(items) {
   Object.entries(items).forEach(([key, value]) => {
     console.log('Search result item key: ' + key);
     console.log('Search result item value: ' + value);
   });
});

itemAdded

Fired when a new item appears in the list, whether its creator was local or remote.
Parameters:
Name Type Description
args Object Arguments provided with the event.
Properties
Name Type Description
item ListItem Added item.
isLocal Boolean Equals 'true' if item was added by local actor, 'false' otherwise.
Example
list.on('itemAdded', function(args) {
  console.log('List item ' + args.item.index + ' was added');
  console.log('args.item.value:', args.item.value);
  console.log('args.isLocal:', args.isLocal);
});

itemRemoved

Fired when a list item is removed, whether the remover was local or remote.
Parameters:
Name Type Description
args Object Arguments provided with the event.
Properties
Name Type Description
index Number The index of the removed item.
isLocal Boolean Equals 'true' if item was removed by local actor, 'false' otherwise.
value Object In case item was removed by a remote actor, contains a snapshot of item data before removal.
Example
list.on('itemRemoved', function(args) {
  console.log('List item ' + args.index + ' was removed');
  console.log('args.value:', args.value);
  console.log('args.isLocal:', args.isLocal);
});

itemUpdated

Fired when a list item is updated (not added or removed, but changed), whether the updater was local or remote.
Parameters:
Name Type Description
args Object Arguments provided with the event.
Properties
Name Type Description
item ListItem Updated item.
isLocal Boolean Equals 'true' if item was updated by local actor, 'false' otherwise.
Example
list.on('itemUpdated', function(args) {
  console.log('List item ' + args.item.index + ' was updated');
  console.log('args.item.value:', args.item.value);
  console.log('args.isLocal:', args.isLocal);
});

removed

Fired when a list is deleted entirely, by any actor local or remote.
Parameters:
Name Type Description
args Object Arguments provided with the event.
Properties
Name Type Description
isLocal Boolean Equals 'true' if list was removed by local actor, 'false' otherwise.
Example
list.on('removed', function(args) {
  console.log('List ' + list.sid + ' was removed');
  console.log('args.isLocal:', args.isLocal);
});

itemRemoved

Fired when an existing item has been removed.
Parameters:
Name Type Description
args Object Arguments provided with the event.
Properties
Name Type Description
key String The key of the removed item.
Example
liveQuery.on('itemRemoved', function(args) {
  console.log('Item ' + args.key + ' was removed');
});

itemUpdated

Fired when an item has been added or updated.
Parameters:
Name Type Description
item InsightsItem Updated item.
Example
liveQuery.on('itemUpdated', function(item) {
  console.log('Item ' + item.key + ' was updated');
  console.log('Item value: ', item.value);
});

itemAdded

Fired when a new item appears in the map, whether its creator was local or remote.
Parameters:
Name Type Description
args Object Arguments provided with the event.
Properties
Name Type Description
item MapItem Added item.
isLocal Boolean Equals 'true' if item was added by local actor, 'false' otherwise.
Example
map.on('itemAdded', function(args) {
  console.log('Map item ' + args.item.key + ' was added');
  console.log('args.item.value:', args.item.value);
  console.log('args.isLocal:', args.isLocal);
});

itemRemoved

Fired when a map item is removed, whether the remover was local or remote.
Parameters:
Name Type Description
args Object Arguments provided with the event.
Properties
Name Type Description
key String The key of the removed item.
isLocal Boolean Equals 'true' if item was removed by local actor, 'false' otherwise.
value Object In case item was removed by a remote actor, contains a snapshot of item data before removal.
Example
map.on('itemRemoved', function(args) {
  console.log('Map item ' + args.key + ' was removed');
  console.log('args.value:', args.value);
  console.log('args.isLocal:', args.isLocal);
});

itemUpdated

Fired when a map item is updated (not added or removed, but changed), whether the updater was local or remote.
Parameters:
Name Type Description
args Object Arguments provided with the event.
Properties
Name Type Description
item MapItem Updated item.
isLocal Boolean Equals 'true' if item was updated by local actor, 'false' otherwise.
Example
map.on('itemUpdated', function(args) {
  console.log('Map item ' + args.item.key + ' was updated');
  console.log('args.item.value:', args.item.value);
  console.log('args.isLocal:', args.isLocal);
});

removed

Fired when a map is deleted entirely, by any actor local or remote.
Parameters:
Name Type Description
args Object Arguments provided with the event.
Properties
Name Type Description
isLocal Boolean Equals 'true' if map was removed by local actor, 'false' otherwise.
Example
map.on('removed', function(args) {
  console.log('Map ' + map.sid + ' was removed');
  console.log('args.isLocal:', args.isLocal);
});

messagePublished

Fired when a Message is published to the Stream either locally or by a remote actor.
Parameters:
Name Type Description
args Object Arguments provided with the event.
Properties
Name Type Description
message StreamMessage Published message.
isLocal Boolean Equals 'true' if message was published by local code, 'false' otherwise.
Example
stream.on('messagePublished', function(args) {
  console.log('Stream message published');
  console.log('Message SID: ' + args.message.sid);
  console.log('Message value: ', args.message.value);
  console.log('args.isLocal:', args.isLocal);
});

removed

Fired when a stream is removed entirely, whether the remover was local or remote.
Parameters:
Name Type Description
args Object Arguments provided with the event.
Properties
Name Type Description
isLocal Boolean Equals 'true' if stream was removed by local code, 'false' otherwise.
Example
stream.on('removed', function(args) {
  console.log('Stream ' + stream.sid + ' was removed');
  console.log('args.isLocal:', args.isLocal);
});