Function ic_websocket_cdk::send

source ·
pub fn send(
    client_principal: ClientPrincipal,
    msg_bytes: Vec<u8>
) -> CanisterSendResult
Expand description

Sends a message to the client. The message must already be serialized using Candid. Use candid::encode_one to serialize the message.

Under the hood, the message is certified and added to the queue of messages that the WS Gateway will poll in the next iteration.

§Example

This example is the serialize equivalent of the OnMessageCallbackArgs’s example deserialize one.

use candid::{encode_one, CandidType, Principal};
use ic_websocket_cdk::send;
use serde::Deserialize;

#[derive(CandidType, Deserialize)]
struct MyMessage {
    some_field: String,
}

// obtained when the on_open callback was fired
let my_client_principal = Principal::from_text("wnkwv-wdqb5-7wlzr-azfpw-5e5n5-dyxrf-uug7x-qxb55-mkmpa-5jqik-tqe").unwrap();

let my_message = MyMessage {
    some_field: "Hello, World!".to_string(),
};

let msg_bytes = encode_one(&my_message).unwrap();
send(my_client_principal, msg_bytes);