jemz
jemz

Reputation: 5143

How do I send message to specific client in node.js

How can I send message to specific client ?,is it possible that I can send only message to a specific client in node.js ?

var net = require('net');

var clients = [];

var server = net.createServer(function (socket) {

   clients.push(socket);

   var message = "message to client";


   //here how to send to a specific client ?   

});

Thank you in advance.

Upvotes: 0

Views: 691

Answers (1)

jfriend00
jfriend00

Reputation: 707976

It's not entirely clear exactly what you're trying to do, but you are already keeping a list of sockets in the clients array so you just need to apply whatever logic fits your need to select one of those sockets and then use .write() to write some data on that socket:

var net = require('net');
var clients = [];
var server = net.createServer(function (socket) {
   clients.push(socket);
   var message = "message to client";

   // pick a socket from the array using whatever logic you want and send to it
   clients[0].write(msg);
});

Upvotes: 2

Related Questions