Redis <> AI2
We have had the cloudDB component for some time now, which uses a redis server as the database backend. It has also been possible for developers to use their own redis server, including SSL, with the cloudDB component. I can now provide a method that offers developers direct access to a Redis server, so that all the features offered by Redis can be used in an AI2 app. This is achieved by using the Webdis "front end" to Redis, an http server that sends commands and receives output from Redis. It is then possible to use http calls in the extension to send and return information to Webdis, then to redis and back again. Given that I already have a redis server setup, I have used this for simplicity. My Redis version is only 5.07, this has no built in features for SSL, therefore Webdis commands are sent using HTTP and not HTTPS for this guide. It should be possible to set everything up using SSL with Redis version < 6.0
You will require:
An online ubuntu (linux) server
A Redis instance installed, configured and running on your ubuntu server
A Webdis instance installed, configured and running on your ubuntu server
The RedisConnector extension (or the RedisConnectorExpert extension)
An AppInventor app
Ubuntu Server & Redis
I am not going to try and reinvent the wheel here. Digital Ocean provide some useful howto guides on setting up a server and installing and setting up Redis
You will need to make some changes to your firewall, allowing all access to ports: 6379, 7379.
Digital Ocean also provide a group of guides about how Redis works and its features
There are other host providers.....
You should, as a minimum, set a strong password (or user/password depending on Redis version) for your redis instance in the config file.
The Redis Command Reference Pages will also assist with understanding the workings of Redis
You should have a very good read of everything here: Webdis on Github
If you have git setup on your server, you can clone Webdis, otherwise, download the zip, unzip it, then upload the webdis-master folder to your sudo user's home directory.
Ensure you have libevent-dev installed:
sudo apt-get install libevent-dev
You will also need make and gcc installed to build from sources
cd into the webdis-master directory and run make clean all.
We now need to setup the webdis.json file (config file):
The three "key":"values" of interest here are "redis_auth", "http_host", and "acl".
"redis_auth" requires the redis password entered in the redis.conf file when you setup redis
"http_host" is the IP address of your server. This is required so that external requests can be made.
"acl" provides a further line of security. The user/pass basic authentication is used by clients to access webdis and redis. I have used just a single entry "for all app users", but it is possible to have as many user/password entries as you wish, one for each app user. I have written a bash script that will set a list of users to the json file.
We can now start Webdis. Still in the directory webdis-master run the following: ./webdis &
That should start Webdis, and we are now ready to test the webdis/redis functionality. You will see that you can daemonize Webdis in the config file.
From your computer, open a terminal and try the following curl command, using your own IP address and user/pass:
this should return:
If it does you are good to go, if not, then you will need to go over your work to see what you have missed, and also check your firewall.
Open a new project in App Inventor
Import the RedisConnector extension (or the RedisConnectorExpert extension) - see below for links
Configure your designer / blocks
The RedisConnector extension has been built to provide easy access to the most useful commands in Redis, and simplifies the uploading and downloading of binary files to/from the server. The inclusion of AppId/ProjectId/Database further assists in the separation of an ai2 project from anything else going on on the redis server. All keys will include the AppId and ProjectId, but these will be "hidden" in sends and output returns. Similarly, the setting of the database (there are 16 databases or namespaces, 0-15, which can be used) also provides a degree of separation from other activities on the server. The default database is 0.
The RedisConnectorExpert extension does away with all the above, and provides only a single block for the entry of commands (as well as the file upload/download blocks) and provides the developer with free rein over how the app is used.