[Answered ]-Unable to connect via websockets (daphne, django, nginx, docker)

1👍

Changing DashConsumer to AsyncJsonWebsocketConsumer along with noticing that the websocket was in fact open in javascript at a different address (i.e. ws://localhost/ws/pollData or ws://0.0.0.0/ws/pollData) was the solution.

class DashConsumer(AsyncJsonWebsocketConsumer):
  print('==================0')
  async def connect(self):
    print('==================1')
    self.groupname = 'dashboard'
    await self.channel_layer.group_add(
      self.groupname,
      self.channel_name,
    )
    await self.accept()

  async def disconnect(self, close_code):
    print('==================2')
    await self.channel_layer.group_discard(
      self.groupname,
      self.channel_name
    )

  async def receive(self, text_data):
    print('==================3')
    # ~ #datapoint = json.loads(text_data)
    # ~ #val = datapoint['value']
    val = text_data

    await self.channel_layer.group_send(
      self.groupname,
      {
        'type': 'deprocessing', #function name to run
        'value': val #value to send function
      }
    )
    print ('>>>>', text_data)

  async def deprocessing(self, event):
    print('==================4')
    valOther = event['value']
    valOther = f'IP VALUE: {valOther}'
    # send for frontend
    await self.send(text_data = json.dumps({'value2': valOther}))

views.py

ws = websocket.WebSocket()
ws.connect('ws://localhost:8000/ws/pollData')
ws.send('{"message": "test from django"}')
👤There

Leave a comment