3

Im trying to create a node websocket for messaging and broadcasting using openshift. Below is my code.

var WebSocketServer = require('ws').Server;
var http = require('http');
var ipaddr  = opneshift_ip;
var port    = openshift_port;
var server = http.createServer();
var wss = new WebSocketServer({server: server, path: '/connectserv'});

wss.broadcast = function(data) {
    for(var i in this.clients) {
        console.log(this.clients[i]);
        this.clients[i].send(data);
    }
};

wss.on('connection', function(ws) {
    console.log('a client connected'); 
    ws.on('message', function(data) {
        console.log('>>> ' + data); 
        ws.send('got '+data);
        if (data == 'broadcst') { 
            console.log('broadcst'); 
            wss.broadcast('Hi All'); 
        }
    });
    ws.on('close', function() {
      console.log('Connection closed!');
    });
    ws.on('error', function(e) {
      console.log(e);
    });
});

console.log('Listening at IP ' + ipaddr +' on port '+port);
server.listen(port,ipaddr);    

When any client connects, console writes "a client connected". When any client sends message, console writes ">>> message" and im getting the same at client as well ("got message")

But when multiple clients are connected, if i want to broadcast a message to all connected clients, i send "broadcst" as message. Than goes into

    if (data == 'broadcst') { 
        console.log('broadcst'); 
        wss.broadcast('Hi All'); 
    }

But only the client which sends get the message.

How to make all clients to get the message? Does each client creates separate session? How to use redis here?

Any quick help appreciated.

Thanks.

  • You're on the right track. You have to have a list that lives outside your `wss.on('connection'...)` that keeps all the client addresses. – sova Jan 11 '17 at 18:19
  • If i try to list it show only one all the time. New session is getting created for every socket connect. How to avoid that? – Singaramani Thangavel Jan 12 '17 at 13:15
  • 1
    Check out the source code for my project haiku.run. https://github.com/sova/haiku.run/blob/master/src/index.js If you look at the clients.push(socket) line at 57 it may help. There's a list of clients (called clients) and everytime someone connects I do clients.push(socket) to add their socket to the array. My clients array lives in the global scope, not in the connection/sessions scope. That's vital. – sova Jan 13 '17 at 02:45

1 Answers1

0

Try

wss.broadcast = function(data) {
  for(var i in wss.clients) {
    console.log(wss.clients[i]);
    wss.clients[i].send(data);
  }
};

broadcasting with wss

  • Tried: `wss.broadcast = function(data) { for(var i in wss.clients) { console.log(wss.clients.length); wss.clients[i].send(data); } };` But that is also not working. It sends 'Hi All' only to the client which sends 'broadcst'. The length of client array is always 1. It creats different session for each client it seems. – Singaramani Thangavel Jan 12 '17 at 03:02
  • 1
    Somehow we need to force the server to use the same connection/session instead of creating new when new client connects. – Singaramani Thangavel Jan 12 '17 at 03:36