0

hi i want to store userID,userName and socketId in to users array using object like this:

var userInfo = new Object();
            userInfo.userName = user.userName;
            userInfo.userId = user.userId;
            userInfo.socketId = socket.id;
            users.push(userInfo);

i created this object using this example link.

Now i want to display this object values (userName, userId,socketId) how can i display this values in to client side?

i dont have any idea please help me

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var users = [];
var roomName;
app.get('/', function(req, res){
    res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
    console.log('a user connected');

    socket.on('create', function (channel) {
        socket.join(channel);
        roomName=channel;
        socket.channel=channel;
        console.log('room', roomName);
    });

    socket.on('addUser', function (user) {
        var userInfo = new Object();
        userInfo.userName = user.userName;
        userInfo.userId = user.userId;
        userInfo.socketId = socket.id;
        users.push(userInfo);
        updateClients();
    });

    socket.on('chat message', function(msg){

    });

    socket.on('disconnect', function(){

    });

    function updateClients() {
        io.to(socket.channel).emit('update', users);
        console.log('users list is ', users);
    }
});

http.listen(3000, function(){
    console.log('listening on *:3000');
});
Community
  • 1
  • 1
yathavan
  • 183
  • 1
  • 14

1 Answers1

3

I tried to understand your requirement after that i would like to to share snippets.

After getting 'addUser' trigger you pushed userinfo into an array at same time you need to emit for client.

//server.js

    socket.on('addUser', function (user) {
            var userInfo = new Object();
            userInfo.userName = user.userName;
            userInfo.userId = user.userId;
            userInfo.socketId = socket.id;
            users.push(userInfo);
            updateClients(users);


        });

       function updateClients(usersInfo) {
          //io.emit('update', usersInfo);
          io.to(socket.channel).emit('update', usersInfo);
          console.log('users list is ', users);
        }

//client

 var socket = io();    // in client.html
 var socket = require("socket.io")(http);   // in client.js
  socket.on('update',function(usersInfo){

          usersInfo.forEach(function(single_user){
            //now you can use userinfo object according to your requirement    
          });

          });

Your emittted data of user triggered .on of client. so you need to understand .on and .emit of socket.io

Dineshaws
  • 2,065
  • 16
  • 26
  • thankyou for your replay dineshaws, can i emit user info in **updateClients()** function. – yathavan Apr 08 '15 at 11:42
  • you can pass as an argument in updateClients() after that emit in function – Dineshaws Apr 08 '15 at 11:54
  • i create this code for display online clients, so need emit in updateClients(), data store correct but i unable to display. help me, data like this : [ { userName: 'cat', userId: '16', socketId: 'CXZNyOz2ZLMdHCBkAAAC' }, { userName: 'dog', userId: '16', socketId: 'CXZNyOz2ZLMdHCBkAAAC' } ] – yathavan Apr 08 '15 at 17:31