ServerStorage: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
(16 intermediate revisions by the same user not shown)
Line 1: Line 1:
serverStorage(''string'' | ''variable'')
serverStorage.clear(''callback'')<br>
serverStorage.getItem(''key'', ''callback'')<br>
serverStorage.getAllItems(''key'', ''callback'')<br>
serverStorage.removeItem(''key'', ''callback'')<br>
serverStorage.setItem(''key'', ''value'', ''callback'')


== Description ==
== Description ==


ServerStorage allows you to save string data on the server so it is available next time you run the program. An entry can be created by assigning to serverStorage.setItem(''variableName'', ''value''), where ''variableName'' is chosen by you. Data is retrieved a similar way.  
ServerStorage allows you to save data in ''key'', ''value'' pairs on the server so it is available next time you run the program. An entry can be created by assigning to serverStorage.setItem(''key'', ''value''), where ''key'' is chosen by you. Data is retrieved a similar way.  The information in serverStorage is only available to the app itself.


Normally, data is saved in a namespace using the app's id. However, it is possible to set the namespace to the name of another app's id, allowing the two apps to share data.
''value'' can be a string, number, array or object. There is no size limit on ''value'', other than it be reasonable.


ServerStorage is insecure: there is currently no protection against other apps reading or changing the data. If two apps are using the same namespace, there is no collision or locking logic: it is possible for one user to erase another user's update.
This feature is only available if your app is hosted on Volt.  


This feature is currently experimental. It only works on apps deployed to nsbapp.com. It will not work locally or on your server at this time. Since the data is on the nsbapp.com test server, do not assume that data you save will be permanently available.
If your app does not have a live internet connection, the request will time out and an error message will be passed to ''callback''.


To turn on this feature, select ServerStorage on the ToolBox panel in Project Properties:
Since the calls access information on a server, they are asynchronous (so your app does not look up while the call is being processed). When the call is complete, the function named in ''callback'' is called in your app. It passes two parameters: (''error'', ''data''). If the call is successful, ''error'' is empty and your results are in ''data''. If the call is unsuccessful, ''error'' is not empty and the error message is in ''data''.message.
[[File:Serverstorage.png]]
 
serverStorage is an alias for $volt.user.storage in the [https://docs.voltcloud.io/api/ Volt API].


== Properties and Methods ==
== Properties and Methods ==
Line 18: Line 23:
{| class="wikitable"
{| class="wikitable"
|-
|-
| clear() || Clear all entries in the current namespace.
| clear(''callback'') || Clear all entries for the app.
|-
|-
| deleteItem(''key'') || Deletes item ''key''.
| getItem(''key'', ''callback'') || Get the item which is saved under ''key''. String.
|-
|-
| getItem(''key'') || Get the item which is saved under ''key''. String.
| getAllItems(''callback'') || Gets all the items for the app, as an array. Each element is a key,value pair.
|-
|-
| key(''n'') || Get the ''n''th item in the namespace.
| removeItem(''key'', ''callback'') || Deletes item ''key''.
|-
|-
| length || The number of items in the current namespace.
| setItem(''key'', ''value'', ''callback'') || Sets item ''key'' to ''value''. Creates item if it does not exist.
|-
| namespace || The group used by all serverStorage methods. Defaults to app ID.
|-
| setItem(''key'', ''value'') || Sets item ''key'' to ''value''. Creates item if it does not exist.
|}
|}


Line 36: Line 37:


<pre>
<pre>
Sub Main()
Function butClear_onclick()
   NameSpace.text = serverStorage.namespace
   serverStorage.clear(done)
End Sub
End Function


Function Button1_onclick()
Function butGetAllItems_onclick()
   t=SysInfo(10)
   serverStorage.getAllItems(getAllItemsCallback)
  For i = 1 To 10
    serverStorage.setItem("item" & i, i)
  Next
  MsgBox "Write 10 items into serverStorage(secs): " &  (SysInfo(10)-t)/1000
End Function
End Function


Function Button2_onclick()
Function butGetItem_onclick()
   NSB.Print(False)
   serverStorage.getItem("SimpleString", getItemCallback)
  For i=0 To serverStorage.length-1
    key = serverStorage.key(i)
    Print key, serverStorage.getItem(key)
  Next
End Function
End Function


Function Button3_onclick()
Function butRemoveItem_onclick()
   serverStorage.clear()
   serverStorage.removeItem("SimpleString", done)
End Function
 
Function butSetItem_onclick()
  serverStorage.setItem("SimpleString", "ABCD", done)
  serverStorage.setItem("Number", 123, done)
  serverStorage.setItem("Array", [1, 2, 3, 4], done)
  serverStorage.setItem("Object", {firstName: "Eric", lastName: "Cartman"}, done)
End Function
 
Function done(error, data)
  If error Then
    If data = undefined Then data = {message: "Network Error"}
    MsgBox data.message
  Else
    console.log("success")
  End If
End Function
 
Function getAllItemsCallback(error, data)
  If error Then
    If data = undefined Then data = {message: "Network Error"}
    MsgBox data.message
  Else
    Print False
    For Each key in data
      console.log(key, data[key])
      k = [key, key][0]
      Print k & ": " & data[key].toString()
    Next
    End If
End Function
 
Function getItemCallback(error, data)
  If error Then
    If data = undefined Then data = {message: "Network Error"}
    MsgBox data.message
  Else
    MsgBox data
  End If
End Function
End Function
</pre>
</pre>
Line 63: Line 95:
== Example - JavaScript ==
== Example - JavaScript ==
<pre>
<pre>
function Main() {
butClear.onclick = function() {
  NameSpace.text = serverStorage.namespace;
    serverStorage.clear(done);
}
};
 
butGetAllItems.onclick = function() {
    serverStorage.getAllItems(getAllItemsCallback);
};
 
butGetItem.onclick = function() {
    serverStorage.getItem("SimpleString", getItemCallback);
};
 
butRemoveItem.onclick = function() {
    serverStorage.removeItem("SimpleString", done);
};
 
butSetItem.onclick = function() {
    serverStorage.setItem("SimpleString", "ABCD", done);
    serverStorage.setItem("Number", 123, done);
    serverStorage.setItem("Array", [1, 2, 3, 4], done);
    serverStorage.setItem("Object", {
        firstName: "Eric",
        lastName: "Cartman"
    }, done);
};


Button1.onclick = function() {
function done(error, data) {
  t=SysInfo(10);
    if (error) {
  for  (i = 1; i  <= 10; i ++) {
        if (data == undefined) {
    serverStorage.setItem("item"  +  i, i);
            data = {
  }
                message: "Network Error"
  alert("Write 10 items into serverStorage(secs): " +  (SysInfo(10)-t)/1000);
            };
        }
        NSB.MsgBox(data.message);
    } else {
        console.log("success");
    }
}
}


Button2.onclick = function() {  
function getAllItemsCallback(error, data) {
  NSB.Print(False);
    if (error) {
  for  (i=0; i <= serverStorage.length-1; i++) {
        if (data == undefined) {
    key = serverStorage.key(i);
            data = {
     NSB.Print((key+ " " + serverStorage.getItem(key)) + "<br>");
                message: "Network Error"
  }
            };
        }
        NSB.MsgBox(data.message);
     } else {
        NSB.Print((false) + "<br>");
        for (key in data) {
            console.log(key, data[key]);
        }
    }
}
}


Button3.onclick = function() {
function getItemCallback(error, data) {
  serverStorage.clear();
    if (error) {
        if (data == undefined) {
            data = {
                message: "Network Error"
            };
        }
        NSB.MsgBox(data.message);
    } else {
        NSB.MsgBox(data);
    }
}
}
</pre>
</pre>
Line 94: Line 170:
== Related Items ==
== Related Items ==


[[localstorage|LocalStorage]], [[sessionstorage|SessionStorage]], [[sql|SQL]]
[[AppStorage|AppStorage]], [[localstorage|LocalStorage]], [[sessionstorage|SessionStorage]], [[sql|SQL]]


[[Category:Language Reference]]
[[Category:Language Reference]]


[[Category:Miscellaneous]]
[[Category:Miscellaneous]]

Revision as of 20:10, 23 January 2017

serverStorage.clear(callback)
serverStorage.getItem(key, callback)
serverStorage.getAllItems(key, callback)
serverStorage.removeItem(key, callback)
serverStorage.setItem(key, value, callback)

Description

ServerStorage allows you to save data in key, value pairs on the server so it is available next time you run the program. An entry can be created by assigning to serverStorage.setItem(key, value), where key is chosen by you. Data is retrieved a similar way. The information in serverStorage is only available to the app itself.

value can be a string, number, array or object. There is no size limit on value, other than it be reasonable.

This feature is only available if your app is hosted on Volt.

If your app does not have a live internet connection, the request will time out and an error message will be passed to callback.

Since the calls access information on a server, they are asynchronous (so your app does not look up while the call is being processed). When the call is complete, the function named in callback is called in your app. It passes two parameters: (error, data). If the call is successful, error is empty and your results are in data. If the call is unsuccessful, error is not empty and the error message is in data.message.

serverStorage is an alias for $volt.user.storage in the Volt API.

Properties and Methods

clear(callback) Clear all entries for the app.
getItem(key, callback) Get the item which is saved under key. String.
getAllItems(callback) Gets all the items for the app, as an array. Each element is a key,value pair.
removeItem(key, callback) Deletes item key.
setItem(key, value, callback) Sets item key to value. Creates item if it does not exist.

Example - BASIC

Function butClear_onclick()
  serverStorage.clear(done)
End Function

Function butGetAllItems_onclick()
  serverStorage.getAllItems(getAllItemsCallback)
End Function

Function butGetItem_onclick()
  serverStorage.getItem("SimpleString", getItemCallback)
End Function

Function butRemoveItem_onclick()
  serverStorage.removeItem("SimpleString", done)
End Function

Function butSetItem_onclick()
  serverStorage.setItem("SimpleString", "ABCD", done)
  serverStorage.setItem("Number", 123, done)
  serverStorage.setItem("Array", [1, 2, 3, 4], done)
  serverStorage.setItem("Object", {firstName: "Eric", lastName: "Cartman"}, done)
End Function

Function done(error, data)
  If error Then
    If data = undefined Then data = {message: "Network Error"}
    MsgBox data.message
  Else
    console.log("success")
  End If
End Function

Function getAllItemsCallback(error, data) 
  If error Then
    If data = undefined Then data = {message: "Network Error"}
    MsgBox data.message
  Else
    Print False
    For Each key in data
      console.log(key, data[key])
      k = [key, key][0]
      Print k & ": " & data[key].toString()
    Next
    End If
End Function

Function getItemCallback(error, data) 
  If error Then
    If data = undefined Then data = {message: "Network Error"}
    MsgBox data.message
  Else
    MsgBox data
  End If
End Function

Example - JavaScript

butClear.onclick = function() {
    serverStorage.clear(done);
};

butGetAllItems.onclick = function() {
    serverStorage.getAllItems(getAllItemsCallback);
};

butGetItem.onclick = function() {
    serverStorage.getItem("SimpleString", getItemCallback);
};

butRemoveItem.onclick = function() {
    serverStorage.removeItem("SimpleString", done);
};

butSetItem.onclick = function() {
    serverStorage.setItem("SimpleString", "ABCD", done);
    serverStorage.setItem("Number", 123, done);
    serverStorage.setItem("Array", [1, 2, 3, 4], done);
    serverStorage.setItem("Object", {
        firstName: "Eric",
        lastName: "Cartman"
    }, done);
};

function done(error, data) {
    if (error) {
        if (data == undefined) {
            data = {
                message: "Network Error"
            };
        }
        NSB.MsgBox(data.message);
    } else {
        console.log("success");
    }
}

function getAllItemsCallback(error, data) {
    if (error) {
        if (data == undefined) {
            data = {
                message: "Network Error"
            };
        }
        NSB.MsgBox(data.message);
    } else {
        NSB.Print((false) + "<br>");
        for (key in data) {
            console.log(key, data[key]);
        }
    }
}

function getItemCallback(error, data) {
    if (error) {
        if (data == undefined) {
            data = {
                message: "Network Error"
            };
        }
        NSB.MsgBox(data.message);
    } else {
        NSB.MsgBox(data);
    }
}

Output

(see serverStorage sample)

Related Items

AppStorage, LocalStorage, SessionStorage, SQL