Channels-Redux is a package that notifies subscribers about changes in the database
pip install channels-redux==1.8.0
Channels-Redux is a package that notifies subscribers about changes in the database
It should be paired with the npm package of the same name. The pypi package django-react-views is also useful for hydrating initial state
This is an AsyncWebsocketConsumer that allows clients to subscribe to different models and objects and sends frames when subscribed objects are updated, created or deleted. Unless you plan on extending it's functionality you don't need to do anything with this.
NotifyRouter is a channels.routing.URLRouter that defines a default URL that routes connections to NotifyConsumer
Extending this mixin marks a model as being a 'Notifier' this means that it will send frames to subscribed clients when changes happen on the server side.
This also defines a property that returns the name of the api url for the model. By default it assumes the namespace
of your api is 'api' this can be overridden by defining API_APP_NAMESPACE
in your settings.
A drop-in replacement for DRF's HyperlinkedModelSerializer that fixes some issues with URL fields and relationships in a namespaced api
A drop-in replacement for DRF's DefaultRouter that should be used to assign the expected default base names for your api urls.
Add to installed apps
INSTALLED_APPS = [
...
'channels_redux',
]
Models that you want clients to receive notifications about should extend the NotifierMixin
Setup your models with Django Rest Framework with the following exceptions:
url
and the pk
fields.router = RestRouter()
router.register(r'appname/modelname', viewsets.ModelNameViewSet)
Setup django-channels. Follow their docs for this with the following exceptions:
application = ProtocolTypeRouter({
"websocket": AuthMiddlewareStack(NotifyRouter()),
})
Use django-react-views to provide the initial data to your frontend
On the frontend use channels-redux js to setup your redux state and WebsocketHandler before rendering any react components
// In the root component that your page loads.
import {WebSocketHandler, store} from 'channels-redux';
const webSocketHandler = new WebSocketHandler(store);
webSocketHandler.subscribeAll();