Read and Write

Socketize gives you a NoSQL like database in the cloud. You can directly access this database from the browser via WebSocket (with fallback for older browsers), backed by a no-compromising security. You can access the database via REST API too, but here we will provide examples of Socketize.js only.

To read and write to Socketize you always need and key, which is an identifier of it's corresponding value. Whenever you store something against a key, Socketize will create this key and store value for it or if the key already exists then it'll overwrite the value.

The key will always be a string. The value can be a string, JSON object or JSON array.

There is another special data type which we call list. A list has a key and a collection of values. The benefit of list is, you can run complex queries in it to filter data. You can use where, limit, offset and so on. The values list contains can also be string, JSON object or JSON array.

Set

socketize.set('brand', 'Apple');
socketize.set('products', {
    apple: ['iPhone', 'MacBook'],
    samsung: ['Galaxy Note', 'Galaxy Tab']
});

If you want to check if the operation completed successfully, follow this:

socketize.set('brand', 'Apple').then(function(response) {
    // successful
}).catch(function(response) {
    // error
});

Get

socketize.get('brand').then(function(brand) {
    alert(brand); // Apple
});

then() method is like a callback but much more powerful, which is known as Promise. Also you can read more about the response object passed to callback here.

When calling get you will get the same type of data used when calling set.

socketize.get('products').then(function(products) {
    console.log(products);
    /*
     You will get
     {
         apple: ['iPhone', 'MacBook'],
         samsung: ['Galaxy Note', 'Galaxy Tab']
     }
    */
});

For full reference please check Socketize.js Reference.

Lists

A list is simply a list or collection of values. To create a list you simply call:

socketize.pushOnList('a_list', 'Bar');

If foo_list is already there then the value(Bar) will be appended to list. You can also push arrays and objects in the list.

socketize.pushOnList('foo_list', {name: 'Bar', city: 'Sylhet'});
socketize.pushOnList('foo_list', {name: 'Baz', city: 'Dhaka'});
socketize.pushOnList('foo_list', {name: 'Tango', city: 'Sylhet'});

To get items from a list:

socketize.getListItems('foo_list').then(function(fooList) {
    fooList.forEach(function(item) {
        console.log(item.name);
    });
});

Query a list

You can search for items within a list like this:

socketize.searchList('foo_list', [
    ['city', 'like', 'Syl%'],
    ['name', '!=', 'Tango']
]).then(function (response) {
    console.log(response[0].name);
});

Supported operators are =, !=, <, >, like, regex.