websocket
DaraClientMessage
from dara.core.internal.websocket import DaraClientMessage
class DaraClientMessage(BaseModel)
Represents a message sent by Dara internals from the frontend to the backend.
An optional chunk_count field can be used to indicate that a message is chunked and what number of messages to expect
Attributes
- type:
- channel: str
- chunk_count: Optional[int]
- message: Any
CustomClientMessagePayload
from dara.core.internal.websocket import CustomClientMessagePayload
class CustomClientMessagePayload(BaseModel)
Attributes
- rchan: Optional[str]
- kind: str
- data: Any
rchan
Return channel if the message is expected to have a response for
CustomClientMessage
from dara.core.internal.websocket import CustomClientMessage
class CustomClientMessage(BaseModel)
Represents a custom message sent by the frontend to the backend.
Attributes
- type:
- message: CustomClientMessagePayload
ServerMessagePayload
from dara.core.internal.websocket import ServerMessagePayload
class ServerMessagePayload(BaseModel)
Attributes
- rchan: Optional[str]
- response_for: Optional[str]
rchan
Return channel if the message is expected to have a response for
response_for
ID of the __rchan included in the original client message if this message is a response to a client message
DaraServerMessage
from dara.core.internal.websocket import DaraServerMessage
class DaraServerMessage(BaseModel)
Represents a message sent by Dara internals from the backend to the frontend.
Attributes
- type:
- message: ServerMessagePayload
CustomServerMessage
from dara.core.internal.websocket import CustomServerMessage
class CustomServerMessage(BaseModel)
Represents a custom message sent by the backend to the frontend.
Attributes
- type:
- message: CustomServerMessagePayload
WebSocketHandler
from dara.core.internal.websocket import WebSocketHandler
class WebSocketHandler()
Represents a WebSocket connection to a given client.
Attributes
- channel_id: str
- send_stream: MemoryObjectSendStream[ServerMessage]
- receive_stream: MemoryObjectReceiveStream[ServerMessage]
- pending_responses: Dict[str, Tuple[Event, Optional[Any]]]
Methods
channel_id
ID of the channel this handler is associated with.
send_stream
Stream for the application to send messages to the client.
receive_stream
Stream containing messages to send to the client.
pending_responses
A map of pending responses from the client. The key is the message ID and the value is a tuple of the event to notify when the response is received and the response data.
send_message
async def send_message(message: ServerMessage)
Send a message to the client.
Arguments:
message
: The message to send
process_client_message
def process_client_message(message: ClientMessage)
Process a message received from the client.
Handles resolving pending responses.
Can return a coroutine to be awaited by the caller.
Arguments:
message
: The message to process
send_and_wait
async def send_and_wait(message: ServerMessage) -> Optional[Any]
Send a message to the client and return the client's response
Arguments:
message
: The message to send
get_user_channels
def get_user_channels(user_identifier: str) -> Set[str]
Get connected websocket channels associated with a given user.
Arguments:
user_identifier
: The user identifier to get channels for
WebsocketManager
from dara.core.internal.websocket import WebsocketManager
class WebsocketManager()
Manages WebSocket connections to clients and communication with them.
Methods
create_handler
def create_handler(channel_id: str) -> WebSocketHandler
Create and register a new WebSocketHandler for the given channel_id.
Arguments:
channel_id
: The channel ID to create a handler for
broadcast
async def broadcast(message: LoosePayload,
custom=False,
ignore_channel: Optional[str] = None)
Send a message to all connected clients.
Arguments:
message
: The message to sendcustom
: Whether the message is a custom messageignore_channel
: A channel ID to ignore when broadcasting
send_message_to_user
async def send_message_to_user(user_id: str,
message: LoosePayload,
custom=False,
ignore_channel: Optional[str] = None)
Send a message to all connected channels associated with the given user.
Arguments:
user_id
: The user ID to send the message tomessage
: The message payload to sendcustom
: Whether the message is a custom messageignore_channel
: A channel ID to ignore when sending
send_message
async def send_message(channel_id: str, message: LoosePayload, custom=False)
Send a message to the client associated with the given channel_id.
Arguments:
channel_id
: The channel ID to send the message tomessage
: The message payload to sendcustom
: Whether the message is a custom message
send_and_wait
async def send_and_wait(channel_id: str, message: LoosePayload, custom=False)
Send a message to the client associated with the given channel_id and wait for a response.
Arguments:
channel_id
: The channel ID to send the message tomessage
: The message to sendcustom
: Whether the message is a custom message
remove_handler
def remove_handler(channel_id: str)
Remove the WebSocketHandler associated with the given channel_id.
Arguments:
channel_id
: The channel ID to remove the handler for
ws_handler
async def ws_handler(websocket: WebSocket,
token: Optional[str] = Query(default=None))
Websocket handler. Used for live_reloading in dev mode and for notifying the UI of task results.
Arguments:
websocket
: The websocket connectiontoken
: The authentication token