Ockam logo
Product
Open Source SDKsOckam RegistryOckam HubKafka Add-onInfluxData Add-onAzure HSM Add-onMicrochip Add-ons
Contact

Have questions? Let us help!

We are here to help. See the Guides And Demos in GitHub Discussions.

Forwarding

This example shows how you can route messages through remote nodes. With message forwarding, only the forwarding address is needed to send messages to a worker. A forwarding address is an alias to a route. By using a forwarding address, messages don't need to contain the entire route to a worker.

Using a RemoteForwarder

The RemoteForwarder API provides a convenient way to create a forwarding address on Ockam Hub.

We use the remote_address of the mailbox as the destination for messages.

1let mailbox = RemoteForwarder::create(&mut ctx, hub, "echo_service").await?;
2println!(
3 "echo_service forwarding address: {}",
4 mailbox.remote_address()
5);

Send a message to the Forwarded Echo Service

The echo_client that you built in the previous example can be used to send messages to the forwarding address of the echo_service in the hub. The remote node address should be set to your hub address. After the hub entry in the route, copy and paste the echo_service forwarding address.

Putting it all Together - Forwarded Echo Service

1use ockam::{async_worker, Context, RemoteMailbox, Result, Routed, TcpTransport, Worker};
2
3struct EchoService;
4
5#[async_worker]
6impl Worker for EchoService {
7 type Message = String;
8 type Context = Context;
9
10 async fn handle_message(&mut self, ctx: &mut Context, msg: Routed<String>) -> Result<()> {
11 println!("echo_service: {}", msg);
12 ctx.send(msg.return_route(), msg.body()).await
13 }
14}
15
16#[ockam::node]
17async fn main(mut ctx: Context) -> Result<()> {
18 let hub = "Paste the address of the node you created on Ockam Hub here.";
19
20 let tcp = TcpTransport::create(&ctx).await?;
21
22 tcp.connect(hub).await?;
23
24 ctx.start_worker("echo_service", EchoService).await?;
25
26 let mailbox = RemoteForwarder::create(&mut ctx, hub, "echo_service").await?;
27
28 println!(
29 "echo_service forwarding address: {}",
30 mailbox.remote_address()
31 );
32
33 Ok(())
34}
35

Run the example:

1cargo run --example echo_service

Putting it all together - Echo Client

1use ockam::{Context, Result, Route, TcpTransport, TCP};
2
3#[ockam::node]
4async fn main(mut ctx: Context) -> Result<()> {
5 let hub = "Paste the address of the node you created on Ockam Hub here.";
6 let echo_service =
7 "Paste the forwarded address that the server received from registration here.";
8
9 let tcp = TcpTransport::create(&ctx).await?;
10
11 tcp.connect(hub).await?;
12
13 ctx.send(
14 Route::new().append_t(TCP, hub).append(echo_service),
15 "Hello Ockam!".to_string(),
16 )
17 .await?;
18
19 // Then wait for a message back!
20 let msg = ctx.receive::<String>().await?;
21 println!("Received echo: '{}'", msg);
22 ctx.stop().await
23}
24

Make sure the echo_service is running, successfully registered, and that you have copied the forwarding address and pasted it into the echo_client source code.

Run the example:

1cargo run --example echo_client
Previous

Hub

Next

Using Add-Ons

On this page
Edit this page
Star Ockam repo