Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated version #1

Closed
achingbrain opened this issue Jan 11, 2023 · 3 comments
Closed

Updated version #1

achingbrain opened this issue Jan 11, 2023 · 3 comments

Comments

@achingbrain
Copy link

I've made some improvements to this example - there are a few things:

  1. If no repo is specified, both nodes will attempt to load config etc from ~/.jsipfs which means they'll share a peer id, so instead ensure they have their own repos and as such their own peer identities
  2. Listen on a random TCP port to ensure there are no conflicts
  3. Wait for the publisher peer to see the subscriber peer in the peer list for the topic before publishing the message

Number 3 is quite important - once the subscriber peer appears in the list it means peer discovery has completed, the publisher is connected to the subscriber and the topic list has been exchanged.

If the message is sent before all this has happened it's possible the subscriber will miss it as it's not part of the topic*.

// constants.mjs
export const TOPIC = 'hjkhjkfdhsjafhjkdhjksajkhfdjhksajhk4242'
// sub.mjs
import * as IPFS from 'ipfs-core'
import os from 'os'
import path from 'path'
import { TOPIC } from './constants.mjs'

async function main() {
  // Create an IPFS instance
  const ipfs = await IPFS.create({
    // create a repo just for the subscriber node
    repo: path.join(os.tmpdir(), `sub-${Date.now()}`),
    config: {
      Addresses: {
        Swarm: [
          // listen on a random tcp port
          '/ip4/0.0.0.0/tcp/0'
        ]
      }
    }
  })

  // Subscribe to the "boop" topic
  ipfs.pubsub.subscribe(TOPIC, (msg) => {
    console.log(`Received message: ${msg.data.toString()}`)
  })
}

main()
// pub.mjs
import * as IPFS from 'ipfs-core'
import os from 'os'
import path from 'path'
import { TOPIC } from './constants.mjs'

async function main() {
  // Create an IPFS instance
  const ipfs = await IPFS.create({
    // create a repo just for the publisher node
    repo: path.join(os.tmpdir(), `pub-${Date.now()}`),
    config: {
      Addresses: {
        Swarm: [
          // listen on a random tcp port
          '/ip4/0.0.0.0/tcp/0'
        ]
      }
    }
  })

  // wait for topic peers
  while (true) {
    const peers = await ipfs.pubsub.peers(TOPIC)

    if (peers.length) {
      // there are some peers interested in this topic
      console.log("found topic peers")
      break
    }

    // wait a second to see if some topic peers are found
    await new Promise((resolve) => {
      setTimeout(() => {
        resolve()
      }, 1000)
    })
  }

  // Publish a message to the "boop" topic
  await ipfs.pubsub.publish(TOPIC, Buffer.from('Hello, world!'))

  console.log("published message to topic")
}

main()

* = If you have a large enough group of peers subscribed to the topic it's possible that the subscriber will still receive the message if it has a connection to another subscriber (transitive or direct) due to the gossip part of the gossipsub protocol, but if the publisher publishes a message without any peers it won't go anywhere. There is a gossipsub setting to cause publishing to zero peers to error out, this is currently disabled in js-ipfs but it should probably be enabled to make things a bit more transparent.

@michaelneale
Copy link
Owner

thanks @achingbrain have added that in and it works great.

@michaelneale
Copy link
Owner

michaelneale commented Jan 19, 2023

hey @achingbrain how do I setup this so that it announces so it can work from behind a NAT?

(using defaults I only see it listening:

Swarm listening on /ip4/127.0.0.1/tcp/4002/p2p/12D3KooWLAteDPP7d2UQcydBScYZeydNZ4irDwGUwDt3iWzzc6qB
Swarm listening on /ip4/192.168.0.102/tcp/4002/p2p/12D3KooWLAteDPP7d2UQcydBScYZeydNZ4irDwGUwDt3iWzzc6qB
Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/p2p/12D3KooWLAteDPP7d2UQcydBScYZeydNZ4irDwGUwDt3iWzzc6qB
)

not advertising or using a public IP

@2color
Copy link

2color commented Jan 19, 2023

To establish connectivity behind NAT, there are a couple of options:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants