Skip to main content

Session

A Session represents an encrypted connection to a paired peer. Sessions are created by calling connect() on a Node and provide methods for sending messages and inspecting connection state.

send(channel, data)

Send an encrypted message over the session.

Parameters:

ParameterTypeDescription
channelstringChannel name. Use "" for the default channel.
databytesMessage payload (binary or UTF-8 encoded text).
// Send to a named channel
session.send("chat", b"hello").await?;

// Send to the default channel
session.send("", b"default message").await?;

Signature: async fn send(&self, channel: &str, data: &[u8]) -> Result<(), CairnError>

close()

Close the session and release resources. The remote peer receives a disconnected state change event.

session.close().await;

Signature: async fn close(&self)

State Properties

Inspect the current session state and remote peer identity.

Current State

The session state is one of: connecting, connected, reconnecting, disconnected.

let state = session.state(); // SessionState enum
println!("State: {:?}", state);

Type: fn state(&self) -> SessionState

Remote Peer ID

Get the peer ID of the remote end of this session.

let peer = session.peer_id();
println!("Connected to: {}", peer);

Type: fn peer_id(&self) -> &PeerID