How to use Secure Websocket with the swarm

Im looking to enable secure websockets (wss) on my libp2p app. The example listed in the docs (WsConfig in libp2p::websocket - Rust) give an example of building a transport, but now how to integrate it with the swarm. Ive tried using the WsConfig with the with_websocket() function but I dont think I can use it there. Next I tried to use .with_other_transport() but could not figure out how to map the connections to what the function wants.

How can I add a wss transport to the swarm so I can listen on /ip/tcp/0/wss/%2Ftest (/ip/tcp/0/wss//test)

Hello @DeepBlue ,

Those are great questions. We just published a blog post that has some information about websockets and webtransports in rust-libp2p.

I don’t have the exact answer for you but if you jump on our Slack or Matrix chats and ping @DougAnderson444 he should be able to help you out.

Cheers! :beers:

1 Like

I was looking for the same thing and came up with this example

use anyhow::Result;
use libp2p::core::upgrade::Version;
use libp2p::futures::StreamExt;
use libp2p::swarm::NetworkBehaviour;
use libp2p::{dns, noise, ping, tcp, websocket, yamux, SwarmBuilder, Transport};
use tokio::fs;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};

struct Behaviour {
    ping: ping::Behaviour,

async fn main() -> Result<()> {

    let mut wss_transport = websocket::WsConfig::new(dns::tokio::Transport::system(

    let pk = fs::read("./private.der").await?;
    let cert = fs::read("./fullchain.der").await?;
    let pk = websocket::tls::PrivateKey::new(pk);
    let cert = websocket::tls::Certificate::new(cert);
    wss_transport.set_tls_config(websocket::tls::Config::new(pk, vec![cert])?);

    // create a new libp2p node with gossipsub
    let mut swarm = SwarmBuilder::with_new_identity()
        .with_other_transport(|local_key| {
        // .with_websocket(
        //     (tls::Config::new, noise::Config::new),
        //     yamux::Config::default,
        // )
        // .await?
        .with_behaviour(|_| {
            let ping = ping::Behaviour::new(ping::Config::default());

            Ok(Behaviour { ping })


    loop {
        let _ = swarm.select_next_some().await;