{"action":"created","issue":{"url":"https://api.github.com/repos/einaros/ws/issues/426","labels_url":"https://api.github.com/repos/einaros/ws/issues/426/labels{/name}","comments_url":"https://api.github.com/repos/einaros/ws/issues/426/comments","events_url":"https://api.github.com/repos/einaros/ws/issues/426/events","html_url":"https://github.com/einaros/ws/issues/426","id":53282067,"number":426,"title":"ws.broadcast messages output undefined to console and the client browser","user":{"login":"r3wt","id":4483325,"avatar_url":"https://avatars.githubusercontent.com/u/4483325?v=3","gravatar_id":"","url":"https://api.github.com/users/r3wt","html_url":"https://github.com/r3wt","followers_url":"https://api.github.com/users/r3wt/followers","following_url":"https://api.github.com/users/r3wt/following{/other_user}","gists_url":"https://api.github.com/users/r3wt/gists{/gist_id}","starred_url":"https://api.github.com/users/r3wt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/r3wt/subscriptions","organizations_url":"https://api.github.com/users/r3wt/orgs","repos_url":"https://api.github.com/users/r3wt/repos","events_url":"https://api.github.com/users/r3wt/events{/privacy}","received_events_url":"https://api.github.com/users/r3wt/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-01-02T22:19:20Z","updated_at":"2015-01-02T23:13:37Z","closed_at":"2015-01-02T22:30:41Z","body":"using the following code\r\n\r\n```js\r\n\"use strict\";\r\nvar http = require('http'),\r\nWebSocketServer = require('ws').Server,\r\nport = 9001\r\n;\r\nweb_server = http.createServer(function(req, res) {\r\n\tres.writeHead(200);\r\n}).listen(port, function() {\r\n\tconsole.log('websocket server listening on port %d\\n',port);\r\n});\r\nvar ws = new WebSocketServer({\r\n\tserver : web_server,\r\n\tclientTracking: true,\r\n});\r\nws.broadcast = function broadcast(data) {\r\n\tfor(var i in this.clients){\r\n\t\tif(this.clients[i].readyState !== this.clients[0].OPEN){\r\n\t\t\tconsole.error('Client state is %s',this.clients[i].readyState);\r\n\t\t}else{\r\n\t\t\tthis.clients[i].send(data,{binary:false,masked:false},function ack(error){\r\n\t\t\t\tif(error !== null){\r\n\t\t\t\t\tconsole.log(error+\"\\r\\n\");\r\n\t\t\t\t}\r\n\t\t\t});\t\r\n\t\t}\r\n\t}\r\n}\r\nws.random_price = function(){\r\n\treturn Math.floor(Math.random()* Math.random() * 50000);\r\n};\r\nws.random_market = function(){\r\n\treturn 'MKT/BTC'+Math.floor(Math.random()*50);\r\n};\r\n\r\nws.mkt_test_msg = function(){\r\n\tvar msg = {'event':'mkt_price','price':ws.random_price(),'pair':ws.random_market()};\r\n\treturn msg || {};\r\n}\r\nconsole.log(JSON.stringify(ws.mkt_test_msg())); //outputs expected json object\r\nsetInterval(function(){\r\n\tif(ws.clients.length > 0){\r\n\t\tws.broadcast(JSON.stringify(ws.mkt_test_msg()));//outputs undefined!\r\n\t}\r\n},50);\r\n```"},"comment":{"url":"https://api.github.com/repos/einaros/ws/issues/comments/68571844","html_url":"https://github.com/einaros/ws/issues/426#issuecomment-68571844","issue_url":"https://api.github.com/repos/einaros/ws/issues/426","id":68571844,"user":{"login":"3rd-Eden","id":28071,"avatar_url":"https://avatars.githubusercontent.com/u/28071?v=3","gravatar_id":"","url":"https://api.github.com/users/3rd-Eden","html_url":"https://github.com/3rd-Eden","followers_url":"https://api.github.com/users/3rd-Eden/followers","following_url":"https://api.github.com/users/3rd-Eden/following{/other_user}","gists_url":"https://api.github.com/users/3rd-Eden/gists{/gist_id}","starred_url":"https://api.github.com/users/3rd-Eden/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/3rd-Eden/subscriptions","organizations_url":"https://api.github.com/users/3rd-Eden/orgs","repos_url":"https://api.github.com/users/3rd-Eden/repos","events_url":"https://api.github.com/users/3rd-Eden/events{/privacy}","received_events_url":"https://api.github.com/users/3rd-Eden/received_events","type":"User","site_admin":false},"created_at":"2015-01-02T23:13:37Z","updated_at":"2015-01-02T23:13:37Z","body":"@r3wt I'm not the author of this library, i'm merely a simple contributor ;-). I haven't touched/read through the documentation and readme in ages. I just look at the code and see that clients is an array not an object: https://github.com/einaros/ws/blob/master/lib/WebSocketServer.js#L86\r\n\r\nAs for the undefined it makes sense as you check if error is not `null` in the send callback. That's true, because it's `undefined` as there is no error, causing you to log `undefined`"}}