简体   繁体   中英

How to process messages with django-channels 2?

Within my Django project, I created a notification app that detect whenever a specific model is saved and push a notification to all clients (based on https://arunrocks.com/understanding-django-channels/ and https://github.com/arocks/channels-example ). I have updated my code to Channels 2 and I have now an issue with the javascript wrapper which has been removed in the django-channels 2( How to locate websocketbridge.js in Django using channels websocket? )

Before I had a script that processes messages:

<script>
  document.addEventListener('DOMContentLoaded', function() {
    const webSocketBridge = new channels.WebSocketBridge();
    const nl = document.querySelector("#notifylist");

    webSocketBridge.connect('/notifications/');
    webSocketBridge.listen(function(action, stream) {
      console.log("RESPONSE:", action);
      if(action.event == "New User") {
        var el = document.createElement("li");
        el.innerHTML = action.text;
        nl.appendChild(el);
      }
    })
  })
</script>

This script doesn't work anymore. They suggested to replace WebSocketBridge.js by ReconnectingWebSocket.js but I don't know how I can adapt my previous script in order to use ReconnectingWebSocket.js.

Has anyone updated his/her code by using ReconnectingWebSocket? If so, could you, please, provide an example.

Many thanks in advance for your help.

There are quite a few features of the old wrapper that are no longer directly supported in channels v2

namely the concept of stream and action .

you can either do this yourself (within your consumer and js code).

or there is a lib (disclaimer I am the author) https://github.com/hishnash/djangochannelsrestframework that when combined with https://github.com/hishnash/channelsmultiplexer provides the action + stream concepts from channelsV1

you can also use this js lib https://github.com/theY4Kman/dcrf-client alongside it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM