/ justapi / examples / 10_websockets.py
swadhinbiswas docs(examples): add 10 progressive tutorial examples and test project · 7/9/2026
b48e461
21 lines · 455 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import asyncio
from justapi import JustAPIApp

app = JustAPIApp()

@app.websocket("/ws")
async def websocket_endpoint(ws):
    """
    A simple WebSocket echo server.
    """
    await ws.accept()
    try:
        while True:
            data = await ws.receive_text()
            await ws.send_text(f"Message text was: {data}")
    except Exception as e:
        print("Client disconnected:", e)

if __name__ == "__main__":
    app.run("127.0.0.1:8000")