Search code examples
node.jspublish-subscribeipfs

Cannot subscribe to more than 6 ipfs pubsub channels with ipfs-http-client


I'm currently building a node application with ipfs-http-client.

I need to subscribe to several pubsub channels (~20).

When I'm connecting to the channels, I receive a 200 response for each subscribe, but only the 6 first subscribe are receiving the messages.

I isolated the problem in a little snippet with only:

  • Connect to the node (ipfs 0.4.23 and I tried with another one in 0.8)
  • Subscribe to 20 channels (with different names or the same channel with different handlers)
  • I always reproduce the problem (only connected to the 6 first subscribers)

I'm running my tests with node 14.16.0

When I look into the ipfs-http-client package, I can see that I actually have no response from the http request after the 6 first. Still, no error is reported.


Solution

  • achingbrain answers this question: https://github.com/ipfs/js-ipfs/issues/3741#issuecomment-898344489

    In node the default agent used by the http client limits connections to 6 so it's consistent with the browser. You can configure your own agent to change this:

    const { create } = require('ipfs-http-client');
    const http = require('http')
    
    async function echo(msg) {
        console.log(`TopicID: ${msg.topicIDs[0]}, Msg: ${new TextDecoder().decode(msg.data)}`);
    }
    
    async function run() {
        // connect to the default API address http://localhost:5001
        const client = create({
            agent: http.Agent({ keepAlive: true, maxSockets: Infinity })
        });
        console.log(await client.version());
    
        for (let i = 0; i < 20; i++) {
            await client.pubsub.subscribe(parseInt(i), echo);
        }
    }
    
    run();