js-ipfs icon indicating copy to clipboard operation
js-ipfs copied to clipboard

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

Open satoshi999 opened this issue 3 years ago • 2 comments

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.

  • "ipfs-core": "0.15.4",
  • "@libp2p/websockets": "3.0.2",
  • Node.js : 14.18.0
import {WebSockets} from '@libp2p/websockets'
import { all } from '@libp2p/websockets/filters'
import {create} from 'ipfs-core'

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
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)
})()

satoshi999 avatar Sep 15 '22 00:09 satoshi999

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

threshold862543 avatar Sep 15 '22 20:09 threshold862543

@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

satoshi999 avatar Sep 16 '22 00:09 satoshi999

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

SgtPooki avatar May 16 '23 21:05 SgtPooki