ServerStorage: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
'''Starting with AppStudio 6, ServerStorage is no longer supported.'''
serverStorage.clear(''callback'')
 
serverStorage.getItem(''key'', ''callback'')
serverStorage(''string'' | ''variable'')
serverStorage.getAllItems(''key'', ''callback'')
serverStorage.removeItem(''key'', ''callback'')
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(''variableName'', ''value''), where ''variableName'' is chosen by you. Data is retrieved a similar way.  
 
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.


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.
''value'' can be a string, number, array or object. There is no size limit on ''value'', other than it be reasonable.


This feature requires server side software. The AppStudio Server has it. If you want to implement it on your own server, [https://github.com/artlogic/server-storage/blob/master/README.md the instructions are here].
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''.


To turn on this feature, select ServerStorage on the [[Toolbox|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]]


== Properties and Methods ==
== Properties and Methods ==

Revision as of 16:31, 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(variableName, value), where variableName is chosen by you. Data is retrieved a similar way.

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.

Properties and Methods

clear() Clear all entries in the current namespace.
deleteItem(key) Deletes item key.
getItem(key) Get the item which is saved under key. String.
key(n) Get the nth item in the namespace.
length The number of items in the current namespace.
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.

Example - BASIC

Sub Main()
  NameSpace.text = serverStorage.namespace
End Sub

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

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

Function Button3_onclick()
  serverStorage.clear()
End Function

Example - JavaScript

function Main() {
  NameSpace.text = serverStorage.namespace;
}

Button1.onclick = function() {
  t=SysInfo(10);
  for   (i = 1; i  <= 10; i ++) {
    serverStorage.setItem("item"  +  i, i);
  }
  alert("Write 10 items into serverStorage(secs): "  +   (SysInfo(10)-t)/1000);
}

Button2.onclick = function() { 
  NSB.Print(False);
  for   (i=0; i <= serverStorage.length-1; i++) {
    key = serverStorage.key(i);
    NSB.Print((key+ " " + serverStorage.getItem(key)) + "<br>");
  }
}

Button3.onclick = function() {
  serverStorage.clear();
}

Output

(see serverStorage sample)

Related Items

LocalStorage, SessionStorage, SQL