diff --git a/crates/torii/libp2p/src/server/mod.rs b/crates/torii/libp2p/src/server/mod.rs index 01d930465c..033803d747 100644 --- a/crates/torii/libp2p/src/server/mod.rs +++ b/crates/torii/libp2p/src/server/mod.rs @@ -69,6 +69,7 @@ impl Relay

{ port_websocket: u16, local_key_path: Option, cert_path: Option, + peers: Vec, ) -> Result { let local_key = if let Some(path) = local_key_path { let path = Path::new(&path); @@ -167,6 +168,12 @@ impl Relay

{ .with(Protocol::Ws("/".to_string().into())); swarm.listen_on(listen_addr_wss.clone())?; + // We dial all of our peers. Our server will then broadcast + // all incoming offchain messages to all of our peers. + for peer in peers { + swarm.dial(peer.parse::().unwrap())?; + } + // Clients will send their messages to the "message" topic // with a room name as the message data. // and we will forward those messages to a specific room - in this case the topic @@ -355,6 +362,24 @@ impl Relay

{ peer_id = %peer_id, "Message verified and set." ); + + // Publish message to all peers + if let Err(e) = self + .swarm + .behaviour_mut() + .gossipsub + .publish( + IdentTopic::new(constants::MESSAGING_TOPIC), + serde_json::to_string(&data).unwrap(), + ) + .map_err(Error::PublishError) + { + info!( + target: LOG_TARGET, + error = %e, + "Publishing message to peers." + ); + } } ServerEvent::Gossipsub(gossipsub::Event::Subscribed { peer_id, topic }) => { info!(