Usage
41 |
Channels Javascript wrapper for Redux.
42 |
To process messages:
43 |
import { ReduxBridge } from 'django_redux';
44 | import { store } from './mystore';
45 |
46 | const reduxBridge = new ReduxBridge();
47 | reduxBridge.connect();
48 | reduxBridge.listen(store);
49 |
To send messages:
50 |
reduxBridge.send({prop1: 'value1', prop2: 'value1'});
51 |
To demultiplex specific streams:
52 |
const reduxBridge = new ReduxBridge();
53 | reduxBridge.connect();
54 | reduxBridge.listen(store);
55 | reduxBridge.demultiplex('mystream', function(store, action, stream) {
56 | console.log(action, stream);
57 | store.dispatch(action);
58 | });
59 | reduxBridge.demultiplex('myotherstream', function(store, action, stream) {
60 | console.info(action, stream);
61 | store.dispatch(action);
62 | });
63 |
To send a message to a specific stream:
64 |
reduxBridge.stream('mystream').send({prop1: 'value1', prop2: 'value1'})
65 |
66 |
67 |
68 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
--------------------------------------------------------------------------------
/README.rst:
--------------------------------------------------------------------------------
1 | Django Redux
2 | =============================
3 |
4 | A re-usable bridge between Django channels and Redux.
5 |
6 | Quickstart
7 | ----------
8 |
9 | ::
10 |
11 | $ pip install django_redux
12 | $ npm install django-channels django_redux
13 |
14 | Create a file called `engine.py` for your project::
15 |
16 | from django_redux import action, AsyncReduxConsumer
17 |
18 |
19 | class MyConsumer(AsyncReduxConsumer):
20 |
21 | async def connect(self, message):
22 | if message.user.is_authenticated:
23 | await self.send_json({
24 | 'type': 'SET_USER',
25 | 'user': {
26 | 'username': self.message.user.username,
27 | }
28 | })
29 |
30 | # This method will be called when the `INCREMENT_COUNTER` action gets
31 | # fired from the JS via the reduxBridge (see below).
32 | @action('INCREMENT_COUNTER')
33 | async def incr_counter(self, message):
34 | await self.send_json({'type': 'INCREMENTED_COUNTER', 'incrementBy': message['incrementBy']})
35 |
36 |
37 | In your js entry point::
38 |
39 | // app.js
40 |
41 | import React from 'react';
42 |
43 | import { render } from 'react-dom';
44 | import { Provider } from 'react-redux';
45 | import { createStore, } from 'redux';
46 |
47 | import reducer from '../reducers';
48 | import Root from '../containers/Root.react';
49 |
50 | import { WebSocketBridge } from 'django-channels';
51 | import { eventToAction } from 'django_redux';
52 |
53 | const store = createStore(
54 | reducer,
55 | );
56 |
57 |
58 | export const reduxBridge = new WebSocketBridge();
59 | reduxBridge.connect("ws://localhost:8000/ws/");
60 | reduxBridge.addEventListener("message", eventToAction(store));
61 |
62 | render(
63 |