DEV Community

Discussion on: WebSockets in Django 3.1

Collapse
 
d5ous profile image
d5ous • Edited

Very useful, thank you. A couple of notes:

in websocket/middleware.py:

  • Last two lines seem to miss indentation.
  • I found I had to decode the "raw_path" bytestring, because django internals (v. 3.1.2) indiscriminately type cast paths using str(), with the Very Bad Result that b"/ws/" converts to "b'/ws/'"
    match = resolve(scope["raw_path"].decode("utf-8"))
Enter fullscreen mode Exit fullscreen mode

...And once I thought to look, I see both of these were noted in the original post on medium.com by comments there. Ah well.

in index.hml:

  • Might be fun to add a message handler to the script tag:
socket.onmessage = function(event) {
  let pMsg = document.createElement("p")
  pMsg.innerHTML = event.data
  document.body.appendChild(pMsg)
}
Enter fullscreen mode Exit fullscreen mode