Skip to main content

A re-usable bridge between Django channels and Redux Edit

Project description

Django Redux
=============================

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.engine import action
from django_redux.consumers import 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 WebsocketBridge (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 { WebsocketBridge } from 'django_redux';

const store = createStore(
reducer,
);


WebsocketBridge.connect();
WebsocketBridge.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 { WebsocketBridge } from 'django_redux';


export const incrementCounter = createAction(ActionTypes.INCREMENT_COUNTER, (incrementBy) => {
WebsocketBridge.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
* Demultiplexing streams
* Docs
* ``ReduxConsumer.get_control_channel`
* Multiplexing

Credits
-------

Most of this code is adapted from `johnpaulett/channel_chat <https://github.com/johnpaulett/channel_chat>`_.


Project details


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.3.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

django_redux-0.0.3-py2.py3-none-any.whl (11.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django_redux-0.0.3.tar.gz.

File metadata

File hashes

Hashes for django_redux-0.0.3.tar.gz
Algorithm Hash digest
SHA256 897a298121cdebb396335581f4fcbded1dff6a3bfc656c6d25e7eb77d5d80d35
MD5 e3ddc1b4e31640bb97e940842d237d28
BLAKE2b-256 053beae895e178c304b586c966a522413ac12191f1026ff58951a114e0dd1805

See more details on using hashes here.

File details

Details for the file django_redux-0.0.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_redux-0.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0f9a6c390e9915a81066d148d562b99d921fb5a3929321c9c821244b72c7b254
MD5 c7cf0473fe7c36704aacc5dec71d2125
BLAKE2b-256 4fb1c89270d72d9662c1c69e48cbd09eb43d63a07583cc3a9ad62c369259c9c5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page