Websocket Consumer
Run an embedded websocket server from the root of your project folder.
pip install websocket-consumer
uv add websocket-consumer
websocket-consumer app.websockets.server
app = Main package
websockets = Sub package
server = Instance that passes the WebsocketServer(Protocol)
Info
Say you have the following project structure below and are running your wsgi and websocket server side by side.
app/
├── wsgi/...
└── websockets/
├── __init__.py
├── __main__.py
└── server.py
In the __main__.py file you have the code needed to start the websockets
server.
If you try and import anything above the websockets package an exception
will be raised. This is because when you run python3 app/websockets the
project root folder is not included in sys.path.
This project fixes that by allowing you to define an instance of a WebSocket class.
How it works
Create your websocket server as class:
import typing as t
from websockets.asyncio.server import serve
class WebsocketServer:
host: str = "120.0.0.1"
port: int = 5003
connections: t.Set = set()
def __init__(self, host: str = "120.0.0.1", port: int = 5003):
self.host = host
self.port = port
async def handler(self, websocket):
...
async def run(self):
async with serve(self.handler, self.host, self.port) as server:
await server.serve_forever()
You must have an async run() method on the class as this is what will be
called at run.
Now create an instance of your websocket server, here's an example:
app/websockets/__init__.py
from app.websockets.server import WebsocketServer
server = WebsocketServer(
host='127.0.0.1',
port=5003
)
Now call the consumer:
websocket_consumer app.websocket.server
Metadata
Release files for websocket-consumer 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| websocket_consumer-0.1.0.tar.gz | 2.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| websocket_consumer-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 5.2 kB
Release files / websocket_consumer-0.1.0.tar.gz
| Download URL | websocket_consumer-0.1.0.tar.gz |
|---|---|
| Size | 2.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f22f05f68820eea48801afcb9b9200e365cbd9af1cb61b7e2d74ae740c5a78f1
|
|
BLAKE2b-256 checksum How to use checksums |
287be9c85c94f5b56dd4bb00c96707c63ff27baf81474d6a3a910e38d5bdcffa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.5.21
|
Release files / websocket_consumer-0.1.0-py3-none-any.whl
| Download URL | websocket_consumer-0.1.0-py3-none-any.whl |
|---|---|
| Size | 2.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c3aca1e2840461dfaba582bb968045f32438a5dd90ee4cfe3717728c0148ce28
|
|
BLAKE2b-256 checksum How to use checksums |
d75f57efbddafaa0fa6af6c902eecbe030ec88d8b4b8827c9f04ff8ac2a4db24
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.5.21
|