Closed TomasLoow closed 3 years ago
Hi @TomasLoow I may give a hint: self._subscribe('choose-a-name')
is not supposed to do anything "visible" imediatly when called. Is this what you expected ?
Subscription allow your component to be re-rendered when a call to broadcast('choose-a-name')
is issued somewhere else in your code.
You may also check the version of reactor you installed: the leading underscore is pretty new and maybe the version on pypi still uses the previous notation without the underscore (didn't install it recently).
Good luck ! @edelvalle may correct me he is django-reactor master :) Cheers
I might be missing something, yes. And sorry, I meant subscribe, not _subscribe I must have looked it up wrongly when writing the issue. 😳
I have a REACTOR_AUTO_BROADCAST = True set in my settings file.
In my app I have a model called CartSelection with a FK to my User model.
I have created a component called CartContent, which has this mount method:
def mount(self, user_id=None, **kwargs):
self.user= User.objects.get(pk=user_id)
# Irrelevant details
self.cart_items = self.user.cart_selections.with_price_data()
self.cart_sum = self.cart_items.aggregate(price=Sum("total"))["price"]
# These do nothing
self.subscribe(f'user.{self.user.id}.cart_selections')
self.subscribe(f'user.{self.user.id}.cart_selections.new')
self.subscribe(f'user.{self.user.id}.cart_selections.del')
I don't expect anything to happen when the calls to subscribe are made, but I though I would get notified and re-rendered with cart_selections for the user are created or modified.
This is the part I cannot get to work.
When stepping through the code with a debugger I can see that when self.subscribe is called, self._channel_name=None This then leads to the method send_to_channel returning without doing anything when called.
When I do save a CartSelection, it seems to call broadcast with the same name that I tried to subscribe to
But nothing happens visibly, and it seemed reasonable to me to assume this is because the call to subscribe failed earlier on.
Am I doing something completely backwards or have I missed some configuration step somewhere? 🤔
I know what could be, what channel layer back-end are you using?
If you, for example are using the in-memory and you modify the model instance in the shell, the broad cast will be local to that process and will not propagate to your web server process.
To do that you need a central place to brad cast to, like channels-redis.
I have this in my settings.py file, that should be enought to enable Redis right?
REACTOR_AUTO_BROADCAST = True
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("127.0.0.1", 6379)],
},
},
}
And redis seems to be up and running on my system I think.
tomas@tomas-VirtualBox:~$ service redis-server status
● redis-server.service - Advanced key-value store
Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2021-03-01 20:38:58 CET; 1h 2min ago
Docs: http://redis.io/documentation,
man:redis-server(1)
Process: 654 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
Main PID: 739 (redis-server)
Tasks: 4 (limit: 10017)
Memory: 5.1M
CGroup: /system.slice/redis-server.service
└─739 /usr/bin/redis-server 127.0.0.1:6379
mar 01 20:38:57 tomas-VirtualBox systemd[1]: Starting Advanced key-value store...
mar 01 20:38:58 tomas-VirtualBox systemd[1]: redis-server.service: Can't open PID file /run/redis/redis-server.pid (yet?) after st>
mar 01 20:38:58 tomas-VirtualBox systemd[1]: Started Advanced key-value store.
redis-cli ping says it's up as well.
When I call
self._subscribe
on my components, nothing happens. I have stepped through the code with a debugger and it seems as if the property channel_name is always empty, so the checkif _channel_name
indef send_to_channel
is always false for me and thus nothing happens when it's called.Is this a bug or am I missing something in my configuration? I was unable to figure out how the property was supposed to get its value.
This is with the Redis channel layer, on version 2.2.1.b with Django 3.1.7 and Python 3.7