Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Error On Using PubSub in Node.js #1038

Closed
beingmohit opened this issue Oct 12, 2017 · 3 comments
Closed

Error On Using PubSub in Node.js #1038

beingmohit opened this issue Oct 12, 2017 · 3 comments

Comments

@beingmohit
Copy link

beingmohit commented Oct 12, 2017

Type: Bug

Severity: Critical

Description: Error on using pubsub in nodejs

TypeError: Cannot read property 'listenerCount' of undefined
    at subscribe (/home/mohit/projects/iptv/node_modules/ipfs/src/core/components/pubsub.js:90:11)
    at Promise (/home/mohit/projects/iptv/node_modules/ipfs/src/core/components/pubsub.js:23:11)
    at Promise (<anonymous>)
    at Object.subscribe (/home/mohit/projects/iptv/node_modules/ipfs/src/core/components/pubsub.js:22:16)    at IPFSPubsub.subscribe (/home/mohit/projects/iptv/node_modules/orbit-db-pubsub/src/ipfs-pubsub.js:23:27)
    at OrbitDB._createStore (/home/mohit/projects/iptv/node_modules/orbit-db/src/OrbitDB.js:71:20)
    at OrbitDB.eventlog (/home/mohit/projects/iptv/node_modules/orbit-db/src/OrbitDB.js:29:17)
    at IPFS.ipfs.on (/home/mohit/projects/iptv/src/api/index.js:12:24)
    at emitNone (events.js:105:13)
    at IPFS.emit (events.js:207:7) Promise {
  <rejected> TypeError: Cannot read property 'listenerCount' of undefined
    at subscribe (/home/mohit/projects/iptv/node_modules/ipfs/src/core/components/pubsub.js:90:11)
    at Promise (/home/mohit/projects/iptv/node_modules/ipfs/src/core/components/pubsub.js:23:11)
    at Promise (<anonymous>)
    at Object.subscribe (/home/mohit/projects/iptv/node_modules/ipfs/src/core/components/pubsub.js:22:16)
    at IPFSPubsub.subscribe (/home/mohit/projects/iptv/node_modules/orbit-db-pubsub/src/ipfs-pubsub.js:23:27)
    at OrbitDB._createStore (/home/mohit/projects/iptv/node_modules/orbit-db/src/OrbitDB.js:71:20)
    at OrbitDB.eventlog (/home/mohit/projects/iptv/node_modules/orbit-db/src/OrbitDB.js:29:17)
    at IPFS.ipfs.on (/home/mohit/projects/iptv/src/api/index.js:12:24)
    at emitNone (events.js:105:13)
    at IPFS.emit (events.js:207:7) }
TypeError: Cannot read property 'publish' of undefined
    at Function.publish.promisify (/home/mohit/projects/iptv/node_modules/ipfs/src/core/components/pubsub.js:54:19)
    at /home/mohit/projects/iptv/node_modules/promisify-es6/index.js:41:20
    at Promise (<anonymous>)
    at Object.publish (/home/mohit/projects/iptv/node_modules/promisify-es6/index.js:35:16)
    at IPFSPubsub.publish (/home/mohit/projects/iptv/node_modules/orbit-db-pubsub/src/ipfs-pubsub.js:37:25)
    at Immediate.setImmediate [as _onImmediate] (/home/mohit/projects/iptv/node_modules/orbit-db/src/OrbitDB.js:88:54)
    at runCallback (timers.js:781:20)
    at tryOnImmediate (timers.js:743:5)
    at processImmediate [as _immediateCallback] (timers.js:714:5) Promise {
  <rejected> TypeError: Cannot read property 'publish' of undefined
    at Function.publish.promisify (/home/mohit/projects/iptv/node_modules/ipfs/src/core/components/pubsub.js:54:19)
    at /home/mohit/projects/iptv/node_modules/promisify-es6/index.js:41:20
    at Promise (<anonymous>)
    at Object.publish (/home/mohit/projects/iptv/node_modules/promisify-es6/index.js:35:16)
    at IPFSPubsub.publish (/home/mohit/projects/iptv/node_modules/orbit-db-pubsub/src/ipfs-pubsub.js:37:25)
    at Immediate.setImmediate [as _onImmediate] (/home/mohit/projects/iptv/node_modules/orbit-db/src/OrbitDB.js:88:54)
    at runCallback (timers.js:781:20)
    at tryOnImmediate (timers.js:743:5)
    at processImmediate [as _immediateCallback] (timers.js:714:5) }

On digging up in ipfs code, found
this._pubsub = undefined
on src/core/index.js:74

Where is pubsub module code?

Steps to reproduce the error:

const IPFS = require('ipfs')
const IpfsApi = require('ipfs-api')
const OrbitDB = require('orbit-db')

const ipfs = new IPFS()
const orbitdb = new OrbitDB(ipfs)

process.on('unhandledRejection', (e, p) => console.error(e,p));
ipfs.on('error', (e) => console.error(e));

ipfs.on('ready', (e) => {
    const db = orbitdb.eventlog('iptv')
    db.add(0);
})
@victorb
Copy link
Member

victorb commented Oct 12, 2017

You need to use EXPERIMENTAL.pubsub option when instantiating IPFS. Code will end up being:

const ipfs = new IPFS({
  EXPERIMENTAL: {
    pubsub: true
  }
})

@thisconnect
Copy link
Contributor

Could/should ipfs-api check if EXPERIMENTAL.pubsub is true or throw a descriptive error?

@victorb
Copy link
Member

victorb commented Oct 12, 2017

@thisconnect I was thinking about how we could make this error more descriptive to avoid these issues from being created but would involve either 1) having this._pubsub being a getter that we can throw if undefined with better error or 2) libraries checking if node.pubsub is actually defined before using it.

  1. would solve most cases but not sure about support in nodejs versions + browsers yet

  2. would require every library using js-ipfs to implement basically the same logic

MicrowaveDev pushed a commit to galtproject/js-ipfs that referenced this issue May 22, 2020
Saves 1 minute running them locally.

License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants