How to find peers by the services they provide?

Hi,

I want to make a service like this:

node.handle(‘/bestdbserver/1.0.0/’, ({ stream }) => {
console.log(‘Received a new /bestdbserver/1.0.0/ stream!’)
// Handle the stream here…
});

How other peers can find peers that support this protocol ?

Thanks.

Ok, so this is what I got until now:

await node._dht.put(
    Buffer.from(`/beastdbserver`, 'utf8'), 
    Buffer.from(node.peerId.toString(), 'utf8')
 );

But from what I understand _dht its an internal variable, is there a better way to do this ? or a module to discover content, tags or services ?

And I get the peers like this, but I get a lot of “garbage” is there a better way or the correct way to get peers ?

            const providers = await this.node._dht.get(Buffer.from(`/beastdbserver`, 'utf8'));

            for await (let provider of providers) {
                if (provider.peer) {
                    peers.push(provider.peer);
                }
            }

Then in dialProtocol I don’t understand very well how stream works:

node.handle('/beastdbserver/1.0.0/', async ({stream}) => {
                console.log('Received a new /beastdbserver/1.0.0/ stream!')
               pipe(
                    stream,
                    source => (async () => {
                      try {
                          for await (const msg of source) {
                              const data = JSON.parse(msg.toString());
                              console.log("DATA REC: ", data);
                          }
                      }
                      catch (e) {
                        console.log(e);
                      }
                    })()
                  );
                });
            })

So from what I have read, the sink methods receives one iterable and when everything was sent it closes, does this means that every-time I want to send something I have to dialProtocol ? Is this a good practice to always dialProtocol and send stuff to stream, what about if I want to send stuff back and forward ? Is there any good documentation how to handle streams on dialProtocol ?

Thanks