Alright! Let's do some peer to peer. Up until now, we've been doing client server programs. That was to show the convenience that node can have as a platform to build network programs. But now, let's focus on actual peer to peer stuff.
You should start by creating a new file since this exercise is gonna take a new approach than the previous ones.
The first program we are going to write is a program that simply connects to other peers. It doesn't do anything with the connection yet, it just says that a new connection has been established and that's that.
You would run this program and pass a port for it to run, and the ports of the other expected peers.
If 5 instances of the program are running, there should be a connection between any two instances (i.e. the network should be fully connected).
There is a module on npm called fully-connected-topology, this module creates a fully connected network based on a list of addresses. Read the docs and you'll be on your way.
To get a list of peers to connect to inside your program passed as command line arguments you could do the following
var me = process.argv[2] // first argument is gonna be your own address
var peers = process.argv.slice(3) // the rest should be the peers you want to connect to
Run this commands in 3 different terminals.
node peer.js localhost:3000 localhost:3001 localhost:3002
node peer.js localhost:3001 localhost:3000 localhost:3002
node peer.js localhost:3002 localhost:3000 localhost:3001
They should all print the new connection message twice.
When you are done click here to go to the next exercise