django-websocket-channel

Websocket support for Django using Redis


Keywords
django, websocket, redis
License
MIT
Install
pip install django-websocket-channel==0.0.5

Documentation

django-websocket-channel

Websocket support for Django using Redis

#Installation

sudo service redis-server start
pip install django-websocket-channel

#Configuration

INSTALLED_APPS = (
    ...
    'websocket_channel',
    ...
)
WS_REDIS = {
        'host': 'localhost',
        'port': 6379,
        'db': 0,
        'password': None,
    }
TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.static',
    'websocket_channel.context_processors.default',
    ...
)


django 1.8

TEMPLATES = [
    {
        '''
        'OPTIONS': {
            'context_processors': [
                '''
                'django.template.context_processors.static',
                'django.contrib.messages.context_processors.messages',
                'websocket_channel.context_processors.default',
                '''
            ],
        },
    },
]


#NGiNX

server {
	'''
	location /ws/ {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass http://unix:/tmp/websocket_channel_websocket.socket;
    }
    location / {
        include uwsgi_params;
        uwsgi_pass unix:/tmp/websocket_channel.socket;

    }

    '''

}

#uWSGI:

[default]
gid = www-data
uid = www-data
umask = 002
;virtualenv = virtualenv/path
master = true
env=DJANGO_SETTINGS_MODULE=settings


env=PYTHON_EGG_CACHE=/tmp/websocket_channel
env=LANG=zh_CN.UTF-8
env=LC_ALL=zh_CN.UTF-8
ignore-sigpipe = true
enable-threads = true
chmod-socket = 666
chdir = project/path/to
pythonpath = project/path/to
max-requests = 500000



[runserver]
ini = :default
socket = /tmp/websocket_channel.socket
pidfile = /tmp/websocket_channel.pid
module = wsgi
buffer-size = 32768
processes = 4
daemonize = project/path/to/web.log

[wsserver]
ini = :default
http-socket = /tmp/websocket_channel_websocket.socket
pidfile = /tmp/websocket_channel_websocket.pid
daemonize = project/path/to/websocket_web.log
module = wsgi_websocket
processes = 1
http-websockets = true
gevent=1000


#Then start uWSGI:

uwsgi --ini uwsgi.ini:runserver
uwsgi --ini uwsgi.ini:wsserver

#Client JavaScript

<script type="text/javascript" src="{{ STATIC_URL }}js/dj_websocket.js"></script>
<script type="text/javascript">
    var ws = new DjWebSocket({uri: '{{ WEBSOCKET_URL }}'});
    ws.onopen = function(){
        ws.subscribe("channel", function(data){
            //channel msg
        });

    }
</script>

#Subscribe to Broadcast Notifications

    RedisPublisher("channel").publishgit _message("1111111111")