How to set up Gossipsub to use delegated router to discover peers?

If I set up my own delegated routing client, gossip sub doesn’t use it to query peers of the pubsub topic. Here is my config

export const pubsubTopicToDhtKey = async (pubsubTopic) => {
  // pubsub topic dht key used by kubo is a cid of "floodsub:topic" https://github.com/libp2p/go-libp2p-pubsub/blob/3aa9d671aec0f777a7f668ca2b2ceb37218fb6bb/discovery.go#L328
  const string = `floodsub:${pubsubTopic}`;

  // convert string to same cid as kubo https://github.com/libp2p/go-libp2p/blob/024293c77e17794b0dd9dacec3032b4c5a535f64/p2p/discovery/routing/routing.go#L70
  const bytes = new TextEncoder().encode(string);
  const hash = await sha256.digest(bytes);
  const cidVersion = 1;
  const multicodec = 0x55;
  const cid = CID.create(cidVersion, multicodec, hash);
  return cid.toString(base32);
};

const pubsubTopic = "random-topic";
const topicPeersCidInRouter = await pubsubTopicToDhtKey(pubsubTopic);

const delegatedRoutingClient = createDelegatedRoutingV1HttpApiClient(
  "https://myownrouter.com"
);

// https://myownrouter.com/routing/v1/providers/<topicPeersCidInRouter> exists and is 200

const helia = await createHelia({
  libp2p: {
    peerId: await getPeerId(),
    addresses: {
      listen: [], // Empty for browser environment
    },
    services: {
      identify: identify(),
      pubsub: gossipsub(),
      delegatedRouting0: () => delegatedRoutingClient,
    },
    peerDiscovery: undefined,
  },
  blockstore: new MemoryBlockstore(),
  blockBrokers: [bitswap()],
});


helia.libp2p.services.pubsub.subscribe(pubsubTopic, () => {}); // this never connects to the peers from https://myownrouter.com/routing/v1/providers/<topicPeersCidInRouter>

// libp2p is not querying the router for gossip sub peers
// no dial or connection attempts are being made

1 Like

I remember doing this once for IPNS (here). What I did:

import { ipns, type IPNS } from '@helia/ipns'
import { delegatedHTTPRouting } from '@helia/routers'

const helianame: IPNS = ipns(_helia, {
    routers: [
       pubsub(_helia),
       ...DELETAGED_ROUTERS.map(delegatedHTTPRouting)
   ]
})

I do not remember however if it was working :sweat_smile: