{"action":"created","issue":{"url":"https://api.github.com/repos/websockets/ws/issues/567","labels_url":"https://api.github.com/repos/websockets/ws/issues/567/labels{/name}","comments_url":"https://api.github.com/repos/websockets/ws/issues/567/comments","events_url":"https://api.github.com/repos/websockets/ws/issues/567/events","html_url":"https://github.com/websockets/ws/issues/567","id":103956340,"number":567,"title":"Websocket Server with Clusters","user":{"login":"McFarts","id":13074002,"avatar_url":"https://avatars.githubusercontent.com/u/13074002?v=3","gravatar_id":"","url":"https://api.github.com/users/McFarts","html_url":"https://github.com/McFarts","followers_url":"https://api.github.com/users/McFarts/followers","following_url":"https://api.github.com/users/McFarts/following{/other_user}","gists_url":"https://api.github.com/users/McFarts/gists{/gist_id}","starred_url":"https://api.github.com/users/McFarts/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/McFarts/subscriptions","organizations_url":"https://api.github.com/users/McFarts/orgs","repos_url":"https://api.github.com/users/McFarts/repos","events_url":"https://api.github.com/users/McFarts/events{/privacy}","received_events_url":"https://api.github.com/users/McFarts/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-08-30T22:07:20Z","updated_at":"2016-01-27T17:35:04Z","closed_at":"2015-09-03T16:28:01Z","body":"For example:\r\n\r\n```\r\nvar cluster = require('cluster');\r\n\r\n \r\nif (cluster.isMaster) {\r\n  var numCPUs = require('os').cpus().length;\r\n  for (var i = 0; i < numCPUs; i++) {\r\n    cluster.fork();\r\n  }\r\n \r\n  Object.keys(cluster.workers).forEach(function(id) {\r\n    console.log(cluster.workers[id].process.pid);\r\n  });\r\n}else{\r\n var WebSocketServer = require('ws').Server, wss = new WebSocketServer({ port: 9300, maximumPayload: 200, speedLimit: 5  }); // speedLimit is in miliseconds 5ms // Payload is in bytes ~ 1048576 = 1 MB. \r\n //Rest of my gamecode logic here ~\r\n\r\n}\r\n```\r\nIs this how you would set this up?\r\n\r\nWould the gamecode in the else statement have an increase of performance? (Websocket server, etc)? Because it loads up 6 nodejs processes but... why does it only work in an else statement?"},"comment":{"url":"https://api.github.com/repos/websockets/ws/issues/comments/175760226","html_url":"https://github.com/websockets/ws/issues/567#issuecomment-175760226","issue_url":"https://api.github.com/repos/websockets/ws/issues/567","id":175760226,"user":{"login":"mems","id":729275,"avatar_url":"https://avatars.githubusercontent.com/u/729275?v=3","gravatar_id":"","url":"https://api.github.com/users/mems","html_url":"https://github.com/mems","followers_url":"https://api.github.com/users/mems/followers","following_url":"https://api.github.com/users/mems/following{/other_user}","gists_url":"https://api.github.com/users/mems/gists{/gist_id}","starred_url":"https://api.github.com/users/mems/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mems/subscriptions","organizations_url":"https://api.github.com/users/mems/orgs","repos_url":"https://api.github.com/users/mems/repos","events_url":"https://api.github.com/users/mems/events{/privacy}","received_events_url":"https://api.github.com/users/mems/received_events","type":"User","site_admin":false},"created_at":"2016-01-27T17:35:04Z","updated_at":"2016-01-27T17:35:04Z","body":"Just for everybody else, what I use:\r\n\r\n`server.js`:\r\n```\r\n\"use strict\";\r\nconst cluster = require(\"cluster\");\r\nconst numCPUs = require('os').cpus().length;\r\n\r\nif(cluster.isMaster){\r\n\tfor (let i = 0; i < numCPUs; i++) {\r\n\t\tlet worker = cluster.fork();\r\n\t\tconsole.log(`Worker ${worker.id} created`);\r\n\t}\r\n}else{\r\n\t// WebSocket server\r\n\tlet wsConfig = {\r\n\t\thost: \"127.0.0.1\",//default \"0.0.0.0\"\r\n\t\tport: process.env.PORT || 9000\r\n\t};\r\n\tconst GameServer = require(\"./GameServer.js\");\r\n\tlet srv = new GameServer(wsConfig);\r\n}\r\n```\r\n`GameServer.js`:\r\n```\r\n\"use strict\";\r\nconst WebSocketServer = require(\"ws\").Server;\r\nmodule.exports = class GameServer{\r\n\tconstructor(wsConfig, dbConfig, serverCount/* = 1*/){\r\n\t\tthis._wsServer = new WebSocketServer(wsConfig);\r\n\t\tconsole.log(`WebSocket server instance listen ${wsConfig.host || \"0.0.0.0\"}:${wsConfig.port}`);\r\n\t\tthis._wsServer.on(\"connection\", this._handleWSConnection.bind(this));\r\n\t}\r\n\t_handleWSConnection(client){\r\n\t\tconsole.log(`New client from: ${client.upgradeReq.headers[\"x-forwarded-for\"] || client.upgradeReq.connection.remoteAddress}`);\r\n\t}\r\n}\r\n```"}}