Reputation: 26966
If I run this code on my pc, I see my message, but another person on a different pc running the same code doesn't see mine. Is this a NAT thing perhaps? Or am I using this wrong?
const Room = require("ipfs-pubsub-room");
const ipfs = require("ipfs");
ipfs.create({}).then(async (node) => {
const room = new Room(node, "room-name");
room.on("peer joined", (peer) => {
console.log("Peer joined the room", peer);
});
room.on("peer left", (peer) => {
console.log("Peer left...", peer);
});
// now started to listen to room
room.on("subscribed", () => {
console.log("Now connected!");
});
room.on("message", ({ from, data }) =>
console.log(from, JSON.parse(data.toString()))
);
room.broadcast(JSON.stringify({ bla: "hello" }));
});
Upvotes: 1
Views: 873
Reputation: 206
Are you using this in Node.js or browser?
The problem should be that your peers are not connected between each other (or with a common set of peers running pubsub) to have the pubsub messages reliably forwarded. The first step for you to diagnose this is to use node.swarm.peers()
and see if you have the other peer.
Considering your peer is not connected to the other person node, you need to manually connect it, or configure a discovery mechanism to help you (this is automatic discovery and connectivity is a known problem for the community and we will be working on improving this experience). The simplest option is to use webrtc-star transport+discovery. You can see examples with it in ipfs browser exchange files and with libp2p.
With this, you will likely see the other peers connected and then be able to exchange Pubsub messages. You can see more about the discovery mechanisms available in Libp2p config.md and libp2p discovery examples.
Let me know if you could get your issue fixed
Upvotes: 1