4 - Chat server with discovery.

Up until now, the client has had the server address hardcoded on the code, but what if we want to discover the server without knowing it's IP? We can do that using something called Multicast DNS.

Multicast DNS is a standard protocol that allows you to do service discovery on a local network without having a centralized server. For example when a printer automatically shows up in a print dialog its because it announces it self using Multicast DNS

Build upon problem 3 to make the server discoverable through mdns using a custom server nickname passed in as a command line argument and make the client connect to it using the same nickname.

Tips

Multicast DNS works almost just like regular DNS so you won't have to modify your code that much.

You'll need to register a multicast dns name in your server and resolve it in your client. To register you can use a module called register-multicast-dns which allows you to route a .local domain to your computer.

For example if you add the following to your previous server.js implementation

var register = require('register-multicast-dns')
register('this-is-a-test.local')

You be able to find that server using the this-is-a-test.local hostname.

To make this hostname resolve from node you'll need add multicast dns lookup support to your client.

You can do that by using the lookup-multicast-dns module and adding the following snippet to your client.js

require('lookup-multicast-dns/global') // your node program will now resolve .local domains
var socket = net.connect(10000, 'this-is-a-test.local') // will route to the above server

To get the nickname you should use from the command line you can do

var nickname = process.argv[2]

Testing

Open 3 terminals, run

node server.js {your-nickname}

on one of them and

node client.js {your-nickname}

on the other 2. Clients should be able to chat and you should be able to see the nicknames of the clients.

After you test it on your machine, try running the server and the client on different machines, and check out how the client discovers the server.

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