Django implementation and API for a counter with live updates. POST to increment. GET to listen for updates (server-sent events).
This project uses Fastly and Fanout for high scale. Essentially, Fastly caches the current counter values, and counter updates are pushed through Fanout, greatly reducing load on the Django app itself in order to serve lots of clients. Technical background in this article.
There is a public instance available at livecounter.org.
Install dependencies:
virtualenv venv
. venv/bin/activate
pip install -r requirements.txt
Also be sure you have Redis installed, which the main server process uses to communicate with the publisher process.
Ensure you have accounts with Fastly and Fanout Cloud, and set up the same domain in both systems. Additionally, create a Fastly API key. Then, create a .env
file with the following environment variables:
FASTLY_API_KEY={api-key}
FASTLY_DOMAIN={domain-name}
GRIP_URL=http://api.fanout.io/realm/{realm-id}?iss={realm-id}&key=base64:{realm-key}
You can also set DATABASE_URL
to point to a database. If not set, sqlite will be used.
Create database tables:
python manage.py migrate
Create a counter object:
from livecounter.models import Counter
c = Counter(name='foo')
c.save()
Run the main server (either using python manage.py runserver
or a webserver with server/wsgi.py
).
Also, run the publisher command (python manage.py publisher
, either in another terminal or as a background service).
Point the domain CNAME at Fanout Cloud. Configure the Fanout Cloud domain to proxy traffic to Fastly. Configure Fastly to point traffic to this server.