Connect libp2p with bootstrap and kad-dht

I am trying to get a libp2p client to connect to other peers, however the code I have used, which uses bootstrap and kad-dht, doesn’t connect to any peers other than the bootstrap node.

I have tried the following code:

import { noise } from "@chainsafe/libp2p-noise";
import { mplex } from "@libp2p/mplex";
import { tcp } from "@libp2p/tcp";
import { webRTC } from "@libp2p/webrtc";
import { webSockets } from "@libp2p/websockets";
import { webTransport } from "@libp2p/webtransport";
import { Libp2p, createLibp2p } from "libp2p";
import { kadDHT } from "@libp2p/kad-dht";
import { bootstrap } from "@libp2p/bootstrap";
import { mdns } from '@libp2p/mdns';

const node: Libp2p = await createLibp2p({
    transports: [tcp(), webSockets(), webTransport(), webRTC()],
    connectionEncryption: [noise()],
    streamMuxers: [mplex()],
    services: {
        dht: kadDHT(),
    },

    peerDiscovery: [bootstrap({
        list: [
            "/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
            "/dnsaddr/bootstrap.libp2p.io/ipfs/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
            "/dnsaddr/bootstrap.libp2p.io/ipfs/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
        ],
        timeout: 1000, // in ms,
        tagName: 'bootstrap',
        tagValue: 50,
        tagTTL: 120000 // in ms
    }),
    mdns()],

    addresses: {
        listen: ['/ip4/0.0.0.0/tcp/42542']
    },
});
await node.start()

const peerId = node.peerId;
console.log(peerId);

node.addEventListener('peer:discovery', (evt) => {
    console.log('Discovered %s', evt.detail.id.toString()) // Log discovered peer
});

However, this code only connects to the bootstrap nodes. The only information available are from older versions of libp2p, whereas I am using the latest version, v0.45.4

First of all, welcome to the libp2p community!! I don’t know the exact answer to your question but maybe @achingbrain or @pshahi might know?

Cheers! :beers:

Please also enable the identify service:

import { fetchService } from 'libp2p/fetch'

const node: Libp2p = await createLibp2p({
    // other stuff
    services: {
        dht: kadDHT(),
        identify: identifyService()
    },
// other stuff

Thanks! That worked!

Hey, how that worked, it didnt working for me??

can you help me