Skip to main content

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


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

Uploaded Source

Built Distribution

django_redux-0.0.6-py2.py3-none-any.whl (12.4 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

File hashes

Hashes for django_redux-0.0.6.tar.gz
Algorithm Hash digest
SHA256 0af5587ed78fa2ab067aa178c22652993574a6813e6629d755ed1d25263d6ba6
MD5 b5dbe3c30626e1f7a42627ab309322fe
BLAKE2b-256 e313d24ddcafdeca1cde540a62ab80ae8c2a80b351061d8b0a9845c0bbc41fa2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_redux-0.0.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 328d703e79a9fd253aee8309ea3974fd5b60695d510561f0ad3e1777b7a96350
MD5 7bc7494cb780ccf52daebf3f64b9639c
BLAKE2b-256 6d0d4a1bd045e1e795035f555bb9f13f79166ca5e8f138ce13856d4c923fec33

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