nodejs-websocket icon indicating copy to clipboard operation
nodejs-websocket copied to clipboard

Server Messages are not broadcasting to all clients

Open sathishsanthosam opened this issue 7 years ago • 0 comments

I'm trying to create a websocket from Client side and send messages from server node-js. When ever i receive a message from client, it brodcast well. But if i send a message from server to all the clients, it is not brodcasting.

below is my code. var wsCallback = function(connection) { connection.nickname = null connection.on("text", function (str) { connection.nickname = str broadcasting(str); }) connection.on("close", function () { broadcasting(connection.nickname+" left") }) };

var server = ws.createServer(wsCallback) server.listen(8081,'localhost')

function broadcasting(str) { console.log(server.connections.length); server.connections.forEach(function (conn) { conn.sendText(str) }) }

when i call broadcasting from some other method, it is not broadcasting, it says the no of connection is 0.But if i send a message from one of the clients, then that message is broadcasting.

I don't know, what i'm missing. Looks like a referencing issue.

could you please help me solve this issue.

sathishsanthosam avatar Mar 27 '17 19:03 sathishsanthosam