1

a user joins a room and emits its username to the room using passport.socketio . With the help of socket.request.user.user_id i am able to get the user_id of the socket and fetch the username corresponding to user_id from the database.

 function onNewplayer_GK (data) {
var room_id = find_Roomid_GK(); 
var room = room_List_GK[room_id]; 
//join the room
this.room_id = room_id;
this.join(this.room_id);
console.log("created new room with id " + room_id);
console.log("created new player with id " + this.id);
 var user_id = this.request.user.user_id;
db.collection('user-details').findOne({_id:ObjectId(user_id)},(err,result)=>{
    var results = JSON.stringify(result);
    var results = JSON.parse(results);
    console.log('name is'+ (results.user_name));
    var name = results.user_name;
    this.emit('myname',{
        my_name:name
    });
    this.broadcast.to(room_id).emit('enemyname',{
        enemy_name:name
    });
});

 io.on('connection',(socket)=>{
console.log('user connected ' + socket.id);
// listen for new player
// socket.on("new_player", onNewplayer);
socket.on('new_player_history',onNewplayer_history);

socket.on('new_player_GK',onNewplayer_GK);

find_Roomid_GK() is a function which looks for rooms which are not filled and if filled it will create a room and assigns it to room_id.There is no problem in the functionality of the rooms. I just want to emit both the user's usernames to each other. how?? I am unable to emit the first user connected username to the second user connected after the first.

But when another user connects and joins the same room, it is able to emit its details to the connected sockets. But this socket is not able to get the details of the sockets connected before this socket which is reasonable enough to understand.

Does anyone have an idea on how this newly connected socket will fetch the username of the connected sockets using passport.socketio ?

roh_dev
  • 275
  • 2
  • 6
  • 17

0 Answers0