Universal Plug and Play (UPnP) with NodeJS
Introduction
UPnP architecture allows devices to device communications among home entertainment devices, consumer electronics devices, wireless device etc. This uses UDP port 1900 and TCP port 2869. Participating devices use Simple Service Device Discovery protocol (SSDP) to discover other devices.Enable UPnP in Windows 7
Window 7 support UPnP for sharing videos, photos, and files to other supported devices including Smart TVs, SetTopBox devices etc.- Go to Control Panel\All Control Panel Items
- Network and Sharing Center
- Choose home group and sharing options
- Create a homegroup
- Select the options you wish to enable and click Next
- Write down the password and 'Finish'
Now your pc is set up as a UPnP enabled devices
Install NodeJS
The sample code discussed here require nodejs to run. You may find the installation details here (NodeJS).
Search
This sample I used is inspired from this github - https://gist.github.com/chrishulbert/895382. This is good for most of the part but had to make two changes to see the code start working and discovering the devices.
Modified code is as follows.
var dgram = require('dgram'); // dgram is UDP
// Listen for responses
function listen(port) {
console.log("port : " + port);
var server = dgram.createSocket("udp4");
server.on("message", function (msg, rinfo) {
console.log("server got: " + msg + " from " + rinfo.address + ":" + rinfo.port);
});
server.bind(port); // Bind to the random port we were given when sending the message, not 1900
// Give it a while for responses to come in
setTimeout(function(){
console.log("Finished waiting");
server.close();
},6000);
}
function search() {
var message = new Buffer(
"M-SEARCH * HTTP/1.1\r\n" +
"HOST:239.255.255.250:1900\r\n" +
"ST:ssdp:all\r\n" + // Essential, used by the client to specify what they want to discover, eg 'ST:ge:fridge'
"MAN:\"ssdp:discover\"\r\n" +
"MX:1\r\n" + // 1 second to respond (but they all respond immediately?)
"\r\n"
);
var client = dgram.createSocket("udp4");
client.bind(8091); // So that we get a port so we can listen before sending
listen(8091);
client.send(message, 0, message.length, 1900, "239.255.255.250", function(err, bytes) {
console.log("ERR: " + err)
client.close();}
);
}
search();
Notice that the client.cose() had to be inside the callback of '.send'.
Now, you may start running this sample code, if you are not receiving any response for the search request, disable the wireless connectivity on your PC and try again.
Listen on Port 1900
Since all devices uses UDP port 1900 to do search for devices, you could listen to port 1900 and see all queries like SEARCH, NOTIFY etc from other devices.
The code can be found here - https://github.com/harikandakkai/ssdp - and is as follows:
var PORT = 1900;
var HOST = '192.168.1.2'; //This is your local IP
var dgram = require('dgram');
var client = dgram.createSocket('udp4');
client.on('listening', function () {
var address = client.address();
console.log('UDP Client listening on ' + address.address + ":" + address.port);
client.setMulticastTTL(128);
client.addMembership('239.255.255.250', HOST);
});
client.on('message', function (msg, remote) {
console.log('UPnP Broadcast recieved.');
console.log('From: ' + remote.address + ':' + remote.port +' \n\n' + msg);
});
client.bind(PORT);
Output for the above listener is as follows:
Comments
Post a Comment