/django_channels_example

Websocket magic in Django application using Channels

Primary LanguagePython

Django Channels demo

The following repo contains code on how to build Django Channels into your application with endpoints created by Django REST framework.

Dependancies

  • Install brew from https://brew.sh/
  • Install python and pip from brew
  • Install redis from brew: brew install redis
  • Start the redis service brew services start redis

Setup

  1. Create a virtual environment by running virtualenv env in the terminal.
  2. Activate this python environment: source env/bin/activate
  3. Install all dependancies found in the requirements.txt. From terminal, run pip install -r requirements.txt
  4. Go into the prototype directory: cd prototype
  5. Migrate: python manage.py migrate
  6. Populate the database with some broadcasting channels: python manage.py populate_db
  7. Run the server: python manage.py runserver

Usage

  • Open up your browser and go to localhost:8000/channels. Open up the console and there is a confirmation message to assert the connection was successful.
  • With a tool like Postman, you can send PUT requests (as shown below) to update the database.

screen shot 2017-09-03 at 15 55 40

The results will update in real time without the client initially sending off HTTP requests!

screen shot 2017-09-03 at 15 58 00

References