fsteff / hyperpubsub

Hypercore Protocol Extension for a simple PubSub system
MIT License
8 stars 1 forks source link

It looks that the order of the execution, laptop -> PC versus PC -> laptop, is quite important: Why? And to make it work in both cases? #5

Open raphael10-collab opened 3 years ago

raphael10-collab commented 3 years ago

I'm here again in order to clarify and understand some aspects of the hyperpubsub behaviour .

(PC):

const {PubSub} = require('hyperpubsub').debug()
const {Server, Client} = require('hyperspace')
const util = require('util')
const setTimeoutPromise = util.promisify(setTimeout)

async function setUpHyperspace() {
  let client, server
  try {
    client = new Client()
    await client.ready()
  } catch(e) {
    console.log('no hyperspace server running, starting up a new one')
    server = new Server()
    await server.ready()
    client = new Client()
    await client.ready()
  }
  return {client, server}
}

setUpHyperspace().then(({client}) => {

  const pubsub = new PubSub(client.network, {application: 'example app', onError: console.error})

  function send() {
      pubsub.pub('some topic', Buffer.from('Hello from Raphy', 'utf-8'))
  }

  setTimeout(send, 200)

  pubsub.sub('some topic', (msg, app) => {
    setTimeout(send, 200)
    console.log(msg.toString('utf-8'))
  })

})

(laptop):

const {PubSub} = require('hyperpubsub').debug()
const {Server, Client} = require('hyperspace')
const util = require('util')
const setTimeoutPromise = util.promisify(setTimeout)

async function setUpHyperspace() {

  let client, server   try {     client = new Client()     await client.ready()   } catch(e) {     console.log('no hyperspace server running, starting up a new one')     server = new Server()     await server.ready()     client = new Client()     await client.ready()   }   return {client, server} }

setUpHyperspace().then(({client}) => {

  const pubsub = new PubSub(client.network, {application: 'example app', onError: console.error})

  function send() {       pubsub.pub('some topic', Buffer.from('Hello from Marco', 'utf-8'))   }

  setTimeout(send, 200)  
  pubsub.sub('some topic', (msg, app) => {     setTimeout(send, 200)     console.log(msg.toString('utf-8'))   })

})

If I execute simple_example_without_simulator.js first on the PC , and then after few seconds on the laptop I get this output (no messages exchange):

(PC) :

(base) raphy@pc:~/hypercore/hyperswarm/hyperpubsubPlaying$ node simple_example_without_simulator.js
no hyperspace server running, starting up a new one
joined topic "some topic" (bce5935f9a6ade33d955c843f9b906e1b1fe769be6f1757fff6ca0f644506e42)
<- sub some topic broadcast
peer-add 192.168.1.9
<- sub some topic to 192.168.1.9
-> msg sub some topic from 192.168.1.9
subscriber 192.168.1.9 added to topic some topic
-> msg sub some topic from 192.168.1.9
-> msg sub some topic from 192.168.1.9

(laptop):

(base) marco@marco-U36SG:~/hypercore/hyperswarm/hyperpubsubPlaying$ node simple_example_without_simulator.js 

no hyperspace server running, starting up a new one peer-add 192.168.1.7 <- sub some topic to 192.168.1.7 peer-add ::ffff:192.168.1.7 <- sub some topic to ::ffff:192.168.1.7 -> msg sub some topic from 192.168.1.7 subscriber 192.168.1.7 added to topic some topic peer-remove 192.168.1.7 subscriber 192.168.1.7 removed from topic some topic joined topic "some topic" (bce5935f9a6ade33d955c843f9b906e1b1fe769be6f1757fff6ca0f644506e42) <- sub some topic broadcast

But If I execute first on the laptop and few seconds later on the PC I get this output, which is the expected output:

(PC) :

(base) raphy@pc:~/hypercore/hyperswarm/hyperpubsubPlaying$ node simple_example_without_simulator.js
no hyperspace server running, starting up a new one
peer-add 192.168.1.9
<- sub some topic to 192.168.1.9
-> msg sub some topic from 192.168.1.9
subscriber 192.168.1.9 added to topic some topic
-> msg sub some topic from 192.168.1.9
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
joined topic "some topic" (bce5935f9a6ade33d955c843f9b906e1b1fe769be6f1757fff6ca0f644506e42)
<- sub some topic broadcast
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9
Hello from Marco
<- msg some topic to 192.168.1.9
-> msg data some topic from 192.168.1.9

(laptop) :

(base) marco@marco-U36SG:~/hypercore/hyperswarm/hyperpubsubPlaying$ node simple_example_without_simulator.js 
no hyperspace server running, starting up a new one
joined topic "some topic" (bce5935f9a6ade33d955c843f9b906e1b1fe769be6f1757fff6ca0f644506e42)
<- sub some topic broadcast
peer-add 192.168.1.7
<- sub some topic to 192.168.1.7
peer-add ::ffff:192.168.1.7
<- sub some topic to ::ffff:192.168.1.7
peer-remove 192.168.1.7
-> msg sub some topic from ::ffff:192.168.1.7
subscriber ::ffff:192.168.1.7 added to topic some topic
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
-> msg sub some topic from ::ffff:192.168.1.7
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy
<- msg some topic to ::ffff:192.168.1.7
-> msg data some topic from ::ffff:192.168.1.7
Hello from Raphy

Why does this happen? How to make it work also when I execute first on PC and then after few seconds on the laptop?

fsteff commented 3 years ago

I'd guess this is only a timing issue - if you call send within the sub callback and the other one doesn't get you subscribe message within 200ms it's not repeated. Might be caused by some networking stuff.