Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more developer-friendly WebSocket Interface #57

Open
VincentRPS opened this issue Dec 20, 2022 · 2 comments
Open

Add more developer-friendly WebSocket Interface #57

VincentRPS opened this issue Dec 20, 2022 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@VincentRPS
Copy link

Is your feature request related to a problem? Please describe.

Currently, Socketify uses a method of using the open, message, and close indicators to show parse the obvious. This makes it much harder to track and sustain a connection easily.

Describe the solution you'd like

I would like an implementation to something like what the WebSockets library has, and what most other libraries do:

async def websocket_handler(ws: socketify.WebSocket) -> None:
    await user_online()

    # stops looping on close
    async for msg in ws:
       await ws.send('hey!')

    await user_offline()

app.ws('/websocket', {
    'compression': CompressOptions.SHARED_COMPRESSOR,
    'max_payload_length': 16 * 1024 * 1024,
    'idle_timeout': 12,
    'handler': websocket_handler,
    'drain': lambda ws: print('WebSocket backpressure: %i' % ws.get_buffered_amount()),
})

This would be optional, and not required to use websockets. It'd also make it much easier to keep state if, per se, you're trying to make a chat app or another similar thing which requires state and easy DX.

else then all of that though, I'd just like to say that this project is really nice, I've always been looking for a fast Python websocket lib and I think this one is a first, I can't wait to see this library be production-ready!

@cirospaciari
Copy link
Owner

The current version basically is a 1-to-1 representation of uWS C++ WS, I will not change this, but I can add some helpers, adding an async/ASGI-like wrapper will add a ton of overhead, and socketify.py focus is performance. But I will consider adding an official helper.

@cirospaciari cirospaciari added the enhancement New feature or request label Dec 20, 2022
@cirospaciari cirospaciari self-assigned this Dec 20, 2022
@cirospaciari
Copy link
Owner

cirospaciari commented Jan 3, 2023

I will add better DX in WebSockets after this features:

  1. Better routers Add decorator router #59
  2. Hot reloading Add hot reloading on CLI Tools #61
  3. Plugins / Extensions Add request, response and WS extensions #62
  4. on_start, on_shutdown hooks Add on_start and on_shutdown lifespan hooks #58
  5. Cache tools LRU Cache and Timed Cache avoiding GIL a in memory cache solution #60
  6. Native optimizations Add native optimizations #52

Will be better, in general, using decorators or something similar to handle the websocket events and routes.

from socketify import App

app = App()
ws = app.ws("/websocket",  {
    'compression': CompressOptions.SHARED_COMPRESSOR,
    'max_payload_length': 16 * 1024 * 1024,
    'idle_timeout': 12,
})
@ws.open
def ws_open(ws):
    print("A WebSocket got connected!")
    ws.send("Hello World!", OpCode.TEXT)

@ws.message
def ws_message(ws, message, opcode):
    print(message, opcode)
    # Ok is false if backpressure was built up, wait for drain
    ok = ws.send(message, opcode)
    
@ws.close
def ws_message( ws, code, message):
     print("WebSocket closed")

I will also add a decorator for something similar to what you proposed too, something similar to ASGI form of handling connections, will be overhead and I will measure it and add it to the docs and the user can decide if is worth it or not.

Edit: almost there!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants