Hi All,
I’d like to have a browser application dial a localhost node.js service (both running libp2p) such that I can use the same app specfic protocols for peers as the browser within the node.js service.
I’m currently trying to do this in the browser with code that looks like this:
const peerId = await PeerId.create();
libp2p = await Libp2p.create({
peerId,
modules: {
transport: [Websockets],
connEncryption: [NOISE],
streamMuxer: [MPLEX],
},
});
// start libp2p
await libp2p.start();
console.log("libp2p has started");
const conn = await libp2p.dial(
"/ip4/192.168.1.9/tcp/8000/ws/p2p/QmZ2R1h21iPYdsCiNeP3Dj8v2NgKkdzmGgXwC3fHwVSAou"
);
but I’m getting an error that
Error: The dial request has no valid addresses
Am I doing something simple wrong?
Thanks,
Tim
1 Like
cheako
January 6, 2022, 9:18pm
2
It’s possible that I am misconstructing the multiaddress because what I am trying to do is use Websockets with this address:
"/ip4/192.168.1.9/tcp/8000/ws/p2p/QmZ2R1h21iPYdsCiNeP3Dj8v2NgKkdzmGgXwC3fHwVSAou"
Is there another way I should be specifying this address to have the browser dial open a websocket connection locally?
For reference, here is my node.js service, which seems to work for node.js <-> node.js:
const peerId = await PeerId.create();
const node = await Libp2p.create({
peerId,
addresses: {
listen: ["/ip4/0.0.0.0/tcp/8000/ws"],
},
modules: {
transport: [WebSockets],
connEncryption: [NOISE],
streamMuxer: [MPLEX],
},
});
// start libp2p
await node.start();
1 Like
It turns out that the default filter prevents dialing with ws and you have to use secure websockets (wss).
If this isn’t right for your use case, you can instead use the filter.all
filter to allow this ala:
const peerId = await PeerId.create();
libp2p = await Libp2p.create({
peerId,
modules: {
transport: [Websockets],
connEncryption: [NOISE],
streamMuxer: [MPLEX],
},
config: {
transport: {
WebSockets: {
filter: filters.all,
},
},
},
});
// start libp2p
await libp2p.start();
console.log("libp2p has started");
const conn = await libp2p.dial(
"/ip4/127.0.0.1/tcp/8000/ws/p2p/QmZ2R1h21iPYdsCiNeP3Dj8v2NgKkdzmGgXwC3fHwVSAou"
);
3 Likes
kostysh
February 6, 2023, 10:43am
5
timfpark:
filters.all,
}
Thank you for this good solution! I am almost broke my head trying to understand why this websockets browser-standalone node connection is not working.
1 Like