M
M
mefisto6612019-12-14 11:08:41
JavaScript
mefisto661, 2019-12-14 11:08:41

How to connect channels 2 to Django?

You need to create a chat and notifications on the site. Decided to do it through channels, got stuck at the first step tutorial`a https://channels.readthedocs.io/en/latest/tutorial...
At the last step, when starting the server:
Watching for file changes with StatReloader
Exception in thread django-main -thread:
Traceback (most recent call last):
File "C:\Python38\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Python38\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Python38\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
fn(*args , **kwargs)
File "C:\Python38\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "C:\Python38\lib\site-packages\django \utils\autoreload.py", line 77, in raise_last_exception
raise _exception[1]
File "C:\Python38\lib\site-packages\django\core\management\__init__.py", line 337, in execute
autoreload.check_errors (django.setup)()
File "C:\Python38\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "C:\Python38\ lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Python38\lib\site-packages\django\apps\registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "C:\Python38\lib\site-packages\django\ apps\config.py", line 116, in create
mod = import_module(mod_path)
File "C:\Python38\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1014, in _gcd_import
File "", line 991, in _find_and_load
File "", line 975, in _find_and_load_unlocked
File "", line 671, in _load_unlocked
File "", line 783, in exec_module
File "", line 219, in _call_with_frames_removed
File "C:\Python38\lib\site-packages\channels\apps.py", line 6, in
import daphne.server
File "C:\Python38\lib\site-packages\daphne\server.py", line 18 , in
asyncioreactor.install()
File "C:\Python38\lib\site-packages\twisted\internet\asyncioreactor.py", line 320, in install
reactor = AsyncioSelectorReactor(eventloop)
File "C:\Python38\lib\site -packages\twisted\internet\asyncioreactor.py", line 69, in __init__
super().__init__()
File "C:\Python38\lib\site-packages\twisted\internet\base.py", line 571, in __init__
self.installWaker()
File "C:\Python38\lib\site-packages\twisted\internet\posixbase.py", line 286,in installWaker
self.addReader(self.waker)
File "C:\Python38\lib\site-packages\twisted\internet\asyncioreactor.py", line 151, in addReader
self._asyncioEventloop.add_reader(fd, callWithLogger, reader,
File "C:\Python38\lib\asyncio\ events.py", line 501, in add_reader
raise NotImplementedError
NotImplementedError

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
antonksa, 2019-12-15
@mefisto661

Better use aiohttp.
I have done this:
1. The user connects to Aiohttp to a specific URL.
2. Aiohttp in the process of creating a websocket pulls the system api in jang, getting the user id.
3. If the user is authorized, then Aiohttp adds the websocket to the internal connection registry. For me, this is a regular dictionary, where the key is the user id. Accordingly, when the socket is closed or it falls off from the registry, it is deleted. Thus, at any time you can get all the open sockets of the user.
4. Janga writes events to RabbitMQ, for example SEND_MESSAGE with JSON context (user id, message text, etc.)
5. Aiohttp reads messages from the rabbit and pushes them into the appropriate web sockets.
6. If, for example, you need confirmation of receipt, then Aiohttp, not finding an open socket for the user, can pull the system api in jang, telling her that the user is offline. You can also organize a system api in jang, where Aiohttp will go when establishing a socket and check for a list of undelivered messages.
7. Profit. There is generally a large field for experimentation, I can say one thing for sure, after a year of using Channels back in 2017, I got tired of chewing a cactus and began to use synchronous and asynchronous frameworks where possible, stopping crossing a hedgehog and a snake.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question