【问题标题】:Pubsub between two nodes in IPFSIPFS 中两个节点之间的 Pubsub
【发布时间】:2019-08-29 17:59:50
【问题描述】:

我正在尝试在两个 IPFS 节点之间发送消息。 我正在运行的守护进程基于 go-ipfs,并使用以下标志运行:

ipfs daemon --enable-pubsub-experiment

我编写了两个 .js 文件,一个是给订阅者的:

const IPFS = require('ipfs')
const topic = 'topic';
const Buffer = require('buffer').Buffer;
const msg_buffer = Buffer.from('message');

const ipfs = new IPFS({
  repo: repo(),
  EXPERIMENTAL: {
    pubsub: true
  },
  config: {
    Addresses: {
      Swarm: [
        '/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star'
      ]
    }
  }
})

ipfs.once('ready', () => ipfs.id((err, info) => {
  if (err) { throw err }
  console.log('IPFS node ready with address ' + info.id)
  subscribeToTopic()
}))

function repo () {
  return 'ipfs-' + Math.random()
}

const receiveMsg = (msg) => {
  console.log(msg.data.toString())
}

const subscribeToTopic = () => {
  ipfs.pubsub.subscribe(topic, receiveMsg, (err) => {
    if (err) {
      return console.error(`failed to subscribe to ${topic}`, err)
    }
    console.log(`subscribed to ${topic}`)
  })
}

还有一个是给出版商的:

const IPFS = require('ipfs');
const topic = 'topic';
const Buffer = require('buffer').Buffer;
const msg_buffer = Buffer.from('message');

const ipfs = new IPFS({
  repo: repo(),
  EXPERIMENTAL: {
    pubsub: true
  },
  config: {
    Addresses: {
      Swarm: [
        '/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star'
      ]
    }
  }
})

ipfs.once('ready', () => ipfs.id((err, info) => {
  if (err) { throw err }
  console.log('IPFS node ready with address ' + info.id)
  publishToTopic()
}))

function repo () {
  return 'ipfs-' + Math.random()
}

const publishToTopic = () => {
  ipfs.pubsub.publish(topic, msg_buffer, (err) => {
    if (err) {
      return console.error(`failed to publish to ${topic}`, err)
    }
    // msg was broadcasted
    console.log(`published to ${topic}`)
    console.log(msg_buffer.toString())
  })
}

我已经运行了 .js 脚本:

node file.js

但是订阅者没有收到订阅者的任何消息,我不知道为什么。

在这种情况下连接两个节点的正确方法是什么?

【问题讨论】:

  • “我运行的守护进程基于 go-ipfs”是什么意思?你是在运行const ipfs = new IPFS 你启动了一个 JS ipfs 节点......另外,你是想在浏览器中运行它,还是在 nodejs 中运行它?

标签: node.js publish-subscribe ipfs libp2p


【解决方案1】:

也许我错了,但 npm 包 ipfs 是 IPFS 协议的完整实现,它在调用构造函数时创建一个节点,这就是为什么 ipfs daemon ... 不是必需的。如果您需要将其用作 ipfs 守护程序的 API,您可以使用 ipfs-http-client 包。

您可以使用ipfs-pubsub-room,它有一个基于此包ipfs-pubsub-room-demo 的工作示例。

希望对您有所帮助,我也在学习这项技术。

【讨论】:

  • 嗨 nahuallt,我不能用 ipfs-pubsub-room 使 ipfsd-ctrl 工作,它是一个控制器,它产生一个守护进程并使用 http-client 来处理这个守护进程,我得到了一些当我尝试使用客户端节点 API 时出现与 libp2p 相关的错误,知道吗?
【解决方案2】:

目前(2019-09-17)ipfs 网络中的大多数节点都没有启用 pubsub,因此您的 pubsub 消息通过的机会很小。 您可以尝试在您的节点之间建立直接连接,如下所述:managing swarm connections in ipfs

基本上:

  1. 在互联网可访问节点上运行“ipfs id”
  2. 检查输出,获取地址(它应该看起来像这样 /ip4/207.210.95.74/tcp/4001/ipfs/QmesRgiWSBeMh4xbUEHUKTzAfNqihr3fFhmBk4NbLZxXDP )
  3. 在其他节点上建立直连:

    ipfs 群连接 /ip4/207.210.95.74/tcp/4001/ipfs/QmesRgiWSBeMh4xbUEHUKTzAfNqihr3fFhmBk4NbLZxXDP

【讨论】:

    【解决方案3】:

    请查看 ipfs Github example,因为它显示了如何通过 WebRTC 将两个 js-ipfs 浏览器节点连接在一起。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-11
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多