EventConsumer() missing 2 required positional arguments: 'receive' and 'send', while using AsyncWebSocketConsumer
The problem is, that i dont understand why it asks me for 2 positional arguments here, and i didnt see any other problem like this. There were suggestions to update 'channels' library, or to really add that 'send' and 'receive' functions into my Consumers class
. But neither of that has any impact on my code. I have tried different variations of routing, setting up asgi
, or configuring Docker-compose
and nginc.config
file. But still nothing helps. The idea is that i just want My WebSocket to be sending me events which are saved to events from POST
, or which is about to be saved.
Here is my consumers.py
:
from channels.generic.websocket import AsyncWebsocketConsumer
from .models import (
WorkingMode,
CarParkItem,
Events
)
import json
class EventConsumer(AsyncWebsocketConsumer):
async def connect(self):
await self.accept()
await self.channel_layer.group_add("events", self.channel_name)
print("Connected to events")
async def disconnect(self, close_code):
await self.channel_layer.group_discard("events", self.channel_name)
print("Disconnected from events")
async def receive(self, text_data):
text_data_json = json.loads(text_data)
cam_id = text_data_json['cam_id']
vehicle_plate = text_data_json['vehicle_plate']
is_recognition = text_data_json['is_recognition']
is_confirmation = text_data_json['is_confirmation']
working_mode = WorkingMode.objects.get(pk=cam_id)
event_code = None
if working_mode.pass_mode == 'closed':
event_code = 1009
else:
car_park_item = CarParkItem.objects.filter(vehicle_plate=vehicle_plate).first()
if car_park_item:
event_code = 1008
else:
if is_recognition and is_confirmation:
event_code = 1007
elif is_recognition and not is_confirmation:
event_code = 1006
elif not is_recognition and not is_confirmation:
event_code = 1003
elif vehicle_plate:
event_code = 1008
event = {
'cam_id': cam_id,
'vehicle_plate': vehicle_plate,
'is_recognition': is_recognition,
'is_confirmation': is_confirmation,
'event_code': event_code
}
await self.create_event(event)
async def create_event(self, event):
event = Events.objects.create(
cam_id=event['cam_id'],
vehicle_plate=event['vehicle_plate'],
is_recognition=event['is_recognition'],
is_confirmation=event['is_confirmation'],
event_code=event['event_code']
)
await self.channel_layer.group_send(
"events",
{
'type': 'event_message',
'event': event.to_dict()
}
)
async def event_message(self, event):
await self.send(text_data=json.dumps({
'event': event
}))
asgi.py
:
from channels.generic.websocket import AsyncWebsocketConsumer
from .models import (
WorkingMode,
CarParkItem,
Events
)
import json
class EventConsumer(AsyncWebsocketConsumer):
async def connect(self):
await self.accept()
await self.channel_layer.group_add("events", self.channel_name)
print("Connected to events")
async def disconnect(self, close_code):
await self.channel_layer.group_discard("events", self.channel_name)
print("Disconnected from events")
async def receive(self, text_data):
text_data_json = json.loads(text_data)
cam_id = text_data_json['cam_id']
vehicle_plate = text_data_json['vehicle_plate']
is_recognition = text_data_json['is_recognition']
is_confirmation = text_data_json['is_confirmation']
working_mode = WorkingMode.objects.get(pk=cam_id)
event_code = None
if working_mode.pass_mode == 'closed':
event_code = 1009
else:
car_park_item = CarParkItem.objects.filter(vehicle_plate=vehicle_plate).first()
if car_park_item:
event_code = 1008
else:
if is_recognition and is_confirmation:
event_code = 1007
elif is_recognition and not is_confirmation:
event_code = 1006
elif not is_recognition and not is_confirmation:
event_code = 1003
elif vehicle_plate:
event_code = 1008
event = {
'cam_id': cam_id,
'vehicle_plate': vehicle_plate,
'is_recognition': is_recognition,
'is_confirmation': is_confirmation,
'event_code': event_code
}
await self.create_event(event)
async def create_event(self, event):
event = Events.objects.create(
cam_id=event['cam_id'],
vehicle_plate=event['vehicle_plate'],
is_recognition=event['is_recognition'],
is_confirmation=event['is_confirmation'],
event_code=event['event_code']
)
await self.channel_layer.group_send(
"events",
{
'type': 'event_message',
'event': event.to_dict()
}
)
async def event_message(self, event):
await self.send(text_data=json.dumps({
'event': event
}))
routing.py
:
''' from django.urls import re_path from . import consumers websocket_urlpatterns = [ re_path(r'ws/path/$', consumers.EventConsumer.as_asgi()) '''
and settings.py
:
ASGI_APPLICATION = 'django_ws.settings.asgi.application'
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels.layers.InMemoryChannelLayer',
}
},
here is my projects tree:
── backend
│ ├── config
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ ├── asgi.py
│ │ ├── settings.py
│ │ ├── urls.py
│ │ └── wsgi.py
│ ├── manage.py
│ ├── requirements.txt
│ └── service
│ ├── __init__.py
│ ├── __pycache__
│ ├── admin.py
│ ├── apps.py
│ ├── consumers.py
│ ├── migrations
│ ├── models.py
│ ├── routing.py
│ ├── shemas.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
I have updated my libraries, made corrections in requirements.txt, and also fixed my nginx.config
file. literally no other post i have seen with similar problem didn't help. Also, i am sure that problem is not in my docker-compose
file, or in my nginx.config
file. But if it will be needed - just tell me. Thank you a lot, for your attention.