ipfs / js-ipfs

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

How to send pubsub message from Browser node to terminal node? #4208

Closed satoshi999 closed 1 year ago

satoshi999 commented 2 years ago

I tried to sending pubsub message from Browser node to terminal node via websockets with following code. It seems that has succeeded connection via websockets because opponent node id is in peer list But, it can not receive pubsub message.

Is it possible that sending pubsub message between browser and terminal node?

On browser side.

window.addEventListener('load', async() => { const ipfs = await create({ repo:'IPFS-' + Math.random(), libp2p: { transports: [new WebSockets({filter:all})], config: { pubsub: { emitSelf:false } } } })

await ipfs.swarm.connect('/ip4/127.0.0.1/tcp/4003/ws/p2p/QmVpoLX9Us7AF39vWsFFHSz274ShJfHaVY5VWoVsuAzpdb')

await ipfs.pubsub.subscribe('message', (msg)=> { const data = new TextDecoder().decode(msg.data) console.log(data) })

const id = await (await ipfs.id()).id

setInterval(async() => { await ipfs.pubsub.publish('message', new TextEncoder().encode(Hello from ${id}))

const peers = await ipfs.swarm.peers()
for(const peer of peers) {
  console.log(peer.peer.toString())
}

}, 1000) })


On terminal side
- "ipfs": "0.54.0"
- Node.js 14.18.0
```JavaScript
const { create } = require('ipfs');

(async() => {
  const ipfs = await create({
    repo: 'IPFS',
    EXPERIMENTAL: {
      pubsub: true
    },
    libp2p: {
      config: {
        pubsub: {
          emitSelf:false
        }
      }
    }
  })
  const id = await (await ipfs.id()).id

  await ipfs.pubsub.subscribe('message', async(msg)=> {
    const data = new TextDecoder().decode(msg.data)
    console.log(data)
  })

  setInterval(async() => {
    await ipfs.pubsub.publish('message', new TextEncoder().encode(`Hello from ${id}`))

    const peers = await ipfs.swarm.peers()
    for(const peer of peers) {
      console.log(peer.peer.toString())
    }
  }, 1000)
})()
threshold862543 commented 2 years ago

I'm having a similar issue ipfs/js-kubo-rpc-client#70

satoshi999 commented 2 years ago

@threshold862543 It issue seems that my case is different from this issue

This issue as linked describes the behavior when accessing by http from a browser using ipfs-http-client to a local node. The pubsub keyword is used, but it seems irrelevant since it is ipfs-http-client

I understand that in my case it is an issue related to the pubsub communication between IPFS node in the browser and the daemon node, In other words, it seems to be a websockets communication issue between the browser node and the daemon node

SgtPooki commented 1 year ago

Hello @satoshi999,

js-ipfs is being deprecated in favor of Helia. You can learn more about this deprecation and the corresponding migration guide here.

As a result, we are going to close this issue. If you think we have done this in error, please feel to reopen with any comments in the next week as we will circle back on the reopened issues.

We hope you will consider Helia for your IPFS in JS needs. If you believe this particular request belongs in Helia, feel free to open a Helia issue. We look forward to engaging with you more there.

Thanks, @ipfs/helia-dev