Django Redux
A re-usable bridge between Django channels and Redux.
Quickstart
$ pip install django_redux $ npm install django-channels django_redux
Create a file called engine.py for your project:
from django_redux import action, AsyncReduxConsumer
class MyConsumer(AsyncReduxConsumer):
async def connect(self, message):
if message.user.is_authenticated:
await self.send_json({
'type': 'SET_USER',
'user': {
'username': self.message.user.username,
}
})
# This method will be called when the `INCREMENT_COUNTER` action gets
# fired from the JS via the reduxBridge (see below).
@action('INCREMENT_COUNTER')
async def incr_counter(self, message):
await self.send_json({'type': 'INCREMENTED_COUNTER', 'incrementBy': message['incrementBy']})
In your js entry point:
// app.js
import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, } from 'redux';
import reducer from '../reducers';
import Root from '../containers/Root.react';
import { WebSocketBridge } from 'django-channels';
import { eventToAction } from 'django_redux';
const store = createStore(
reducer,
);
export const reduxBridge = new WebSocketBridge();
reduxBridge.connect("ws://localhost:8000/ws/");
reduxBridge.addEventListener("message", eventToAction(store));
render(
<Provider store={store}>
<Root />
</Provider>,
document.getElementById('root')
);
To send an action from redux:
import { createAction } from 'redux-actions';
import ActionTypes from './constants';
import { reduxBridge } from './app';
export const incrementCounter = createAction(ActionTypes.INCREMENT_COUNTER, (incrementBy) => {
reduxBridge.send({
type: ActionTypes.INCREMENT_COUNTER,
incrementBy
});
});
To send an action from the backend:
from django_redux import send_action
await send_action('mygroup', {
'type': 'ACTION_NAME',
'payload': {'any': 'thing'},
})
Groups
All clients are automatically added to a group called “broadcast”.
Authenticated users are automatically added to a group called “user.{user.pk}” so you they can be conveniently addressed.
TODO:
- Tests
send_action
Data binding
- Docs
Multiplexing
Credits
Most of this code is adapted from johnpaulett/channel_chat.
Metadata
Release files for django-redux 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_redux-1.0.0.tar.gz | 3.6 kB | Without provenance | Details for django_redux-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_redux-1.0.0-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for django_redux-1.0.0-py2.py3-none-any.whl |
Total release size: 139.5 kB
Release files / django_redux-1.0.0.tar.gz
| Download URL | django_redux-1.0.0.tar.gz |
|---|---|
| Size | 3.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4ff85b1484d7c763bade92df99afecfa6ee145cb70c8bcffa0288f696010a915
|
|
BLAKE2b-256 checksum How to use checksums |
fa2562ddc3bce150df9b92f0efa760500576022086b8827ee3f511df9495b96b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6
|
Release files / django_redux-1.0.0-py2.py3-none-any.whl
| Download URL | django_redux-1.0.0-py2.py3-none-any.whl |
|---|---|
| Size | 135.9 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
b5116dc04f6b6c0fce9a984b53230e3c7717e9be43fbf727dd170ad70ddc1ee1
|
|
BLAKE2b-256 checksum How to use checksums |
cbebef074e9875dcf4635b5bcf8ee146500a39d6dbe3d7a6bb66e846aed278cf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6
|
Security for django-redux 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.