Ws: send message to a specific connected client

Created on 4 Oct 2014  路  3Comments  路  Source: websockets/ws

Is there any way of initiating a message to a connected client as opposed to replying to a message from a client ?

Most helpful comment

If you keep track of clients on connect, it's very easily done. This is more or less a duplicate of: https://github.com/einaros/ws/issues/81

I took a similar approach to @michael and did something like this on connect, dirtying the ws object with an id...

var id = 0;
var lookup = {};

wss.on('connection', function(ws) {
  ws.id = id++;
  lookup[ws.id] = ws;

  [...]
}

if you want to send a message to the client with id == 0, you'd just call...

lookup[0].send('message');

There's probably a better way to do this, but it's working fine for me.

All 3 comments

If you keep track of clients on connect, it's very easily done. This is more or less a duplicate of: https://github.com/einaros/ws/issues/81

I took a similar approach to @michael and did something like this on connect, dirtying the ws object with an id...

var id = 0;
var lookup = {};

wss.on('connection', function(ws) {
  ws.id = id++;
  lookup[ws.id] = ws;

  [...]
}

if you want to send a message to the client with id == 0, you'd just call...

lookup[0].send('message');

There's probably a better way to do this, but it's working fine for me.

It seems over my research, I have found ZERO help on this subject so... I came up with what I think is the best solution by far. People almost always seem to forget the basics of js :/ it's times like this that one of the most basic functions of this family of compiled languages comes to the rescue...
The for loop!

var Server = require('ws').Server;
const portTCP = 8080;
var ws = new Server({port: portTCP});

var socket = [];
ws.on('connection', function(w){
  w.on('message', function(msg){
    var message = msg;
    for(i = 0; i < socket.length; i++) {
      socket[i].send(message);
    }
  });

  socket.push(w);

I kept the array "socket" outside of the actual connection so that we may refrence certain connections from within another socket event.

To elaborate, the connection is what varies from client to client but the events are global. In order to target a client you need to be able to reach it from another clients event. In this case I used a for loop to iterate through all of the open connections to "port" in the array socket[]

If you notice I actually add each sockets individual connection to the array AFTER the event and OUTSIDE of it as well. Doing it like this is the only real way to handle this efficiently because you can access connections from ANYWHERE within your server code and therefore any client can communicate with any other client OR the server itself.

Any questions email me at [email protected]
;)

If you keep track of clients on connect, it's very easily done. This is more or less a duplicate of: #81

I took a similar approach to @michael and did something like this on connect, dirtying the ws object with an id...

var id = 0;
var lookup = {};

wss.on('connection', function(ws) {
  ws.id = id++;
  lookup[ws.id] = ws;

  [...]
}

if you want to send a message to the client with id == 0, you'd just call...

lookup[0].send('message');

There's probably a better way to do this, but it's working fine for me.

Can you also send something to everyone without looping through the lookup?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brainkim picture brainkim  路  4Comments

ORESoftware picture ORESoftware  路  3Comments

ImBundle picture ImBundle  路  3Comments

sherikapotein picture sherikapotein  路  3Comments

NodePing picture NodePing  路  5Comments