0

I am working on a task to create a chat module which uses socket.io, node.js, java in Android Studio IDE.

I went through the basics and was able to create basic chat room to group chat with multiple users. But I want to make it user to user. For this, I have listed all the connected clients using their socket id's in node.js. But this isn't enough, to emit this list to the client side, I need a username for each connected socket id so that I can combine the socket.id with a respective username and put it in a list.

I am stuck in the middle where I am unable to figure out how to make a string coming from socket.on(event, function(string)) to be accessed globally. I have tried to push this string to an array and access it after the socket.on, but nothing happens.

Here's my code

index.js

const express = require('express'),
http = require('http'),
app = express(),
server = http.createServer(app),
io = require('socket.io').listen(server);
app.get('/', (req, res) => {

res.send('Chat Server is running on port 4040')
});

io.on('connection', (socket) => {

console.log('user connected');
console.log("The socket id is " + socket.id);

socket.on('join', function(userNickname) {
    console.log("User" + userNickname +" : has joined the chat ");
    io.sockets.emit('userjoinedthechat',userNickname +" : has joined the chat ");

    });


var req = io.sockets.connected;
var count = Object.keys(req).length;
console.log(socket);
console.log("Number of connected clients: " + count);
var list = new Array();
for(var i = 0; i < count; i++) {
    user = Object.keys(req)[i];
    list.push("User Id : " + user, "User Name : ");
}
console.log("Connected Clients Socket Id's: " + list);
io.sockets.emit('users', list);
});

What I was trying is,

//Declare a global variable or array
var user;
//(or)
var users = new Array();

socket.on('join', function(userNickname) {
    console.log("User" + userNickname +" : has joined the chat ");
    user = userNickname;
    //(or)
    users.put(userNickname);
    io.sockets.emit('userjoinedthechat', userNickname +" : has joined the chat ");
    });

And access it outside,

console.log(user);    //global variable
//or
console.log(users);   //Array list

Any help would be great. Thanks.

4
  • 1
    It's not possible to say for certain what doesn't work for you, we would need to see more code. That said, users.put should probably be users.push, and I guess you are calling console.log synchronously, before the 'join' event has taken place. Commented Jan 16, 2019 at 7:26
  • Sorry, my mistake. The correction is users.put(userNickname). There is no problem with console.log. The userNickname is shown in the console. But when I try to put this userNickname in the Array, the globally declared array remains unchanged, I mean empty. This is the code, this is it. The client side is android activity which is listening to this socket emit. If you still need the code, I will clean it up and update. Thanks in advance. Commented Jan 16, 2019 at 11:14
  • Are you sure? Array has no method put unless you've augmented it. But the matter with the code you've shared with us is that it's in pieces. Can you put it all together so that we can see the big picture? Commented Jan 16, 2019 at 12:09
  • Oh..! I am sorry, I got it wrong. It is users.push(userNickname). In the meantime, I am trying to dig up with some other alternative way of taking all the users and their socket id's. I am trying to make it work with some java code. Will try it and post my code for answers. Thank you, Sami, I will get back to you. Commented Jan 16, 2019 at 17:16

1 Answer 1

1

Something like this should work:

//Declare a global variable or array
var user;
var users = [];

socket.on('join', function(userNickname) {
    console.log("User" + userNickname +" : has joined the chat ");
    user = userNickname;
    users.push(userNickname);
    io.sockets.emit('userjoinedthechat', userNickname +" : has joined the chat ");
    console.log("User list: ", users);
});

When you save user it will be updated every time someone joins, so it's really the latest user who joined (you could change it to latestUser or something like that).

The users array will be available globally, however be careful about the order of your console.log statements. If you console.log just after the socket.on('join').. call, remember that you'll see an empty list initially.

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

2 Comments

Already tried. The socket.on function is not throwing the parameter value outside itself. The value is only available inside socket.on. When I console.log after the function, nothing happens. I am looking for an alternate way to achieve the result.
It's not that the socket.on function is failing to throw the parameter value outside of itself, it's just that the console.log statement is executed before the users array is updated!

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.