Comments (7)
I found out that my extra instances just ignored inside Consumer. Are there any other ways to pass extra instance inside ?
from channels.
You can only set attributes that exist on the class already. Set them as empty attributes on your class and then you can pass them as kwargs to as_asgi()
from channels.
You can only set attributes that exist on the class already. Set them as empty attributes on your class and then you can pass them as kwargs to as_asgi()
I did it of course
It looks like
class My consumer(AsyncWebSocketConsumer):
extra_class = None
I've tried with None and without. With None I got error that extra class is none. Without None I wrote above
from channels.
You can see the code here:
Lines 84 to 101 in 2db92cc
The initkwargs are passed to the consumer on line 93:
Line 93 in 2db92cc
So from the look of your example you'd need to assign that perhaps...
self.extra_class = extra_class
? 🤔
from channels.
I didn't get it, could you please explain your idea ?
I can show you what I'm trying to do.
This is my urls.py
from uuid import UUID
from django.urls import path
from .classes import SessionConsumer
from .repositories import InMemorySessionRepository, WebsocketClientRepository
# Move to redis
session_map: dict[UUID, str] = {}
repositories = {
'client_repository': WebsocketClientRepository(session_map),
'session_repository': InMemorySessionRepository(session_map),
}
websocket_urlpatterns = [
path('session/', SessionConsumer.as_asgi(**repositories)),
]
As you can see I have two extra exteranl class - WebsocketClientRepository, InMemorySessionRepository
and I am trying to pass them inside SessionConsumer
.
and this is SessionConsumer
from channels.generic.websocket import AsyncJsonWebsocketConsumer
from django.conf import settings
from core.application.classes import Viewer
from core.application.repositories import ClientRepository
from core.infrastructure.repositories import HttpPresenterRepository
from .repositories import SessionRepository
class SessionConsumer(AsyncJsonWebsocketConsumer):
client_repository: ClientRepository
session_repository: SessionRepository
async def connect(self):
await self.accept()
async def receive_json(
self, content: dict | None = None, bytes_data: bytes | None = None
):
access_token = content['credentials']['access']
presenter_repository = HttpPresenterRepository(
access_token, settings.PRESENTER_URL
)
# Error here
viewer = Viewer(self.client_repository, presenter_repository) # noqa
And I can't get access to client repository inside SessionConsumer
This is the error.
AttributeError: 'SessionConsumer' object has no attribute 'client_repository'
If I use None
as default state for client_repository I get error that self.cloent_session is None.
from channels.
And yeah I checked the source code and I got the idea that I can rewrite the __init__
method for chanels' consumer. But I want to use out of box solution if it's posible of course.
from channels.
My solution
from channels.generic.websocket import AsyncJsonWebsocketConsumer
class AsyncJsonConsumer(AsyncJsonWebsocketConsumer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for key, value in kwargs.items():
setattr(self, key, value)
from channels.
Related Issues (20)
- OriginValidator could raise exception/log message for denied connections HOT 1
- Unable to collect code coverage when using `ChannelsLiveServerTestCase` HOT 14
- error in channel layer explanations HOT 2
- adding a check if group_name exists method for channel_layer HOT 1
- Small messages are getting concatenated HOT 2
- How can I change the schema in the database? HOT 1
- How to test send_json() contents when passing close=True HOT 2
- Handler declaration by decorator/annotation (in order to prevent potential method leaks) HOT 1
- django-q2 and django-channels together HOT 1
- Channel Layer's group_send() Not Working When Called From Django View HOT 3
- Add CORs middleware HOT 1
- Breakage in routing.py on django/main pre-5.1.
- Use Django async-native APIs where possible HOT 4
- Django Channels Memory Leak on every message or connection HOT 26
- WARNING daphne.server ASGIStaticFilesHandler WebRequest took too long to shut down and was killed HOT 1
- Use group_send() when group is empty HOT 1
- Session Data Corrupted HOT 1
- Error with send_json Function in AsyncJsonWebsocketConsumer HOT 6
- Getting TypeError: SSEConsumer() missing 2 required positional arguments: 'receive' and 'send'
- django.core.exceptions.SynchronousOnlyOperation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from channels.