Skip to content

Docs: Incorrect example for websocket listener connection_lifespan  #4100

@v3ss0n

Description

@v3ss0n

Description

I am trying the example , and it is not working. I had drill down to included MVCE and i believe connection_lifespan is the culprit.

URL to code causing the issue

https://github.com/litestar-org/litestar/blob/027ce12a64711f90c9d13b79d753be81779e6db0/docs/examples/websockets/stream_and_receive_listener.py

MCVE

import asyncio
import time
from typing import Any, AsyncGenerator

from litestar import Litestar, WebSocket, websocket_listener
from litestar.handlers import send_websocket_stream


async def listener_lifespan(socket: WebSocket) -> None:
    pass


@websocket_listener("/", connection_lifespan=listener_lifespan)
def handler(socket: WebSocket, data: Any) -> None:
    print(f"{socket.client}: {data}")


app = Litestar([handler])

Steps to reproduce

  1. litestar run --reload -p 8881 --host=0.0.0.0
  2. websocat ws://localhost:8881

Screenshots

No response

Logs

INFO:     ('127.0.0.1', 38680) - "WebSocket /" 403
INFO:     connection rejected (403 Forbidden)
INFO:     connection closed



websocat: WebSocketError: Connection refused (os error 111)
websocat: error running
(litestar-chat)

Litestar Version

027ce12

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Sub-issues

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions