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.
users.putshould probably beusers.push, and I guess you are callingconsole.logsynchronously, before the'join'event has taken place.users.put(userNickname). There is no problem withconsole.log. TheuserNicknameis shown in the console. But when I try to put thisuserNicknamein 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.Arrayhas no methodputunless 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?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.