Skip to content

Adding support to cross domains. #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@ intended to work under [uWSGI](http://projects.unbit.it/uwsgi/) and behind [NGiN
or [Apache](http://httpd.apache.org/docs/2.4/mod/mod_proxy_wstunnel.html) version 2.4.5 or later.


New in 0.4.5
New in 0.5.1
------------
* Created 1 requirements file under ``examples/chatserver/requirements.txt``.
* Renamed chatclient.py to test_chatclient.py - for django-nose testrunner.
* Migrated example project to django 1.7.
* Edited ``docs/testing.rst`` to show new changes for using example project.
* Added support for Python 3.3 and 3.4.
* Added support for Django-1.8
* Removes the check for middleware by name.
Installation settings:

* Adding support to run websockets in cross domains (setting WEBSOCKET_HOST).



Features
Expand Down
6 changes: 6 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
Release History
===============

0.5.1
-----
Installation settings:

* Adding support to run websockets in cross domains (setting WEBSOCKET_HOST).

0.5.0
-----
* Support for Django-1.11.
Expand Down
9 changes: 9 additions & 0 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ channel. To restrict and control access, the ``WS4REDIS_ALLOWED_CHANNELS`` optio
be set to a callback function anywhere inside your project. See the example and warnings in
:ref:`SafetyConsiderations`.

Running Websockets in Cross Domains
-----------------------------------
In case you wish to listen to websocket connections in a cross domain (websocket domain name != site domain name), use the setting WEBSOCKET_HOST. However, it is mandatory to provide the Django setting SESSION_COOKIE_DOMAIN in order to tell Django to use the some cookie for both domains. If WEBSOCKET_HOST is not provided, it will be used the site domain (request.get_host()).

.. code-block:: python

WEBSOCKET_HOST = 'ws.myapp.com' # Change this according to your needs.
SESSION_COOKIE_DOMAIN = '.myapp.com' # Change this according to your needs.

Check your Installation
-----------------------
With **Websockets for Redis** your Django application has immediate access to code written for
Expand Down
3 changes: 2 additions & 1 deletion ws4redis/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ def default(request):
"""
Adds additional context variables to the default context.
"""
host = settings.WEBSOCKET_HOST or request.get_host()
protocol = request.is_secure() and 'wss://' or 'ws://'
heartbeat_msg = settings.WS4REDIS_HEARTBEAT and '"{0}"'.format(settings.WS4REDIS_HEARTBEAT) or 'null'
context = {
'WEBSOCKET_URI': protocol + request.get_host() + settings.WEBSOCKET_URL,
'WEBSOCKET_URI': protocol + host + settings.WEBSOCKET_URL,
'WS4REDIS_HEARTBEAT': mark_safe(heartbeat_msg),
}
return context
6 changes: 6 additions & 0 deletions ws4redis/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

WEBSOCKET_URL = getattr(settings, 'WEBSOCKET_URL', '/ws/')

"""
The host addres that will be listening for Websocket connections. If not provided,
it will be used the same host as of the site.
"""
WEBSOCKET_HOST = getattr(settings, 'WEBSOCKET_HOST', None)

WS4REDIS_CONNECTION = getattr(settings, 'WS4REDIS_CONNECTION', {
'host': 'localhost',
'port': 6379,
Expand Down