Skip to content

Instantly share code, notes, and snippets.

@max-mapper
Last active March 14, 2017 22:30
Show Gist options
  • Save max-mapper/0532dd0fda601396dc31361e54ddcab7 to your computer and use it in GitHub Desktop.
Save max-mapper/0532dd0fda601396dc31361e54ddcab7 to your computer and use it in GitHub Desktop.
read multicast packets from node
var socket = dgram.createSocket({ type: 'udp4', reuseAddr: true })
socket.bind(5004, function () {
socket.addMembership('239.255.42.42', getIpForInterface('en2'))
socket.setMulticastTTL(255)
})
socket.on('message', function (m) {
// m is a buffer
})
function getIpForInterface (ifname) {
return os.networkInterfaces()[ifname].reduce(function (i, j) {
if (i.family === 'IPv4') return i
return j
}).address
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment