Menu

Expand
Rate this page:

Sync Maps

A map stores unordered JSON objects accessible via a developer defined key.

Maps can be created, updated, subscribed to and removed via the client JavaScript SDK. Servers wishing to manage these objects can do so via the REST API.

Adding JSON to or Updating existing JSON in a map

syncClient.map('users').then(function(map) {
  map.set('david', {
    phone_number: 12345678,
    country: 'UK'
  }).then(function(item) {
    console.log('Added: ', item.key);
  }).catch(function(err) {
    console.error(err);
  });
});

Using set will overwrite any existing data in a map item.

Updating data in a map item

syncClient.map('users').then(function(map) {
  map.update('david',{country: "IRL"});
});

Use mutate for more control over updates.

syncClient.map('users').then(function (map) {
  map.mutate('david',function(remoteData) {
    remoteData.country = "USA";
    return remoteData;
  });
});

For more details on mutate and conflict resolution read up on Mutation and Conflict Resolution.

Subscribing to a map

syncClient.map('users').then(function (map) {
  map.on('itemAdded', function(item) {
    console.log('key', item.key);
    console.log('JSON data', item.value);
  });

//Note that there are two separate events for map item adds and map item updates:
  map.on('itemUpdated', function(item) {
    console.log('key', item.key);
    console.log('JSON data', item.value);
  });
});

Get items from a map

syncClient.map('users').then(function(map) {
  map.getItems().then(function(page) {
    console.log('show first item', page.items[0].key,
      page.items[0].value);
  });
});

Delete a map

syncClient.map('users').then(function (map) {
  map.removeMap().then(function () {
    console.log('map deleted');
  });
});

Get a particular item from a map

syncClient.map('users').then(function(map) {
  map.get('david').then(function(item) {
    console.log(item.value);
  });
});

Delete a particular item from a map

syncClient.map('users').then(function(map) {
  map.remove('david').then(function() {
    console.log('item deleted');
  });
});

See the latest JavaScript SDK documentation for a full reference.

Rate this page:

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

        
        
        

        Thank you for your feedback!

        We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

        Sending your feedback...
        🎉 Thank you for your feedback!
        Something went wrong. Please try again.

        Thanks for your feedback!

        Refer us and get $10 in 3 simple steps!

        Step 1

        Get link

        Get a free personal referral link here

        Step 2

        Give $10

        Your user signs up and upgrade using link

        Step 3

        Get $10

        1,250 free SMSes
        OR 1,000 free voice mins
        OR 12,000 chats
        OR more