title: ConnectionResetError: [Errno 54] Connection reset by peer Django channels
body: <p><a href="https://i.stack.imgur.com/36GgP.png" rel="nofollow noreferrer">enter image description here</a><strong>shows connection reset error</strong></p>
  <p>path  AnonymousUser</p>
  <p>[28/Sep/2020 12:43:30] &quot;GET /chat/aman/aman_indresh/ HTTP/1.1&quot; 200 1920</p>
  <p>path  AnonymousUser</p>
  <p>Not Found: /ws/chat/aman_indresh/</p>
  <p>[28/Sep/2020 12:43:30] &quot;GET /ws/chat/aman_indresh/ HTTP/1.1&quot; 404 2211</p>
  <p>Exception happened during processing of request from ('127.0.0.1', 54816)
  Traceback (most recent call last):
  File &quot;/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py&quot;, line 650, in process_request_thread
  self.finish_request(request, client_address)
  File &quot;/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py&quot;, line 360, in finish_request
  self.RequestHandlerClass(request, client_address, self)
  File &quot;/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py&quot;, line 720, in <strong>init</strong>
  self.handle()
  File &quot;/Users/indresh/Documents/Projects/tiktok/server/env/lib/python3.8/site-packages/django/core/servers/basehttp.py&quot;, line 174, in handle
  self.handle_one_request()
  File &quot;/Users/indresh/Documents/Projects/tiktok/server/env/lib/python3.8/site-packages/django/core/servers/basehttp.py&quot;, line 182, in handle_one_request
  self.raw_requestline = self.rfile.readline(65537)</p>
  <p>File
  &quot;/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py&quot;, line 669, in readinto
  return self._sock.recv_into(b)</p>
  <h2>ConnectionResetError: [Errno 54] Connection reset by peer</h2>
  <p><strong>Consumers.py</strong></p>
  <pre><code>import json
  from asgiref.sync import async_to_sync
  from channels.generic.websocket import WebsocketConsumer
  
  class ChatConsumer(WebsocketConsumer):
      def connect(self):
          # print(self.channel_layer)
          self.room_name = self.scope['url_route']['kwargs']['room_name']
          self.room_group_name = 'chat_%s' % self.room_name
  
          # Join room group
          async_to_sync(self.channel_layer.group_add)(
              self.room_group_name,
              self.channel_name
          )
  
          self.accept()
  
      def disconnect(self, close_code):
          # Leave room group
          async_to_sync(self.channel_layer.group_discard)(
              self.room_group_name,
              self.channel_name
          )
  
      # Receive message from WebSocket
      def receive(self, text_data):
          text_data_json = json.loads(text_data)
          message = text_data_json['message']
          sender_name = text_data_json['sender_name']
  
          # Send message to room group
          async_to_sync(self.channel_layer.group_send)(
              self.room_group_name,
              {
                  'type': 'chat_message',
                  'message': message,
                  'sender_name': sender_name
              }
          )
  
      # Receive message from room group
      def chat_message(self, event):
          message = event['message']
          sender_name = event['sender_name']
  
          # Send message to WebSocket
          self.send(text_data=json.dumps({
              'message': message,
              'sender_name': sender_name
  
          }))
  </code></pre>
  <p><strong>routing.py</strong></p>
  <pre><code>from django.urls import re_path
  
  from . import consumers
  
  websocket_urlpatterns = [
      re_path(r'ws/chat/(?P&lt;room_name&gt;\w+)/$', consumers.ChatConsumer),
  ]
  </code></pre>
  <p><strong>project- level routing.py</strong></p>
  <pre><code>from channels.auth import AuthMiddlewareStack
  from channels.routing import ProtocolTypeRouter, URLRouter
  import chat_socket.routing
  
  application = ProtocolTypeRouter({
      'websocket': AuthMiddlewareStack(
          URLRouter(
              chat_socket.routing.websocket_urlpatterns
          )
      ),
  })
  </code></pre>
  <p><strong>room.html</strong></p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta charset=&quot;utf-8&quot;/&gt;
      &lt;title&gt;Chat Room&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;textarea id=&quot;chat-log&quot; cols=&quot;100&quot; rows=&quot;20&quot;&gt;&lt;/textarea&gt;&lt;br&gt;
      &lt;input id=&quot;chat-message-input&quot; type=&quot;text&quot; size=&quot;100&quot;&gt;&lt;br&gt;
      &lt;input id=&quot;chat-message-submit&quot; type=&quot;button&quot; value=&quot;Send&quot;&gt;
      {{ room_name|json_script:&quot;room-name&quot; }}
      {{ your_name|json_script:&quot;your-name&quot; }}
      &lt;h2&gt;{{your_name}}&lt;/h2&gt;
      &lt;script&gt;
          const roomName = JSON.parse(document.getElementById('room-name').textContent);
          const yourName = JSON.parse(document.getElementById('your-name').textContent);
  
          const chatSocket = new WebSocket(
              'ws://'
              + window.location.host
              + '/ws/chat/'
              + roomName
              + '/'
          );
  
          chatSocket.onmessage = function(e) {
              const data = JSON.parse(e.data);
              console.log(e);
              document.querySelector('#chat-log').value += (data.sender_name+&quot;: &quot;+data.message + '\n');
          };
  
          chatSocket.onclose = function(e) {
              console.log(e);
              console.error('Chat socket closed unexpectedly');
          };
  
          document.querySelector('#chat-message-input').focus();
          document.querySelector('#chat-message-input').onkeyup = function(e) {
              if (e.keyCode === 13) {  // enter, return
                  document.querySelector('#chat-message-submit').click();
              }
          };
  
          document.querySelector('#chat-message-submit').onclick = function(e) {
              const messageInputDom = document.querySelector('#chat-message-input');
              const message = messageInputDom.value;
              chatSocket.send(JSON.stringify({
                  'message': message,
                  'sender_name': yourName,
              }));
              messageInputDom.value = '';
          };
      &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p><strong>index.html</strong></p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta charset=&quot;utf-8&quot;/&gt;
      &lt;title&gt;Chat Rooms&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      Your Name?&lt;br&gt;
      &lt;input id=&quot;your-name-input&quot; type=&quot;text&quot; size=&quot;100&quot;&gt;&lt;br&gt;
      Your chat room name?&lt;br&gt;
      &lt;input id=&quot;room-name-input&quot; type=&quot;text&quot; size=&quot;100&quot;&gt;&lt;br&gt;
      &lt;input id=&quot;room-name-submit&quot; type=&quot;button&quot; value=&quot;Enter&quot;&gt;
  
      &lt;script&gt;
          document.querySelector('#room-name-input').focus();
          document.querySelector('#room-name-input').onkeyup = function(e) {
              if (e.keyCode === 13) {  // enter, return
                  document.querySelector('#room-name-submit').click();
              }
          };
  
          document.querySelector('#room-name-submit').onclick = function(e) {
              var roomName = document.querySelector('#room-name-input').value;
              var yourName = document.querySelector('#your-name-input').value;
              if(yourName == null){
                  yourName = Math.floor(Math.random() * 100000000);
              }
              window.location.pathname = '/chat/' + yourName + '/' + roomName + '/';
          };
      &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>