Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

websocket_consumer-0.1.0.tar.gz (2.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

websocket_consumer-0.1.0-py3-none-any.whl (2.9 kB view details)

Uploaded Python 3

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

Hashes for websocket_consumer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f22f05f68820eea48801afcb9b9200e365cbd9af1cb61b7e2d74ae740c5a78f1
MD5 906fca1ba1551ba881faca73369a1e9e
BLAKE2b-256 287be9c85c94f5b56dd4bb00c96707c63ff27baf81474d6a3a910e38d5bdcffa

See more details on using hashes here.

File details

Details for the file websocket_consumer-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for websocket_consumer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c3aca1e2840461dfaba582bb968045f32438a5dd90ee4cfe3717728c0148ce28
MD5 0e5ad7c7c7f68e70203ef9a99726f499
BLAKE2b-256 d75f57efbddafaa0fa6af6c902eecbe030ec88d8b4b8827c9f04ff8ac2a4db24

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page