Synchronet v3.19b-Win32 (install) has been released (Jan-2022).

You can donate to the Synchronet project using PayPal.

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
service:jsondb [2022/11/28 18:06] – Changed section formatting for table of contents; added a Best Practices section Nightfoxservice:jsondb [2022/12/08 14:01] (current) – [Best Practices] Nightfox
Line 4: Line 4:
 \\ \\
 ===== Updating services.ini and json-service.ini ===== ===== Updating services.ini and json-service.ini =====
-Synchronet provides this via the script json-service.js (in the exec directory).  To enable the JSON service, add the following lines to your ctrl/[['config:services.ini']] if not already there:+Synchronet provides this via the script json-service.js (in the exec directory).  To enable the JSON service, add the following lines to your ''[[dir:ctrl]]''/''[[config:services.ini]]'' if not already there:
 <file> <file>
 [JSON] [JSON]
Line 13: Line 13:
 Note the port number is 10088.  This is a common port number to use for the JSON service.\\ Note the port number is 10088.  This is a common port number to use for the JSON service.\\
 \\ \\
-For storing data to be accessed this way, Synchronet uses the concept of JSON "databases" Each JSON database is given a name and are configured in ctrl/json-service.ini.  For instance, there is a one-liners script in xtrn/oneliners, and if you want to host one-liners, you would need these lines in ctrl/json-service.ini:+For storing data to be accessed this way, Synchronet uses the concept of JSON "databases" Each JSON database is given a name and are configured in ''[[dir:ctrl]]''/json-service.ini.  For instance, there is a one-liners script in xtrn/oneliners, and if you want to host one-liners, you would need these lines in ctrl/json-service.ini:
 <file> <file>
 [oneliners] [oneliners]
Line 23: Line 23:
  
 ===== Reading from & writing to a JSON database=====  ===== Reading from & writing to a JSON database===== 
-Most basically, all that is really required to interact with a JSON database is writing (adding/updating data) and reading (getting data from the JSON database).  The top of json-client.js (in sbbs/exec/load) has a list of relevant functions for working with a JSON database.  Assuming the JSON client object is jsonClient, the two functions for reading from and writing to a JSON database are:+In a JavaScript mod/door/game (a 'client'), first you will need to have your script include json-client.js (in the ''[[dir:exec]]''/''[[dir:load]]'' directory).  Before you use JSONClient, add this to your JS script: 
 +  require("json-client.js", "JSONClient"); 
 +Then, create a JSONClient object.  When doing so, you will need to provide the server (host) name/IP address and the port number as parameters.  For example, to connect to your own BBS (you could use "127.0.0.1" or "localhost"): 
 +  var jsonClient = new JSONClient("127.0.0.1", 10088); 
 +Most basically, all that is really required to interact with a JSON database is writing (adding/updating data) and reading (getting data from the JSON database).  The top of json-client.js has a list of relevant functions for working with a JSON database.  The two functions for reading from and writing to a JSON database are defined as follows:
  
   jsonClient.read(scope,location,lock)   jsonClient.read(scope,location,lock)
Line 52: Line 56:
   {   {
   }   }
 +
 +Also, when you are done using your JSONClient object, it is generally a good practice to explicitly have it disconnect from the server, by calling the 'disconnect' method:
 +  jsonClient.disconnect();
  
 ===== Best Practices ===== ===== Best Practices =====
-When there could be concurrent access to the same data (i.e., multiple clients that might need to write to the same location in the JSON data), it would be a good practice to lock that location for writing, read the data, write your changesthen unlock it.  There are a couple of functions, lock() and unlock() that you can use to do this:+When there could be concurrent access to the same data (i.e., multiple clients that might need to write to the same location in the JSON data), it is a good practice to lock that location for writing when you're writing an update.  The idea is to prevent two or more systems from writing data to the same place at the same timewhere one write might overwrite another write, or perhaps another system reading data would get the wrong copy of the data just as it has been updatedetc.\\ 
 +\\ 
 +Note that the write function has a lock parameter: 
 +  JSONCLient.write(scope, data, location, lock) 
 +If you specify the lock parameter (such as 2, the write lock), it will lock, write, and unlock all at once.  For instance: 
 +  var JSON_DB_LOCK_WRITE = 2; 
 +  jsonClient.write("scores", "player1.TestGame", scoresData, JSON_DB_LOCK_WRITE); 
 +\\ 
 +There are also a couple of functions, lock() and unlock() that you can use for locking and unlocking:
   jsonClient.lock(scope,location,lock);   jsonClient.lock(scope,location,lock);
   jsonClient.unlock(scope,location);   jsonClient.unlock(scope,location);
 +When you use those, then you would //not// specify the lock parameter for write().  If you lock before you write, you know that you can read after that and whatever value you get back will not have changed by some other system.\\ 
 +\\
 For instance: For instance:
   var JSON_DB_LOCK_READ = 1;   var JSON_DB_LOCK_READ = 1;
   var JSON_DB_LOCK_WRITE = 2;   var JSON_DB_LOCK_WRITE = 2;
   var jsonClient = new JSONClient("servername", 10088);   var jsonClient = new JSONClient("servername", 10088);
-  jsonClient.lock("json_scope", "my_game.user_scores", JSON_DB_LOCK_WRITE); +  jsonClient.lock("scores", "player1.TestGame", JSON_DB_LOCK_WRITE); 
-  var scoresData = jsonClient.read("json_scope", "my_game.user_scores", JSON_DB_LOCK_READ);+  var scoresData = jsonClient.read("scores", "player1.TestGame");
   // Change scoresData ...   // Change scoresData ...
-  jsonClient.write("json_scope", "my_game.user_scores", scoresData, JSON_DB_LOCK_WRITE); +  jsonClient.write("scores", "player1.TestGame", scoresData); 
-  jsonClient.unlock("json_scope", "my_game.user_scores");+  jsonClient.unlock("scores", "player1.TestGame");
      
 +Note that you need to either specify the lock parameter for write() OR call lock() & unlock() without specifying a lock parameter for write().  If you both call lock() with 2 (write lock) AND specify 2 for the lock parameter for write(), then it will not update the data on the server because you'd have 2 locks interfering with each other.
 ===== Special files: commands.js and service.js ===== ===== Special files: commands.js and service.js =====
 There are a couple of additional JavaScript files you can create for your JSON database:\\ There are a couple of additional JavaScript files you can create for your JSON database:\\