Simple P2P Chat!

Now that we can create a p2p network, let's use it for some chatting. We would provide this program with a username, the address to use and a list of addresses for other peers. Then it should indicate when connections are established (just like the past example), and whatever a user write in his terminal, should be shown in the other users' terminals.

Tips

You should build on top of the solution to problem 5. Our solution looks like this.

var topology = require('fully-connected-topology')
var me = process.argv[2]
var peers = process.argv.slice(3)

var swarm = topology(me, peers)

swarm.on('connection', function (socket, id) {
  console.log('new connection from', id)
})

Similar to problem 3 we want to send messages with a username so you'll probably want to look at how you solved that problem to help with this one. Similar to problem 3 you could use duplex-json-stream to add a protocol and stream-set to track the open connections.

Testing

Run this commands in 3 different terminals.

node peer.js eduardo localhost:3000 localhost:3001 localhost:3002
node peer.js mafintosh localhost:3001 localhost:3000 localhost:3002
node peer.js your-name localhost:3002 localhost:3000 localhost:3001

The clients should be able to chat between them.

Wo-hoo! You created a chat system that doesn't require a centralized server.

When you are done click here to go to the next exercise