ipfs / js-ipfs

IPFS implementation in JavaScript
https://js.ipfs.tech
Other
7.44k stars 1.25k forks source link

Use pubsub between browser node and daemon node (go-ipfs) #1960

Closed gil-air-may closed 5 years ago

gil-air-may commented 5 years ago

Type: Question

Description:

I'm having difficulty using pubsub between a browser node (using js-ipfs) and a go-ipfs node running on my machine.

First, I tried to setup a direct connection between the nodes, but I keep getting the same error:

image

Since this was not possible, I attempted to setup a p2p connection between Node A and B. I believe this was done correctly. The browser logs:

Swarm listening on /p2p-circuit/ip4/127.0.0.1/tcp/4001/ipfs/QmPMaDyK2ee95BpjnMyWYiVi46EcZFrY8AUmSzieNSLbEa/ipfs/QmcCnVBpYLvcRLbutwRMkzeJwHfjbFaq8y9JQ2sLcBAX1L

Despite having set up the p2p circuit, the node running on my machine does not receive the message published by the node running on the browser.

Steps to reproduce the error:

I start the daemon on the terminal using: ipfs daemon --enable-pubsub-experiment and subscribe to the topic using ipfs pubsub sub testing123

I instantiate the node on the browser using:

this.ipfsNode = new IPFS({
      EXPERIMENTAL: { pubsub: true },
      relay: { enabled: true, hop: { enabled: true } },
      config: {
        Addresses: {
          Swarm: [
            "/ip4/127.0.0.1/tcp/4001/ipfs/QmPMaDyK2ee95BpjnMyWYiVi46EcZFrY8AUmSzieNSLbEa",
            "/dns4/wrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star"
          ]
        },
        libp2p: {
          modules: {
            transport: [wstar],
            peerDiscovery: [wstar.discovery]
          }
        }
      }
    });

I navigate to localhost:3000 and the console logs:

Swarm listening on /p2p-circuit/ip4/127.0.0.1/tcp/4001/ipfs/QmPMaDyK2ee95BpjnMyWYiVi46EcZFrY8AUmSzieNSLbEa/ipfs/QmcCnVBpYLvcRLbutwRMkzeJwHfjbFaq8y9JQ2sLcBAX1L

Then on the browser, we try to communicate with the daemon using

publishFileHash() {

    const topic = "testing123";
    const msg = Buffer.from(this.state.added_file_hash);
    console.log("msg is");
    console.log(this.state.added_file_hash);

    this.ipfsNode.pubsub.publish(topic, msg, err => {
      if (err) {
        return console.error(`failed to publish to ${topic}`, err);
      }
      console.log(`published to ${topic}`);
    });
  }

After this, browser console logs: published to testing123 but the daemon does not get the message. :cry:

Question

I would like to know if it is possible to get the Pubsub functionality working between these two types of nodes. If so, I was hoping someone could point me in the right configuration for both nodes. The current configuration for both nodes can be found here:

https://github.com/open-science-org/idea-hub/issues/10

Thanks!

alanshaw commented 5 years ago

@gil-air-may go-ipfs doesn't have webrtc discovery or transport so you'll not be able to connect to it in this way.

It does have a websockets transport though so you should be able to connect and use pubsub with it. The "exchange files in browser" example should help you set this up:

https://github.com/ipfs/js-ipfs/tree/master/examples/exchange-files-in-browser