'send message to one user using Socket.io

I'm looking for a way to send messages to one specific client with Node.js, socket.io, Redis. ( which also means to keep track of that user)

your help, please

var app = require('express')();

var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('redis');
var user=[];

server.listen(3000);
io.on('connection', function (socket) {

    var redisClient = redis.createClient();
    redisClient.subscribe('message');

    user[socket.handshake.query.userid] = socket;

    socket.on('create', function(id) {
        user[id] = socket;
    });

  
    redisClient.on("message", function(channel, message) {
        var message=JSON.parse(message);
        var id=JSON.parse(message.id);
        var target=id;
             
        console.log("target id => "+user[target].id+" / socket.id => "+socket.id)
        
         try{
             if(user[target].id==socket.id){
                  user[target].emit(channel,message.msg);
              }else{
                  console.log('when socket.id dont match')
               }
         }catch (e){
             console.log(e);
         }
      console.log("\n#########################\n");
         
    });

  

});


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source