NEW: Fenrir v1.2.2 is now available — Logo & Favicon Patch! Read the changelog
websocket.md
docs websocket.md

WebSocket

Basic WebSocket Echo Server

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from fenrir import WebSocket, WebSocketDisconnect

@app.websocket("/ws/echo")
async def websocket_echo(websocket: WebSocket):
    await websocket.accept()
    try:
        while True:
            # Receive text
            data = await websocket.receive_text()
            # Send response
            await websocket.send_text(f"Echo: {data}")
    except WebSocketDisconnect:
        print("Client disconnected")

WebSocket Chat Application

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from typing import Set

# Store active connections
active_connections: Set[WebSocket] = set()

@app.websocket("/ws/chat/<room_id>")
async def websocket_chat(websocket: WebSocket, room_id: str):
    await websocket.accept()
    active_connections.add(websocket)

    try:
        while True:
            data = await websocket.receive_text()
            # Broadcast to all connections
            for connection in active_connections:
                try:
                    await connection.send_text(
                        f"[Room {room_id}] {data}"
                    )
                except:
                    pass
    except WebSocketDisconnect:
        active_connections.remove(websocket)

WebSocket with JSON Messages

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
@app.websocket("/ws/json")
async def websocket_json(websocket: WebSocket):
    await websocket.accept()

    try:
        while True:
            # Receive JSON
            data = await websocket.receive_json()

            # Process and respond
            result = {"received": data, "processed": True}

            # Send JSON response
            await websocket.send_json(result)
    except WebSocketDisconnect:
        print("WebSocket disconnected")
Edit on GitHub Last Updated: Jun 04, 2026
© 2026 Fenrir Project.
main*
v1.2.2
Ln 1, Col 1
UTF-8
Prettier
Light Mode
Markdown