Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

send message to a specific connected client #367

Closed
pacmac opened this issue Oct 4, 2014 · 5 comments
Closed

send message to a specific connected client #367

pacmac opened this issue Oct 4, 2014 · 5 comments

Comments

@pacmac
Copy link

pacmac commented Oct 4, 2014

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

@fecaleagle
Copy link

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.

@lpinca lpinca closed this as completed Nov 8, 2016
@bryku
Copy link

bryku commented Aug 13, 2019

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?

@WiktorLigeza
Copy link

WiktorLigeza commented Jun 7, 2021

Hi, i have just used this method but lookup[i].send(msg) isn't sending anything in this configuration, it does work when i broadcast to all, like so:
wsServer.clients.forEach(function (client) { client.send(msg); });
, or when i send raw msg just after i establish connection like:
wsServer.on('connection', function (socket) { socket.send("Hello!") ...

basically what the following code does is: Client with tag connects to a server and is being remembered, client without tag just sends information with a specific destination which is being represented by that tag, so the role of the server is just to forward this info to a specific client

`

    var id = 0;
    var lookup = {};
    var clients_tag = [];
    
    Object.size = function(obj) {
      var size = 0,
        key;
      for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
      }
      return size;
    };
    
    wsServer.on('connection', function (socket) {
        socket.on('message', function (msg) {
            if (msg.slice(0, 3) == "TAG"){
              socket.id = id++;
              lookup[socket.id] = socket;
              clients_tag[socket.id] = msg.slice(3);
              console.log(msg.slice(3) + " - just connected, with id: "+ socket.id);
              
            }
           else{
             const msg_obj = JSON.parse(msg);
             console.log("from "+  msg_obj.user + " to " + msg_obj.device_tag + " || color value: " +  msg_obj.color);
             
             for (var i = 0; i < Object.size(lookup); i++){ 
                 if(clients_tag[i] == msg_obj.device_tag){
                   console.log("sending to client with id: ", lookup[i].id);
                   lookup[i].send(msg);
                 }
             }
           }
            
            // wsServer.clients.forEach(function (client) {
            //     client.send(msg);
            // });
        });
    });

`

output after connecting 3 clients, 2 are listening 1 is sending - and it works fine but nothing is being sent to a specific client:
2137 - just connected, with id: 0
2134 - just connected, with id: 1
from John to 2137 || color value: #b18181
sending to a client with id: 0

@netizen-ais
Copy link

@WiktorLigeza I think you're mixing id/tag. This is not an issue in this library, but your code. Try SO or google for help

@webcoderph
Copy link

webcoderph commented Feb 19, 2022

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.
@fecaleagle
how would you manage that array if the socket is disconnected? I mean, what is the best way to handle that kind of situation? Thanks

andruwik777 pushed a commit to andruwik777/tic-tac-toe-websocket-server that referenced this issue Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants