{"action":"created","issue":{"url":"https://api.github.com/repos/websockets/ws/issues/800","repository_url":"https://api.github.com/repos/websockets/ws","labels_url":"https://api.github.com/repos/websockets/ws/issues/800/labels{/name}","comments_url":"https://api.github.com/repos/websockets/ws/issues/800/comments","events_url":"https://api.github.com/repos/websockets/ws/issues/800/events","html_url":"https://github.com/websockets/ws/issues/800","id":168927895,"number":800,"title":"Cannot access wss.clients array","user":{"login":"SonnyRajagopalan","id":1585670,"avatar_url":"https://avatars.githubusercontent.com/u/1585670?v=3","gravatar_id":"","url":"https://api.github.com/users/SonnyRajagopalan","html_url":"https://github.com/SonnyRajagopalan","followers_url":"https://api.github.com/users/SonnyRajagopalan/followers","following_url":"https://api.github.com/users/SonnyRajagopalan/following{/other_user}","gists_url":"https://api.github.com/users/SonnyRajagopalan/gists{/gist_id}","starred_url":"https://api.github.com/users/SonnyRajagopalan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SonnyRajagopalan/subscriptions","organizations_url":"https://api.github.com/users/SonnyRajagopalan/orgs","repos_url":"https://api.github.com/users/SonnyRajagopalan/repos","events_url":"https://api.github.com/users/SonnyRajagopalan/events{/privacy}","received_events_url":"https://api.github.com/users/SonnyRajagopalan/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2016-08-02T16:19:13Z","updated_at":"2016-08-02T22:23:25Z","closed_at":"2016-08-02T21:14:01Z","body":"I have a nodejs v6.3.1 class for `ws` objects that looks like this (`WSS.js`):\r\n\r\n    var EventEmitter = require ('events');\r\n    var WebSocketServer = require('ws').Server;\r\n\r\n    function WSS () {\r\n      this.wss = new WebSocketServer ({port: 8088});\r\n      this.cee = new EventEmitter ();\r\n      this.wss.on ('connection', function (conn) {\r\n        console.log ('New connection');\r\n        //conn.send ('tickles');\r\n        conn.on ('message', function (message) {\r\n          console.log ('received: %s', message);\r\n        });\r\n      });\r\n      this.cee.on('sendEvent', function (message) {\r\n        for (var i=0, l = this.wss.clients.length; i < l; ++i) {\r\n          this.wss.clients[i].send (message);\r\n        }\r\n        //this.wss.clients.forEach (function each (client) {\r\n          //client.send (message);\r\n        //});\r\n      });\r\n    }\r\n    WSS.prototype.sendRequest = function send (message) {\r\n      this.cee.emit ('sendEvent', message);\r\n    }\r\n    module.exports = WSS;\r\n\r\n\r\nI use the module in my code in the following manner (`main.js`):\r\n\r\n    var WSS = require ('./WSS.js');\r\n    var http = require ('http');\r\n\r\n    process.on('uncaughtException', function (err) {\r\n      console.error(err);\r\n      console.log(\"Node NOT Exiting...\");\r\n    });\r\n\r\n    var wsserver = new WSS ();\r\n\r\n    http.createServer (function handleRequest (req, res) {\r\n      res.end('It Works!! Path Hit: ' + req.url + '\\n');\r\n        wsserver.sendRequest ('Hey, a new HTTP request');\r\n    }).listen (8080);\r\n\r\nI run the server like so:\r\n\r\n    $ node main.js\r\n\r\nI connect to the websocket server like so:\r\n\r\n    $ wscat -c http://localhost:8088/\r\n\r\nWhich works, but I am not able to broadcast (`Hey, a new HTTP request`) when I send a `HTTP GET` to `/` using `curl` like so:\r\n\r\n    $ curl localhost:8080\r\n\r\nThe `curl` itself works, but it does not trigger a broadcast for the following error:\r\n\r\n    TypeError: Cannot read property 'clients' of undefined\r\n        at EventEmitter.<anonymous> (/full/path/WSS.js:15:31)\r\n        at emitOne (events.js:96:13)\r\n        at EventEmitter.emit (events.js:188:7)\r\n        at WSS.send [as sendRequest] (/full/path/WSS.js:24:12)\r\n        at Server.handleRequest (/full/path/main.js:13:14)\r\n        at emitTwo (events.js:106:13)\r\n        at Server.emit (events.js:191:7)\r\n        at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:543:12)\r\n        at HTTPParser.parserOnHeadersComplete (_http_common.js:105:23)\r\n\r\nI am surprised because per the documentation, `this.wss.clients` contains all the `client` objects. Any insight is appreciated. Thanks!"},"comment":{"url":"https://api.github.com/repos/websockets/ws/issues/comments/237065144","html_url":"https://github.com/websockets/ws/issues/800#issuecomment-237065144","issue_url":"https://api.github.com/repos/websockets/ws/issues/800","id":237065144,"user":{"login":"ericmdantas","id":7040162,"avatar_url":"https://avatars.githubusercontent.com/u/7040162?v=3","gravatar_id":"","url":"https://api.github.com/users/ericmdantas","html_url":"https://github.com/ericmdantas","followers_url":"https://api.github.com/users/ericmdantas/followers","following_url":"https://api.github.com/users/ericmdantas/following{/other_user}","gists_url":"https://api.github.com/users/ericmdantas/gists{/gist_id}","starred_url":"https://api.github.com/users/ericmdantas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ericmdantas/subscriptions","organizations_url":"https://api.github.com/users/ericmdantas/orgs","repos_url":"https://api.github.com/users/ericmdantas/repos","events_url":"https://api.github.com/users/ericmdantas/events{/privacy}","received_events_url":"https://api.github.com/users/ericmdantas/received_events","type":"User","site_admin":false},"created_at":"2016-08-02T22:23:25Z","updated_at":"2016-08-02T22:23:25Z","body":"`bind` is all about javascript, it's not restrict to node. I suggest you search stuff like `arrow functions`, `bind vs call`, etc."}}
{"actor":{"display_login":"ericmdantas"}}