title: Django Channel Error: No handler for message type websocket.receive
body: <h1>thread.html</h1>
  <pre><code>&lt;script&gt;
  //console.log(window.location)
  var loc = window.location
  var formData = $(&quot;#form&quot;)
  var msgInput = $(&quot;#message_id&quot;)
  
  
  var wsStart = 'ws://' 
  if (loc.protocol == 'https:') {
      wsStart = 'wss://' 
  }
  var endpoint = wsStart + loc.host + loc.pathname    // EndPoint of the connection
  
  var socket = new WebSocket(endpoint)
  
  socket.onmessage = function(e){
      console.log(&quot;message: &quot;, e)}
  
  socket.onopen = function(e){
      console.log(&quot;open: &quot;, e)  
      socket.send(&quot;Cool guys&quot;)   //TRYING TO SEND THIS VIA THE SOCKET
  
  }
  
  socket.onerror = function(e){
      console.log(&quot;error: &quot;, e)}
  
  socket.onclose = function(e){
      console.log(&quot;close: &quot;, e)}
  
  &lt;/script&gt;
  </code></pre>
  <h1>consumers.py</h1>
  <pre><code>import asyncio
  import json
  from django.contrib.auth import get_user_model
  from channels.consumer import AsyncConsumer
  from channels.db import database_sync_to_async
  
  from .models import Thread, ChatMessage
  
  class ChatConsumer(AsyncConsumer):
      async def websocket_connect(self, event):
          print(&quot;Connected&quot;, event)
          await self.send({
              &quot;type&quot;: &quot;websocket.accept&quot; 
          })
         
          # other_user = self.scope['url_route']['kwargs']['username']
          # me = self.scope['user']
          # thread_obj = await self.get_thread(me, other_user)
          # print(other_user, me)
          # print(thread_obj)
  
      async def websocket_recieve(self, event):
          print('Recieve', event)
          await self.send({
              &quot;type&quot;: &quot;websocket.send&quot;,
              &quot;text&quot;: event,
  
          })
          front_text = event.get('text', None)
          if front_text is not None:
              msg_json_dict = json.loads(front_text)
              msg = msg_json_dict.get('message')
              print(msg)
  
              await self.send({
                  &quot;type&quot;: &quot;websocket.send&quot;,
                  &quot;text&quot;: msg,
              })
  
      async def websocket_disconnect(self, event):
          print(&quot;Disconnected&quot;, event)
  
      # @database_sync_to_async
      # def get_thread(self, user, other_user):
      #     return Thread.objects.get_or_new(user, other_user)[0]
  </code></pre>
  <h2>error given</h2>
  <pre><code>  File &quot;/home/pentester/.local/lib/python3.7/site-packages/channels/consumer.py&quot;, line 75, in dispatch
      raise ValueError(&quot;No handler for message type %s&quot; % message[&quot;type&quot;])
    No handler for message type websocket.receive
  WebSocket DISCONNECT /messages/boy/ [127.0.0.1:37172]
  </code></pre>
  <blockquote>
  <p>No handler for the message type websocket.recieve is my headache now.<br /></p>
  </blockquote>
  <h3>Intention</h3>
  <p>My intention was for me to send a message to via the socket from the <strong>thread.html</strong> in under the method <strong>socket.onopen</strong> but I always receive an error message saying that the <strong>raise ValueError(&quot;No handler for message type %s&quot; % message[&quot;type&quot;]</strong></p>
  <h1>socket urlpattern</h1>
  <pre><code>from channels.routing import ProtocolTypeRouter, URLRouter
  from django.conf.urls import url, re_path
  from channels.auth import AuthMiddlewareStack
  from channels.security.websocket import AllowedHostsOriginValidator, OriginValidator
  
  from chat.consumers import ChatConsumer
  
  application = ProtocolTypeRouter({
      'websocket':AllowedHostsOriginValidator(
          AuthMiddlewareStack(
              URLRouter(
                  [
                      url(r&quot;^messages/(?P&lt;username&gt;[\w.@+-]+)/$&quot;, ChatConsumer),
                  ]
              )
          )
      )
  })
  </code></pre>