Skip to content

Instantly share code, notes, and snippets.

@ghiliweld
Last active August 1, 2019 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghiliweld/879408f9a03badef7a59907e11246ebd to your computer and use it in GitHub Desktop.
Save ghiliweld/879408f9a03badef7a59907e11246ebd to your computer and use it in GitHub Desktop.
Ephemeral Chat on IPFS

Ephemeral Chat on IPFS

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.

Approach

dependencies

npm install ipfs ipfs-pubsub-room

problem

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.

solution

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.

Example

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'
  }))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment