1-1 messaging for ipfs nodes. Emits connection events, listening for messages, send messages to nodes. Best suited for server-to-server messaging.
npm install @0xvaibhav/ipfs-messenger
Initializing and starting the messenger.
import * as IPFS from "ipfs-core";
import Messenger from "@0xvaibhav/ipfs-messenger";
const ipfs = await IPFS.create();
const messenger = new Messenger(ipfs, {});
await messenger.start();
Listen for events emitted when starting messenger.
messenger.on("start", (m) => {
console.log(m.message)
})
Once messenger has been started, listen to incoming messages.
messenger.on("message", (m) => {
console.log(m.data)
});
Make sure to establish connection with other node before sending any message and only on establishment of connection, proceed to send any message.
messenger.on("connected", (d) => {
console.log(`Connected to ${d.address}`)
messenger.send("to-address", "Any Message (Objects should be stringyfied)")
})
messenger.connect("ipfs-address")
Close connection and stop listening for messages.
messenger.on("disconnected", (d) => {
console.log(`Disconnected from ${d.address}`)
})
messenger.on("stop", () => {
console.log("Stopped listening to any incoming messages")
})
messenger.disconnect("ipfs-address")
messenger.stop()
ipfs
: IPFS Node, Must have pubsub activated.
const messenger = new Messenger(ipfs, {})
Starts the messenger
Stops the messenger
ipfsAddr
: string, Address of IPFS node to connect with. Establish connection to IPFS node. Required before sending any message.
Terminate the connection established with an IPFS node.
message
: string, Data to be sent across. Send message to the IPFS node with which connection has already been established.
Once the messenger has been started.
Once the messenger has been stopped.
When connected with an IPFS node.
When disconnect with an IPFS node.
When a message is received.
Once a message has been successfully sent.
Feel free to join in. All welcome. Open an issue!