Repository: litestar-org/litestar

Issue:
Docs: Channels run_in_background.py example does not work
### Summary

Hi,

i just tried to run the channels example "run_in_background.py" from the [website](https://docs.litestar.dev/2/usage/channels.html) (which translates to [run_in_background.py](https://github.com/litestar-org/litestar/blob/main/docs/examples/channels/run_in_background.py)) and found that it didn't work for me. 

I'm running Python 3.8.10 in a virtual environment with Litestar 2.4.1.

The application contains exacly the same source code that is provided in the example file and was run using `uvicorn app:app --reload`

Log output: 

```
INFO:     Started reloader process [219108] using WatchFiles
Process SpawnProcess-1:
Traceback (most recent call last):
  File "/usr/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
    self.run()
  File "/usr/lib/python3.8/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/home/.../venv/lib/python3.8/site-packages/uvicorn/_subprocess.py", line 76, in subprocess_started
    target(sockets=sockets)
  File "/home/.../venv/lib/python3.8/site-packages/uvicorn/server.py", line 61, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "uvloop/loop.pyx", line 1517, in uvloop.loop.Loop.run_until_complete
  File "/home/.../venv/lib/python3.8/site-packages/uvicorn/server.py", line 68, in serve
    config.load()
  File "/home/.../venv/lib/python3.8/site-packages/uvicorn/config.py", line 467, in load
    self.loaded_app = import_from_string(self.app)
  File "/home/.../venv/lib/python3.8/site-packages/uvicorn/importer.py", line 21, in import_from_string
    module = importlib.import_module(module_str)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/.../source/app.py", line 18, in <module>
    plugins=[ChannelsPlugin(backend=MemoryChannelsBackend())],
  File "/home/.../venv/lib/python3.8/site-packages/litestar/channels/plugin.py", line 82, in __init__
    raise ImproperlyConfiguredException("Must define either channels or set arbitrary_channels_allowed=True")
litestar.exceptions.http_exceptions.ImproperlyConfiguredException: 500: Must define either channels or set arbitrary_channels_allowed=True
```

This error is easy to resolve. I've changed 

    plugins=[ChannelsPlugin(backend=MemoryChannelsBackend())],

 to

    plugins=[ChannelsPlugin(backend=MemoryChannelsBackend(), channels=["general"])],

and the app started up successfully. 

But there seems to be another problem. As soon as my browser connects to the websocket, I get the following warning:

```
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [219218] using WatchFiles
INFO:     Started server process [219220]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     ('127.0.0.1', 33682) - "WebSocket /ws" [accepted]
/home/...../source/app.py:10: RuntimeWarning: coroutine 'ChannelsPlugin.subscribe' was never awaited
  async with channels.subscribe(["some_channel"]) as subscriber, subscriber.run_in_background(socket.send_text):
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
INFO:     connection open
INFO:     connection closed
```

I'm not sure how to resolve this problem. Probably the documentation does not refect the current state of Litestar? Could you check if the example is still valid? Thank you :-) 



<!-- POLAR PLEDGE BADGE START -->
---
> [!NOTE]  
> While we are open for sponsoring on [GitHub Sponsors](https://github.com/sponsors/litestar-org/) and 
> [OpenCollective](https://opencollective.com/litestar), we also utilize [Polar.sh](https://polar.sh/) to engage in pledge-based sponsorship.
>
> Check out all issues funded or available for funding [on our Polar.sh Litestar dashboard](https://polar.sh/litestar-org)
> * If you would like to see an issue prioritized, make a pledge towards it!
> * We receive the pledge once the issue is completed & verified
> * This, along with engagement in the community, helps us know which features are a priority to our users.

<a href="https://polar.sh/litestar-org/litestar/issues/2813">
<picture>
  <source media="(prefers-color-scheme: dark)" srcset="https://polar.sh/api/github/litestar-org/litestar/issues/2813/pledge.svg?darkmode=1">
  <img alt="Fund with Polar" src="https://polar.sh/api/github/litestar-org/litestar/issues/2813/pledge.svg">
</picture>
</a>
<!-- POLAR PLEDGE BADGE END -->



Discussion:
> Probably the documentation does not refect the current state of Litestar? Could you check if the example is still valid?

Your assessment is correct here, the documentation is out of date. We'll have to update this!

Resolving patch:
diff --git a/docs/examples/channels/run_in_background.py b/docs/examples/channels/run_in_background.py
--- a/docs/examples/channels/run_in_background.py
+++ b/docs/examples/channels/run_in_background.py
@@ -7,13 +7,13 @@
 async def handler(socket: WebSocket, channels: ChannelsPlugin) -> None:
     await socket.accept()
 
-    async with channels.subscribe(["some_channel"]) as subscriber, subscriber.run_in_background(socket.send_text):
+    async with await channels.subscribe(["some_channel"]) as subscriber, subscriber.run_in_background(socket.send_text):
         while True:
-            await socket.receive_text()
-            # do something with the message here
+            response = await socket.receive_text()
+            await subscriber.send(response)
 
 
 app = Litestar(
     [handler],
-    plugins=[ChannelsPlugin(backend=MemoryChannelsBackend())],
+    plugins=[ChannelsPlugin(backend=MemoryChannelsBackend(), channels=["some_channel"])],
 )


Test patch: