I’ve been playing around with mDNS lately for broadcasting some services for applications to auto-connect with.
The first experiment I had was setting up a server that broadcasts a TCP endpoint for an Electron application to discover and connect for the application data.
This was so easily done that I challenged myself to see how fast I can whip out a blog post.
First, get an Ubuntu server up (I used a Vagrant VM).
Run the commands:
From here, the service for avahi (mdns) should be auto-started. Edit the configuration to enable broadcasting:
vim /etc/avahi/avahi-daemon.conf – here’s a config that’s minimally broadcasting only the IPv4 address:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Now, create a service configuration:
vim /etc/avahi/services/mywebserver.service, with this contents:
1 2 3 4 5 6 7
Simple as that. Just restart the avahi-daemon –
sudo service avahi-daemon restart.
This should now have your server broadcasting that it has a webserver running at port 80, named
To check the service is broadcasting, run
avahi-browse _http._tcp -tr – this should show your server as servername.local, with
Webserver, pointing to its IP and port.
1 2 3 4 5 6
Now for the electron portion, in your application, install the node mdns module:
npm install --save mdns.
This will add the node module to your project, but since it has native compilation steps, you must build it with
electron-rebuild. Do this:
npm install --save-dev electron-rebuild.
./node_modules/.bin/electron-rebuild – this will rebuild the mdns module for your electron node version correctly.
To do the DNS lookups, simply run the steps from the node mdns
README. Set the discovery type to
http and it will find your service. From there, you can grab the address and then get the data from the web server (or html page redirection) as you so wish!