{"action":"created","issue":{"url":"https://api.github.com/repos/nodejs/help/issues/1207","repository_url":"https://api.github.com/repos/nodejs/help","labels_url":"https://api.github.com/repos/nodejs/help/issues/1207/labels{/name}","comments_url":"https://api.github.com/repos/nodejs/help/issues/1207/comments","events_url":"https://api.github.com/repos/nodejs/help/issues/1207/events","html_url":"https://github.com/nodejs/help/issues/1207","id":313083775,"node_id":"MDU6SXNzdWUzMTMwODM3NzU=","number":1207,"title":"NodeJS sockets, how to receive multiple multicast address on the same port","user":{"login":"lantern77","id":5553615,"node_id":"MDQ6VXNlcjU1NTM2MTU=","avatar_url":"https://avatars2.githubusercontent.com/u/5553615?v=4","gravatar_id":"","url":"https://api.github.com/users/lantern77","html_url":"https://github.com/lantern77","followers_url":"https://api.github.com/users/lantern77/followers","following_url":"https://api.github.com/users/lantern77/following{/other_user}","gists_url":"https://api.github.com/users/lantern77/gists{/gist_id}","starred_url":"https://api.github.com/users/lantern77/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lantern77/subscriptions","organizations_url":"https://api.github.com/users/lantern77/orgs","repos_url":"https://api.github.com/users/lantern77/repos","events_url":"https://api.github.com/users/lantern77/events{/privacy}","received_events_url":"https://api.github.com/users/lantern77/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2018-04-10T20:49:00Z","updated_at":"2018-06-01T15:13:53Z","closed_at":null,"author_association":"NONE","body":"* **Node.js Version**: 6.11.2\r\n* **OS**: Linux (Ubuntu Server)\r\n* **Scope (install, code, runtime, meta, other?)**: Code\r\n* **Module (and version) (if relevant)**: DGram (https://nodejs.org/dist/latest-v6.x/docs/api/dgram.html)\r\n\r\n\r\n\r\nHello I am currently trying to receive information sent over multiple network interface, with multiple multicast address, but they all share the same port.\r\nPut it more simply, I am binding a socket to listen to a specific port and wish to filter out data that only contain a specific multicast address and was provided in a specific multicastInterface.\r\n\r\nOriginal sample code here:\r\n```js\r\n/**\r\n     * Create instance of the multicast receiver\r\n     * @param multicast, The multicast address we want to receive on, of form { 'ips' : ['addy'], 'port' : # }\r\n     * @param localIPs, an array of ip addresses, [ \"\",\"\",... ] that represent the network interfaces we are trying to filter on\r\n     * @param transmit, Does something with the message that we received, should be a function\r\n     *                  function(message, info, details) {}\r\n     *                     message, the message received on the multicast\r\n     *                     info,    an object of the source information (contains properties: 'address', 'family', 'port', 'size'),\r\n     *                     details, an object that gets passed to the consuming function containing extra information pertinent to the multicast receiver.\r\n     * @return receiver, an object that can receive content\r\n     */\r\n    'getMulticastUDPReceiver' : function(multicast, localIPs, transmit) {\r\n\r\n        // create the connection\r\n    \t\r\n        var socket = dgram.createSocket({'type' : 'udp4', 'reuseAddr' : true});\r\n        var receiver = {\r\n            'connection' : socket\r\n        };\r\n  \r\n        \r\n        // grab address information\r\n        var port = multicast['port'];\r\n        var addresses = multicast['ips'];\r\n        \r\n        // Extra arguments that get passed to the transmit consumer function.\r\n        var details = {};\r\n        details['multicast'] = multicast;\r\n        \r\n        // on socket error event handler\r\n        socket.on('error', function(exception) {\r\n                var address = receiver['address'];\r\n                console.log('Multicast Error : ' + (address ? (address['address'] + ':' + address['port']) : 'unknown issue'));\r\n        });\r\n\r\n        // on socket close event handler\r\n        socket.on('close', function() {\r\n                var address = receiver['address'];\r\n               console.log('Multicast Close : ' + (address ? (address['address'] + ':' + address['port']) : 'unknown address'));\r\n        });\r\n\r\n        // on message receive event handler\r\n        socket.on('message', function(message, info) {\r\n        \t\r\n            // call the message event handler function\r\n            if (transmit)\r\n                transmit(message, info, details);\r\n        });\r\n\r\n        // bind to the multicast port\r\n        var multicastPort = multicast['port'];\r\n        var multicastIPs  = multicast['ips'];\r\n        \r\n        // 239.71.238.19 :1234\r\n        \r\n        // establish connection\r\n        socket.bind({ 'port' : multicastPort, 'exclusive' : false }, function() {\r\n        \tsocket.setMulticastLoopback(true);\r\n        \tsocket.setMulticastTTL(128);\r\n        \t\r\n            // cache the address for use\r\n            receiver['address'] = socket.address();\r\n            \r\n            var address = receiver['address'];\r\n            console.log('Multicast Connect : ' + (address ? (address['address'] + ':' + address['port']) : 'unknown address'));\r\n\r\n            // loop through the multicast groups and join with the interfaces\r\n            for (var index1 = 0; index1 < multicastIPs.length; index1++) {\r\n                var multicastIP = multicastIPs[index1];\r\n                \r\n                // for each local ip add membership\r\n                for (var index2 = 0; index2 < localIPs.length; index2++) {\r\n                    var localIP = localIPs[index2];\r\n                    \r\n                    try {\r\n                    \tconsole.log(\"Add Membership : \" + multicastIP + ':' + multicastPort + ' on ' + localIP);\r\n                    \t\r\n                        // attempt to subscribe\r\n                        socket.addMembership(multicastIP, localIP);\r\n                        console.log('Multicast Joined : ' + multicastIP + ':' + multicastPort + ' on local : ' + localIP);\r\n                    } catch(exception) {\r\n                            console.log('Multicast Join Failed : ' + multicastIP + ':' + multicastPort + ' on local : ' + localIP);\r\n                    }\r\n                }\r\n            }\r\n        });\r\n\r\n        // return the multicast receiver instance\r\n        return receiver;\r\n```\r\n\r\nInitially this code works, however for some strange reason, every once in awhile( beyond my understanding at this moment)\r\nany information set on the same port is still receives and **socket.on('message', function(){})** is triggered(Note this does not happen consistently and the code will work sometimes?). Thus if we set the multicast object to be \r\n``` js\r\n{\r\n   \"ips\"  : ['239.0.0.1'],\r\n   \"port\": \"1234\"\r\n}\r\n```\r\nand the data coming over the wire has the multicast address and port of 239.71.238.19 :1234 it still triggers the socket on message call.\r\n\r\nTo make sure, I wasn't missing anything I've been scouring on the internet looking for a solution\r\nhttps://github.com/nodejs/node/issues/1690\r\nand mocked my code to look like **receive-specific-address.js** with the LOCALIP replaced with MULTICAST_IP as indicated by **santigimeno**.\r\n\r\nHowever that code does not work for me as I would like to listen to multiple multicast addresses that have the same port (Edit: Tried it anyways and unfortunately it fails when there is a lot of data, still no idea as to why). Sorry to bother you guys with some noobie question. But I have been pulling my hair for the past few days around this issue.\r\n\r\n### Edit: \r\nSo I have been doing a bit more testing and found that if this bug only occurs when there is a LOT of information sent over the wire. Note that this code gets called on start up, would it be possible that the bind callback function is not being called on time as the data is coming in and thus the multicast is not being joined?\r\nOtherwise, when data is coming in at a slow rate it seems to be working. \r\n\r\nI have tried updating my code so that the socket.on message occurs after the bind (so now its in the callback function), however to no avail, it still fails to behave as intended when massive amount.\r\n**Note** this data is coming in even before the code runs\r\n\r\n\r\n### Edit \r\nFound something else to add to this, I found out that there is another node process that is also binding to the same port, its also runs the same code but with different addresses.\r\n"},"comment":{"url":"https://api.github.com/repos/nodejs/help/issues/comments/393911756","html_url":"https://github.com/nodejs/help/issues/1207#issuecomment-393911756","issue_url":"https://api.github.com/repos/nodejs/help/issues/1207","id":393911756,"node_id":"MDEyOklzc3VlQ29tbWVudDM5MzkxMTc1Ng==","user":{"login":"lantern77","id":5553615,"node_id":"MDQ6VXNlcjU1NTM2MTU=","avatar_url":"https://avatars2.githubusercontent.com/u/5553615?v=4","gravatar_id":"","url":"https://api.github.com/users/lantern77","html_url":"https://github.com/lantern77","followers_url":"https://api.github.com/users/lantern77/followers","following_url":"https://api.github.com/users/lantern77/following{/other_user}","gists_url":"https://api.github.com/users/lantern77/gists{/gist_id}","starred_url":"https://api.github.com/users/lantern77/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lantern77/subscriptions","organizations_url":"https://api.github.com/users/lantern77/orgs","repos_url":"https://api.github.com/users/lantern77/repos","events_url":"https://api.github.com/users/lantern77/events{/privacy}","received_events_url":"https://api.github.com/users/lantern77/received_events","type":"User","site_admin":false},"created_at":"2018-06-01T15:13:53Z","updated_at":"2018-06-01T15:13:53Z","author_association":"NONE","body":"I'll post my more cleaner reproduction here as well, I have 3 files\r\n**socketClient.js, socketClient2.js, and socketServer.js.**\r\nsocketServer.js broadcasts UDP messsages on single multicast address(239.0.0.2) and a single port (1234)\r\nwhile socketClient.js is attempting to receive on multicast address(239.0.0.1) and a port (1234)\r\nwhile socketClient2.js is attempting to receive on multicast address(239.0.0.2) and a port (1234)\r\n\r\nIN the end running the server and both clients will cause the clients to receive message from the server when only socketClient2.js should be receiving it. This doesn't seem to be intended behaviour, but I could be wrong. Is there something wrong with my system, and what is the best way to look into this. Been working on this on and off for weeks and its driving me nuts xD\r\n\r\nCode samples below to reproduce:\r\n**socketServer.js**\r\n```javascript\r\nvar dgram = require('dgram');\r\n\r\nvar socket = dgram.createSocket('udp4');\r\nvar PORT = '1234';\r\nvar MULTICAST_ADDR = '239.0.0.2';\r\n \r\n // bind the udp socket to the specified interface ip and enable broadcasting\r\nsocket.bind(\r\n\t\t{\r\n\t\t'address' : '172.16.110.24'\r\n\t\t}\r\n\t\t,function() {\r\n\r\n\t\t// set multicast\r\n\t\tsocket.setBroadcast(true);\r\n\t\tsocket.setMulticastTTL(128);\r\n\t\tsetInterval(function () {\r\n\t\t\t\tlet message = new Buffer(new Date().toLocaleTimeString());\r\n\t\t\t\tsocket.send(message, 0, message.length, PORT, \r\n\t\t\t\t\t\tMULTICAST_ADDR, function () {\r\n\t\t\t\t\t\tconsole.log(\"Sent '\" + message + \"'\");\r\n\t\t\t\t\t\t});\r\n\t\t\t\t}, 1000);\r\n\t\t}\r\n);\r\n\r\n```\r\n\r\n\r\n**socketClient.js**\r\n```javascript\r\nvar PORT = '1234';\r\nvar MULTICAST = '239.0.0.1';\r\nvar adapter = '172.16.110.24';\r\n//var adapter = '10.10.160.103';\r\nvar dgram = require('dgram');\r\nvar client = dgram.createSocket({'type' : 'udp4', 'reuseAddr': true});\r\n\r\nclient.on('listening', function () {\r\n    var address = client.address();\r\n    //client.setMulticastLoopback(false);\r\n    console.log('UDP Client listening on ' + address.address + \":\" + address.port);\r\n    client.setMulticastTTL(128); \r\n    client.addMembership(MULTICAST, adapter);\r\n});\r\n\r\nclient.on('message', function (message, remote) {   \r\n    console.log('A: Epic Command Received. Preparing Relay.');\r\n    console.log('B: From: ' + remote.address + ':' + remote.port +' - ' + 'message');\r\n});\r\n\r\nclient.bind({'port' : PORT});\r\n```\r\n\r\n\r\n**socketClient2.js**\r\n```javascript\r\nvar PORT = '1234';\r\nvar MULTICAST = '239.0.0.2';\r\n//var adapter = '10.10.160.103';\r\nvar adapter = '172.16.110.24';\r\nvar dgram = require('dgram');\r\nvar client = dgram.createSocket({'type' : 'udp4', 'reuseAddr' : true});\r\n\r\nclient.on('listening', function () {\r\n    var address = client.address();\r\n    //client.setMulticastLoopback(false);\r\n    console.log('UDP Client listening on ' + address.address + \":\" + address.port);\r\n    client.setMulticastTTL(128); \r\n    client.addMembership(MULTICAST, adapter);\r\n});\r\n\r\nclient.on('message', function (message, remote) {   \r\n    console.log('A: Epic Command Received. Preparing Relay.');\r\n    console.log('B: From: ' + remote.address + ':' + remote.port +' - ' + 'message');\r\n});\r\n\r\nclient.bind({'port' : PORT});\r\n```"}}
{"actor":{"display_login":"lantern77"}}