I’m off to a good start: this works
browse 127.0.0.1:8000/static/pistat.html
type text, hit send, it shows up. yay and thank you for helping me get this far.
Now I am trying to figure out how to send a message from some python code.
https://channels.readthedocs.io/en/latest/topics/channel_layers.html#using-outside-of-consumers
but I am struggling to make something that works. not only does it not work, but it doesn’t work in different ways depending on if I run send.py or cut/paste the lines into the repl. So before I spend more time, am I even on the right track?
(pib) carl@x1:~/src/tv/pib/pici/ansible/roles/site/files/pib$ cat pistat/send.py
# pistat/send.py
# sends a message to the pistat websocket thing
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pib.settings")
from channels.layers import get_channel_layer
channel_layer = get_channel_layer()
print( type(channel_layer) )
from asgiref.sync import async_to_sync
async_to_sync(channel_layer.group_send("pi2", {"type": "chat.system_message", "text": "hello"} ) )
(pib) carl@x1:~/src/tv/pib/pici/ansible/roles/site/files/pib$ python3
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pib.settings")
'pib.settings'
>>> from channels.layers import get_channel_layer
>>> channel_layer = get_channel_layer()
>>> print( type(channel_layer) )
<class 'NoneType'>
>>> from asgiref.sync import async_to_sync
>>> async_to_sync(channel_layer.group_send("pi2", {"type": "chat.system_message", "text": "hello"} ) )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group_send'
>>>
(pib) carl@x1:~/src/tv/pib/pici/ansible/roles/site/files/pib$ python3 pistat/send.py
Traceback (most recent call last):
File "/home/carl/src/tv/pib/pici/ansible/roles/site/files/pib/pistat/send.py", line 9, in <module>
channel_layer = get_channel_layer()
File "/home/carl/.virtualenvs/pib/lib/python3.10/site-packages/channels/layers.py", line 357, in get_channel_layer
return channel_layers[alias]
File "/home/carl/.virtualenvs/pib/lib/python3.10/site-packages/channels/layers.py", line 78, in __getitem__
self.backends[key] = self.make_backend(key)
File "/home/carl/.virtualenvs/pib/lib/python3.10/site-packages/channels/layers.py", line 43, in make_backend
config = self.configs[name].get("CONFIG", {})
File "/home/carl/.virtualenvs/pib/lib/python3.10/site-packages/channels/layers.py", line 37, in configs
return getattr(settings, "CHANNEL_LAYERS", {})
File "/home/carl/.virtualenvs/pib/lib/python3.10/site-packages/django/conf/__init__.py", line 89, in __getattr__
self._setup(name)
File "/home/carl/.virtualenvs/pib/lib/python3.10/site-packages/django/conf/__init__.py", line 76, in _setup
self._wrapped = Settings(settings_module)
File "/home/carl/.virtualenvs/pib/lib/python3.10/site-packages/django/conf/__init__.py", line 190, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 992, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'pib'
(pib) carl@x1:~/src/tv/pib/pici/ansible/roles/site/files/pib$