Start an embedded websocket server from the root of your project.
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file websocket_consumer-0.1.0.tar.gz.
File metadata
- Download URL: websocket_consumer-0.1.0.tar.gz
- Upload date:
- Size: 2.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f22f05f68820eea48801afcb9b9200e365cbd9af1cb61b7e2d74ae740c5a78f1
|
|
| MD5 |
906fca1ba1551ba881faca73369a1e9e
|
|
| BLAKE2b-256 |
287be9c85c94f5b56dd4bb00c96707c63ff27baf81474d6a3a910e38d5bdcffa
|
File details
Details for the file websocket_consumer-0.1.0-py3-none-any.whl.
File metadata
- Download URL: websocket_consumer-0.1.0-py3-none-any.whl
- Upload date:
- Size: 2.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c3aca1e2840461dfaba582bb968045f32438a5dd90ee4cfe3717728c0148ce28
|
|
| MD5 |
0e5ad7c7c7f68e70203ef9a99726f499
|
|
| BLAKE2b-256 |
d75f57efbddafaa0fa6af6c902eecbe030ec88d8b4b8827c9f04ff8ac2a4db24
|