const Promise = require('bluebird') const WebSocket = require('ws') const nbr = 100 const now = () => { return new Date().getTime() } function Client(id) { this.ws = null this.start = function() { const that = this this.ws = new WebSocket('ws://localhost:8080') this.ws.on('open', function() { console.log(`client ${id} connected`) that.send() that.ws.on('message', function(data) { console.log('received from server: ', data) setTimeout(function() { that.send() }, Math.random()*1000) }) }) } this.send = function() { const data = { move: { x: Math.random(), y: Math.random() }, start: now() } this.ws.send(JSON.stringify(data)) } } const ids = Array.from(new Array(nbr), (x,i) => i) Promise.map(ids, (id) => { const c = new Client(id) c.start() return Promise.resolve() }).then(() => { console.log('all up') })