3box issue for ephemeral chat on ipfs
ipfs-pubsub-room github page
This document explains a way to implement ephemeral chats on ipfs that conforms to the api described in the 3box issue.
npm install ipfs ipfs-pubsub-room
Since 3box only has one pinning node, we need a chat system that uses pubsub but that doesn't depend on circulating a message among many nodes, since all peers will be ineteracting with the chat system through the single 3box node.
Fortunately, ipfs-pubsub-room
allows messages to be broadcasted such that the broadcaster can still receive the broadcasted message. This should eliminate the need for a rendez-vous server.
Peers leaving or joining the chat also can't be detected if they're all on one peer, so again we leverage the room.broadcast()
api but this time instead of sending messages, we can broadcast events such as a peer joining/leaving the chat.
room.on('message', (message) => {
const msg = message.data.toString()
const json = JSON.parse(msg)
switch (json.message) {
case 'joining':
console.log('peer joined')
break;
case 'leaving':
console.log('peer left')
break;
default:
console.log('New message: ', json.message);
}
})
room.broadcast(JSON.stringify({
type: 'notification',
message: 'joining'
}))
room.broadcast(JSON.stringify({
type: 'notification',
message: 'leaving'
}))
room.broadcast(JSON.stringify({
type: 'chat',
message: 'hello'
}))