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');
});

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

thankyou for your replay dineshaws, can i emit user info in updateClients() function.
you can pass as an argument in updateClients() after that emit in function
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' } ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.