-
Notifications
You must be signed in to change notification settings - Fork 13
Websocket API
#What are WebSockets WebSockets defines a bi-directional web communication that makes it possible to open an interactive session between the server and the client’s browser. The server and the client can both send messages and receive event-driven responses. Instead of polling the server for an update, the server can push notifications whenever there is an update in the system.
#How does it work
##Dependencies and installation
###1. Redis
Redis is a flexible, open-source, key value data store. It allows the user to store vast amounts of data without the limits of a relational database. Redis is used as a message queue and is run side by side with Django.
To install Redis, use apt
to get redis-server
:
sudo apt-get install redis-server
To start Redis server, go into the Redis directory and do:
$> src/redis-server
Once the server is started, you can test it by starting the program redis-cli
which can be used to send messages to Redis. When sending a PING
command, the server will answer with a PONG
if it is working properly.
$> redis-cli
redis 127.0.0.1:6379> PING
redis 127.0.0.1:6379> PONG
###2. ws4redis
Websocket for Redis (ws4redis) allows uni- and bidirectional communication from the client to the server and vice versa. Each websocket is identified by the part of the URL which follows the prefix /ws/. Use different uniform locators to distinguish between unrelated communication channels.
To install, first make sure that Redis server is running, then do:
$> pip install django-websocket-redis
To add heartbeat configuration, a setting should be added WS4REDIS_HEARTBEAT = '--heartbeat--'
.
###3. Psycopg
Psycopg is a PostgreSQL adapter for the Python programming language. It is a wrapper for the libpq, the official PostgreSQL client library. And Psycopg2 package is the mature implementation of the adapter.
If it is not already installed, install the package with:
$> pip install psycopg2
###4. Settings and configurations
####WebSocket settings in settings.py
WEBSOCKET_URL = '/ws/' # Specify the URL that distinguishes websocket connections from normal requests
WSGI_APPLICATION = 'ws4redis.django_runserver.application' # Overrides Django’s internal main loop and adds a URL dispatcher in front of the request handler
WS4REDIS_EXPIRE = 3600 # Number in seconds, to additionally persist messages published on the message queue
WS4REDIS_HEARTBEAT = '--heartbeat--' # Customized heartbeat message
WS4REDIS_PREFIX = 'rodan' # Can prefix each entry in the datastore with a string.
WS4REDIS_CONNECTION =
{
'host' = 'localhost',
'port' = 6379,
'db' = 0
} # Hostname, port and database number for redis
###5. Run migration
Run this to create triggers inside Postgres database for WebSocket communications. Make sure to be the superuser of the Postgres database.
(rodan-env)$> python manage.py migrate
##Server Side
###1. Post-update trigger in Postgres database
Instead of the server polling for any update request from the client, the update notification is 'pushed' from the server side by adding triggers inside Postgres tables containing Rodan models. A signal is sent each time an INSERT
or UPDATE
action is performed on a table containing the trigger. Specifically, looping through the Rodan model tables, the triggers are first destroyed if they already exist, and then they are re-created.
See this file.
###2. Publish Redis socket messages
Every time a signal is received from the post-update trigger, a message containing information about the status of update, the model name and the UUID of the model instance is broadcasted to all subscribed clients through Redis.
Redis connection can be opened with:
r = redis.StrictRedis(REDIS_HOST, REDIS_PORT, DB)
where REDIS_HOST
, REDIS_PORT
, and DB
are specified in the settings_production.py
.
Once the connection is opened, messages can be broadcasted.
r.publish('rodan:broadcast:rodan', 'message_string')
###3. Post-migrate signal
The process of creating triggers on database tables is performed after receiving the post-migrate signal from Django, ie. triggers will be destroyed and re-created each time this command is executed:
(rodan-env)$> python manage.py migrate
#Stress Testing
##Dependancies and installation
Thor is needed for stress testing. Make sure that Node.js is installed on your system. Then, install the module using
$> npm install -g thor
##Testing
Then, stress tests can be run by running
$> ~/Rodan/rodan/test/test_load.sh -a[options] -n[options] -s[options]
Running the command will diplay a help menu.
$> ~/Rodan/rodan/test/test_load.sh -h
Options:
-a Number of connections to test on, if unspecified, 10000 connections will be tested
-n Number of messages to be sent per connection
-s Name of the server, eg. rodan.simssa.ca. if unspecified, server will be localhost
-h Show help instructions
The test results will be stored in the directory ~/Rodan/rodan/test/test_log
.
- Repository Structure
- Working on Rodan
- Testing Production Locally
- Working on Interactive Classifier
- Job Queues
- Testing New Docker Images
- Set up Environment Variables
- Set up SSL with Certbot
- Set up SSH with GitHub
- Deploying on Staging
- Deploying on Production
- Import Previous Data