Closed raphael10-collab closed 3 years ago
@raphael10-collab do you have a local hyperspace server instance running? You can do that either over the hyp cli or in code, analogous to the hyperspace client. Also it might be necessary to await the .ready() of the client (and server)
@fsteff
With :
// https://github.com/fsteff/hyperpubsub#api
// https://hypercore-protocol.org/guides/getting-started/hyperspace/
const {PubSub} = require('hyperpubsub').debug() // call debug() if you want debugging messages printed to the cli
// ... set up or connect to a hyperspace instance
//client = a hyperspace client
const { Server: HyperspaceServer } = require('hyperspace')
const { Client: HyperspaceClient } = require('hyperspace')
const server = new HyperspaceServer()
const client = new HyperspaceClient()
const get_ready = async () => {
await server.ready()
await client.ready()
}
get_ready()
const pubsub = new PubSub(client.network, {application: 'example app', onError: console.log("error")})
pubsub.join('some topic') // returns Promise<string> of the discovery key used for the dht
pubsub.sub('some topic', (msg, app) => console.log(msg.toString('utf-8'))) // messages are binary blobs
pubsub.pub('some topic', Buffer.from('hello', 'utf-8')) // sends message to all known listening peers
pubsub.unsub('some topic') // no longer interested
pubsub.close() // cleanup
I get :
(base) raphy@pc:~/hypercore/hyperswarm/hyperpubsub$ node --trace-warnings hyperpubsub_api.js
error
<- unsub some topic broadcast
/home/raphy/hypercore/hyperswarm/node_modules/hyperpubsub/index.js:81
this.extension.broadcast({ topic, type: MSG_TYPE_UNSUBSCRIBE, application: opts.application })
^
ReferenceError: opts is not defined
at PubSub.unsub (/home/raphy/hypercore/hyperswarm/node_modules/hyperpubsub/index.js:81:84)
at Object.<anonymous> (/home/raphy/hypercore/hyperswarm/hyperpubsub/hyperpubsub_api.js:28:8)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
After modifying in /home/raphy/hypercore/hyperswarm/node_modules/hyperpubsub/index.js:81
opts
--> this.opts
,
I get:
(base) raphy@pc:~/hypercore/hyperswarm/hyperpubsub$ node --trace-warnings hyperpubsub_api.js
error
<- unsub some topic broadcast
(node:75233) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED /tmp/hyperspace.sock
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
at emitUnhandledRejectionWarning (internal/process/promises.js:168:15)
at processPromiseRejections (internal/process/promises.js:247:11)
at processTicksAndRejections (internal/process/task_queues.js:94:32)
(node:75233) Error: connect ECONNREFUSED /tmp/hyperspace.sock
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
(node:75233) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
at emitDeprecationWarning (internal/process/promises.js:180:11)
at processPromiseRejections (internal/process/promises.js:249:13)
at processTicksAndRejections (internal/process/task_queues.js:94:32)
(node:75233) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED /tmp/hyperspace.sock
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
at emitUnhandledRejectionWarning (internal/process/promises.js:168:15)
at processPromiseRejections (internal/process/promises.js:247:11)
at processTicksAndRejections (internal/process/task_queues.js:94:32)
(node:75233) Error: connect ECONNREFUSED /tmp/hyperspace.sock
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
(node:75233) UnhandledPromiseRejectionWarning: Error: RPC stream destroyed
at RPC._request (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/arpeecee/index.js:194:31)
at Method.request (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/arpeecee/index.js:247:29)
at HRPCServiceNetwork.open (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/@hyperspace/rpc/index.js:590:23)
at RemoteNetworker._open (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/@hyperspace/client/index.js:227:44)
at RemoteNetworker.ready (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/@hyperspace/client/index.js:257:27)
at HyperspaceClient.ready (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/@hyperspace/client/index.js:941:35)
at get_ready (/home/raphy/hypercore/hyperswarm/hyperpubsub/hyperpubsub_api.js:19:16)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at emitUnhandledRejectionWarning (internal/process/promises.js:168:15)
at processPromiseRejections (internal/process/promises.js:247:11)
at processTicksAndRejections (internal/process/task_queues.js:94:32)
(node:75233) Error: RPC stream destroyed
at RPC._request (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/arpeecee/index.js:194:31)
at Method.request (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/arpeecee/index.js:247:29)
at HRPCServiceNetwork.open (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/@hyperspace/rpc/index.js:590:23)
at RemoteNetworker._open (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/@hyperspace/client/index.js:227:44)
at RemoteNetworker.ready (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/@hyperspace/client/index.js:257:27)
at HyperspaceClient.ready (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/@hyperspace/client/index.js:941:35)
at get_ready (/home/raphy/hypercore/hyperswarm/hyperpubsub/hyperpubsub_api.js:19:16)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
@raphael10-collab this opts.application
looks like a bug, I'll fix that asap.
If you call get_ready()
an then proceed with the rest it doesn't wait for it to be done.
You could e.g. put it all into the function or into a .then(...) block, what ever suits your taste.
I'm not sure if I've done it correctly:
const {PubSub} = require('hyperpubsub').debug() // call debug() if you want debugging messages printed to the cli
// ... set up or connect to a hyperspace instance
//client = a hyperspace client
const { Server: HyperspaceServer } = require('hyperspace')
const { Client: HyperspaceClient } = require('hyperspace')
const server = new HyperspaceServer()
const client = new HyperspaceClient()
const get_ready = async () => {
await server.ready()
await client.ready()
}
try {
get_ready().then(() => {
const pubsub = new PubSub(client.network, {application: 'example app', onError: console.log("error")})
pubsub.join('some topic') // returns Promise<string> of the discovery key used for the dht
pubsub.sub('some topic', (msg, app) => console.log(msg.toString('utf-8'))) // messages are binary blobs
pubsub.pub('some topic', Buffer.from('hello', 'utf-8')) // sends message to all known listening peers
pubsub.unsub('some topic') // no longer interested
pubsub.close() // cleanup
})
} catch {
console.log("error")
}
With that:
(base) raphy@pc:~/hypercore/hyperswarm/hyperpubsub$ node --trace-warnings hyperpubsub_api.js
(node:10448) UnhandledPromiseRejectionWarning: Error: RPC stream destroyed
at RPC._request (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/arpeecee/index.js:194:31)
at Method.request (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/arpeecee/index.js:247:29)
at HRPCServiceNetwork.open (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/@hyperspace/rpc/index.js:590:23)
at RemoteNetworker._open (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/@hyperspace/client/index.js:227:44)
at RemoteNetworker.ready (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/@hyperspace/client/index.js:257:27)
at HyperspaceClient.ready (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/@hyperspace/client/index.js:941:35)
at get_ready (/home/raphy/hypercore/hyperswarm/hyperpubsub/hyperpubsub_api.js:19:16)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at emitUnhandledRejectionWarning (internal/process/promises.js:168:15)
at processPromiseRejections (internal/process/promises.js:247:11)
at processTicksAndRejections (internal/process/task_queues.js:94:32)
(node:10448) Error: RPC stream destroyed
at RPC._request (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/arpeecee/index.js:194:31)
at Method.request (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/arpeecee/index.js:247:29)
at HRPCServiceNetwork.open (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/@hyperspace/rpc/index.js:590:23)
at RemoteNetworker._open (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/@hyperspace/client/index.js:227:44)
at RemoteNetworker.ready (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/@hyperspace/client/index.js:257:27)
at HyperspaceClient.ready (/home/raphy/hypercore/hyperswarm/hyperpubsub/node_modules/@hyperspace/client/index.js:941:35)
at get_ready (/home/raphy/hypercore/hyperswarm/hyperpubsub/hyperpubsub_api.js:19:16)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:10448) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
at emitDeprecationWarning (internal/process/promises.js:180:11)
at processPromiseRejections (internal/process/promises.js:249:13)
at processTicksAndRejections (internal/process/task_queues.js:94:32)
Sorry, I missed that part myself... hyperspace can be a litte tricky to use. That's a useful pattern for starting hyperspace, it first tries to use an already running one:
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()
}
Also, testing hyperspace locally causes problems in some (many?) cases. For that purpose I'd recommend using the simulator
I've modified example.js
to contain the simplest possible example, hopefully that works for you.
@fsteff example.js
works fine.
I've tried to modify it in order to use @hyperspace/client
instead of thesimulator
which, as far as I understand, is suited mainly for testing purposes :
const {PubSub} = require('hyperpubsub').debug() // call debug() if you want debugging messages printed to the cli
//const simulator = require('hyperspace/simulator')
const HyperspaceClient = require('@hyperspace/client')
const example = async () => {
let client
try {
client = new HyperspaceClient()
//simulator().then(({client}) => {
const pubsub = new PubSub(client.network, {application: 'example app', onError: console.error})
pubsub.sub('some topic', (msg, app) => console.log(msg.toString('utf-8')), true) // messages are binary blobs
setInterval(send, 1000)
function send() {
pubsub.pub('some topic', Buffer.from('hello', 'utf-8'))
}
//})
} catch {
//console.log("error")
console.log('no hyperspace server running, starting up a new one')
server = new Server()
await server.ready()
client = new Client()
await client.ready()
}
}
example()
But its execution yields this error message :
(base) raphy@pc:~/hypercore/hyperswarm/hyperpubsub$ node --trace-warnings simple_example_without_simulator.js
(node:89019) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED /tmp/hyperspace.sock
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
at emitUnhandledRejectionWarning (internal/process/promises.js:168:15)
at processPromiseRejections (internal/process/promises.js:247:11)
at processTicksAndRejections (internal/process/task_queues.js:94:32)
(node:89019) Error: connect ECONNREFUSED /tmp/hyperspace.sock
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
(node:89019) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
at emitDeprecationWarning (internal/process/promises.js:180:11)
at processPromiseRejections (internal/process/promises.js:249:13)
at processTicksAndRejections (internal/process/task_queues.js:94:32)
What am I missing and / or doing wrongly?
If you do it that way the server is started after the pubsub stuff fails. If the client can't find a server you need to start one first.
You can run that snippet with multiple devices (if started multiple times locally it won't find the other running instances):
const {PubSub} = require('hyperpubsub').debug()
const {Server, Client} = require('hyperspace')
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})
pubsub.sub('some topic', (msg, app) => console.log(msg.toString('utf-8')), true) // messages are binary blobs
setInterval(send, 1000)
function send() {
pubsub.pub('some topic', Buffer.from('hello', 'utf-8'))
}
})
Thank you @fsteff !!!
With:
I get:
node: v14.15.5 OS: Ubuntu 20.04 Desktop