To communicate with a remote Erlang node, you must perform a handshake using ClientSideHandshake, establish a channel, and then send a message.
Steps:
- Connect to the peer via
TcpStream (port usually retrieved from EPMD). - Define local node information using
LocalNode and Creation. - Execute the handshake using
ClientSideHandshake with the node's name and the distribution cookie. - Use
LOWEST_DISTRIBUTION_PROTOCOL_VERSION for the initial handshake step. - Create a communication channel using
channel() with capability flags derived from the intersection of local and peer node flags. - Construct a message (e.g., using
Message::reg_send) and send it via the channel's transmitter (tx).
use erl_dist::LOWEST_DISTRIBUTION_PROTOCOL_VERSION;
use erl_dist::node::{Creation, LocalNode};
use erl_dist::handshake::ClientSideHandshake;
use erl_dist::term::{Atom, Pid};
use erl_dist::message::{channel, Message};
use tokio::net::TcpStream;
// Connect to a peer node.
let peer_host = "localhost";
let peer_port = 7483; // NOTE: Usually, port number is retrieved from EPMD.
let connection = TcpStream::connect((peer_host, peer_port)).await?;
// Local node information.
let creation = Creation::random();
let local_node = LocalNode::new("foo@localhost".parse()?, creation);
// Do handshake.
let mut handshake = ClientSideHandshake::new(connection, local_node.clone(), "cookie");
let _status = handshake.execute_send_name(LOWEST_DISTRIBUTION_PROTOCOL_VERSION).await?;
let (connection, peer_node) = handshake.execute_rest(true).await?;
// Create a channel.
let capability_flags = local_node.flags & peer_node.flags;
let (mut tx, _) = channel(connection, capability_flags);
// Send a message.
let from_pid = Pid::new(local_node.name.to_string(), 0, 0, local_node.creation.get());
let to_name = Atom::from("bar");
let msg = Message::reg_send(from_pid, to_name, Atom::from("hello").into());
tx.send(msg).await?;