I'm building a P2P messaging platform using Node.JS, IPFS (the web3 decentralized blockchain module), and ipfs-http-client.
I'm trying to identify and print only the most recent message sent to an IPFS topic by a user with a peerId as specified in the variable peerId.
The code below seems to print all the messages received in the topic.
What is the best way to identify the most recent message sent to the topic by a specified peer?
I'm trying to use seqno or date to identify the most recent message but having trouble understanding how.
Is there a better way to do this?
Thanks in advance!
const IPFS = require('ipfs')
const IpfsHttpClient = require('ipfs-http-client').create;
const uint8ArrayToString = require("uint8arrays/to-string").toString;
let ipfs
let topic=``
let peerId=``
async function nodeConnect (node_url) {
ipfs = IpfsHttpClient(node_url)
}
...
...
async function subscribe (topic) {
await nodeConnect('/ip4/.../tcp/5001')
await ipfs.pubsub.subscribe(topic, msg => {
const from = msg.from
const seqno = uint8ArrayToString(msg.seqno, 'base16')
if (from === peerId) return console.log(`Ignoring message ${seqno} from self`)
console.log(`Message ${seqno} from ${from}:`)
try {
console.log(JSON.stringify(uint8ArrayToString(msg.data), null, 2))
} catch (_) {
console.log(uint8ArrayToString(msg.data, 'base16'))
}
})
}
...
...