Skip to content

Websocket send JSON encoded bytes #1576

Closed Answered by Kludex
kontsaki asked this question in Potential Issue
Discussion options

You must be logged in to vote

You can use the following send_json function:

from typing import Any

import orjson

from starlette.websockets import WebSocket


async def send_json(websocket: WebSocket, data: dict[str, Any]):
    text = orjson.dumps(data)
    await websocket.send({"type": "websocket.send", "text": text})

Replies: 1 comment

Comment options

Kludex
Jan 14, 2024
Maintainer Sponsor

You must be logged in to vote
0 replies
Answer selected by Kludex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants