Giter Site home page Giter Site logo

django-channels's Introduction

Django Channels

This is a work-in-progress code branch of Django implemented as a third-party app, which aims to bring some asynchrony to Django and expand the options for code beyond the request-response model.

The proposal itself is detailed in a very long Gist and there is discussion about it on django-developers.

If you wish to use this in your own project, there's basic integration instructions below - but be warned! This is not stable and may change massively at any time!

Integration

Make sure you're running Django 1.8. This doesn't work with 1.7 (yet?)

If you want to use WebSockets (and that's kind of the point) you'll need autobahn and twisted packages too. Python 3/asyncio support coming soon.

pip install channels and then add channels to the TOP of your INSTALLED_APPS list (if it is not at the top you won't get the new runserver command).

You now have a runserver that actually runs a WSGI interface and a worker in two different threads, runworker to run separate workers, and runwsserver to run a Twisted-based WebSocket server.

You should place consumers in either your views.py or a consumers.py. Here's an example of WebSocket consumers for basic chat:

import redis
from channels import Channel

redis_conn = redis.Redis("localhost", 6379)

@Channel.consumer("django.websocket.connect")
def ws_connect(path, send_channel, **kwargs):
    redis_conn.sadd("chatroom", send_channel)

@Channel.consumer("django.websocket.receive")
def ws_receive(channel, send_channel, content, binary, **kwargs):
    # Ignore binary messages
    if binary:
        return
    # Re-dispatch message
    for channel in redis_conn.smembers("chatroom"):
        Channel(channel).send(content=content, binary=False)

@Channel.consumer("django.websocket.disconnect")
def ws_disconnect(channel, send_channel, **kwargs):
    redis_conn.srem("chatroom", send_channel)
    # NOTE: this does not clean up server crash disconnects,
    # you'd want expiring keys here in real life.

Alternately, you can just push some code outside of a normal view into a worker thread:

from django.shortcuts import render
from channels import Channel

def my_view(request):
    # Dispatch a task to run outside the req/response cycle
    Channel("a_task_channel").send(value=3)
    # Return a response
    return render(request, "test.html")

@Channel.consumer("a_task_channel")
def some_task(channel, value):
    print "My value was %s from channel %s" % (value, channel)

Limitations

The runserver this command provides currently does not support static media serving, streamed responses or autoreloading.

In addition, this library is a preview and basically might do anything to your code, or change drastically at any time.

If you have opinions, please provide feedback via the appropriate django-developers thread.

django-channels's People

Contributors

andrewgodwin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.