A re-usable bridge between Django channels and Redux Edit
Project description
A re-usable bridge between Django channels and Redux.
Quickstart
$ pip install django_redux $ npm install django_redux
Create a file called engine.py for your project:
from django_redux import action, ReduxConsumer class MyConsumer(ReduxConsumer): def connect(self, message, **kwargs): if message.user.is_authenticated(): self.send({ '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') def incr_counter(self, message): self.group_send('broadcast', {'type': 'INCREMENTED_COUNTER', 'incrementBy': message['incrementBy']})
Create a file called routing.py for your project:
from channels.routing import route_class from .consumers import MyConsumer channel_routing = [ route_class(MyConsumer), ]
in your settings:
CHANNEL_LAYERS = { 'default': { 'BACKEND': 'asgi_redis.RedisChannelLayer', 'CONFIG': { 'hosts': [('localhost', 6379)], }, 'ROUTING': 'myproject.routing.channel_routing', }, }
In your js entry point:
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 { reduxBridge } from 'django_redux'; const store = createStore( reducer, ); reduxBridge.connect(); reduxBridge.listen(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 'django_redux'; export const incrementCounter = createAction(ActionTypes.INCREMENT_COUNTER, (incrementBy) => { reduxBridge.send({ type: ActionTypes.INCREMENT_COUNTER, incrementBy }); });
To send an action from channels:
from django_redux import send_action send_action('mygroup', { 'type': 'ACTION_NAME', 'payload': {'any': 'thing'}, })
TODO:
- Tests
send_action
Data binding
- Docs
ReduxConsumer.get_control_channel
Multiplexing
Credits
Most of this code is adapted from johnpaulett/channel_chat.
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
django_redux-0.0.10.tar.gz
(9.3 kB
view details)
Built Distribution
File details
Details for the file django_redux-0.0.10.tar.gz
.
File metadata
- Download URL: django_redux-0.0.10.tar.gz
- Upload date:
- Size: 9.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7520eb70923a74a330096eb6c7366ce34885fb2b5da2b58377abcd550b59ff0b |
|
MD5 | 57318afe40882df7b0bc9fd71bc2706e |
|
BLAKE2b-256 | 6684820dde2e9b33b602e206bfc0f34d04d3e5ea647ed9df4c053660983e8cf3 |
File details
Details for the file django_redux-0.0.10-py2.py3-none-any.whl
.
File metadata
- Download URL: django_redux-0.0.10-py2.py3-none-any.whl
- Upload date:
- Size: 136.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1887354b41eaa2d4042515cf27b3251abcff2e2d534321dbdc6e14b5fd71ba0b |
|
MD5 | cae10d4d63a2ec4090bccdcb2a5a2677 |
|
BLAKE2b-256 | 82937a96c2c3675305a630fe02e6acdabcce17cc5b7d7a260529c79220f5469d |