Classes

Classes

Client
Document
List
ListItem
Map
MapItem
Paginator
Stream
StreamMessage

Events


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);
});

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);
});

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);
});