Websocket support for Django using Redis
Project description
# django-websocket-channel
Websocket support for Django using Redis
#Installation
<pre>
sudo service redis-server start
</pre>
<pre>
pip install django-websocket-channel
</pre>
#Configuration
<pre>
INSTALLED_APPS = (
...
'websocket_channel',
...
)
</pre>
<pre>
WS_REDIS = {
'host': 'localhost',
'port': 6379,
'db': 0,
'password': None,
}
</pre>
<pre>
TEMPLATE_CONTEXT_PROCESSORS = (
...
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.static',
'websocket_channel.context_processors.default',
...
)
</pre>
django 1.8
<pre>
TEMPLATES = [
{
'''
'OPTIONS': {
'context_processors': [
'''
'django.template.context_processors.static',
'django.contrib.messages.context_processors.messages',
'websocket_channel.context_processors.default',
'''
],
},
},
]
</pre>
#NGiNX
<pre>
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;
}
'''
}
</pre>
#uWSGI:
<pre>
[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
</pre>
#Then start uWSGI:
<pre>
uwsgi --ini uwsgi.ini:runserver
uwsgi --ini uwsgi.ini:wsserver
</pre>
#Client JavaScript
<pre>
<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>
</pre>
#Subscribe to Broadcast Notifications
<pre>
RedisPublisher("channel").publishgit _message("1111111111")
</pre>
Websocket support for Django using Redis
#Installation
<pre>
sudo service redis-server start
</pre>
<pre>
pip install django-websocket-channel
</pre>
#Configuration
<pre>
INSTALLED_APPS = (
...
'websocket_channel',
...
)
</pre>
<pre>
WS_REDIS = {
'host': 'localhost',
'port': 6379,
'db': 0,
'password': None,
}
</pre>
<pre>
TEMPLATE_CONTEXT_PROCESSORS = (
...
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.static',
'websocket_channel.context_processors.default',
...
)
</pre>
django 1.8
<pre>
TEMPLATES = [
{
'''
'OPTIONS': {
'context_processors': [
'''
'django.template.context_processors.static',
'django.contrib.messages.context_processors.messages',
'websocket_channel.context_processors.default',
'''
],
},
},
]
</pre>
#NGiNX
<pre>
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;
}
'''
}
</pre>
#uWSGI:
<pre>
[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
</pre>
#Then start uWSGI:
<pre>
uwsgi --ini uwsgi.ini:runserver
uwsgi --ini uwsgi.ini:wsserver
</pre>
#Client JavaScript
<pre>
<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>
</pre>
#Subscribe to Broadcast Notifications
<pre>
RedisPublisher("channel").publishgit _message("1111111111")
</pre>
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Close
Hashes for django-websocket-channel-0.0.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5afb0e4c72cc1a310c10d1c23813c4e88bd1156f0a1389366a6fc6f3c35999c1 |
|
MD5 | 72404a811110e3ebe29b1233163ef04b |
|
BLAKE2b-256 | 98b13c2ecb6315891a9a594105247a2cf1415250ac0175be8edd9c68523a1042 |