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:
| Parameter | Type | Description |
|---|---|---|
channel | string | Channel name. Use "" for the default channel. |
data | bytes | Message payload (binary or UTF-8 encoded text). |
- Rust
- TypeScript
- Go
- Python
- PHP
// 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>
// Send to a named channel
await session.send('chat', Buffer.from('hello'));
// Send to the default channel
await session.send('', Buffer.from('default message'));
Signature: send(channel: string, data: Buffer): Promise<void>
// Send to a named channel
session.Send("chat", []byte("hello"))
// Send to the default channel
session.Send("", []byte("default message"))
Signature: func (s *Session) Send(channel string, data []byte) error
# Send to a named channel
await session.send("chat", b"hello")
# Send to the default channel
await session.send("", b"default message")
Signature: async def send(self, channel: str, data: bytes) -> None
// Send to a named channel
$session->send('chat', 'hello');
// Send to the default channel
$session->send('', 'default message');
Signature: public function send(string $channel, string $data): void
close()
Close the session and release resources. The remote peer receives a disconnected state change event.
- Rust
- TypeScript
- Go
- Python
- PHP
session.close().await;
Signature: async fn close(&self)
session.close();
Signature: close(): void
session.Close()
Signature: func (s *Session) Close() error
await session.close()
Signature: async def close(self) -> None
$session->close();
Signature: public function close(): void
State Properties
Inspect the current session state and remote peer identity.
Current State
The session state is one of: connecting, connected, reconnecting, disconnected.
- Rust
- TypeScript
- Go
- Python
- PHP
let state = session.state(); // SessionState enum
println!("State: {:?}", state);
Type: fn state(&self) -> SessionState
const state = session.state; // "connected" | "reconnecting" | ...
console.log(`State: ${state}`);
Type: state: string (readonly property)
state := session.State() // "connected", "reconnecting", ...
fmt.Println("State:", state)
Type: func (s *Session) State() string
state = session.state # "connected", "reconnecting", ...
print(f"State: {state}")
Type: state: str (readonly property)
$state = $session->state(); // "connected", "reconnecting", ...
echo "State: $state\n";
Type: public function state(): string
Remote Peer ID
Get the peer ID of the remote end of this session.
- Rust
- TypeScript
- Go
- Python
- PHP
let peer = session.peer_id();
println!("Connected to: {}", peer);
Type: fn peer_id(&self) -> &PeerID
const peer = session.peerId;
console.log(`Connected to: ${peer}`);
Type: peerId: string (readonly property)
peer := session.PeerID()
fmt.Println("Connected to:", peer)
Type: func (s *Session) PeerID() string
peer = session.peer_id
print(f"Connected to: {peer}")
Type: peer_id: str (readonly property)
$peer = $session->peerId();
echo "Connected to: $peer\n";
Type: public function peerId(): string