last day (14 days later) » 

5:00 PM
1
A: How to get all the sockets connected to Socket.io

NijeeshIn latest version of socket.io (1.4.5) you'l have to do Object.keys(io.sockets.sockets); This returns an array containing id's of sockets connected and you can apply forEach on it. Object.keys(io.sockets.sockets).forEach(function(id) { console.log("ID:",id) // socketId }) Ok now for th...

 
Thanks but it gives me TypeError: list.forEach is not a function i can copy the full error if is needed.
 
can you add the list object you are getting? Maybe its got something to do with the socket,io version you're using. For me "io.sockets.sockets" returns an array of objects. So list.forEach works well and i'm getting the socketId's. Btw i checked this in socket.io version 1.3.5
Turns out i was right. It was a version problem. I have modified my answer
 
wow thanks man! so final code will be like this : var list = io.sockets.sockets; console.log("Connected sockets:") Object.keys(io.sockets.sockets).forEach(function(id) { console.log("ID:",id) // socketId plseas edit your question and only reamin the correct part so i can choose yours as the right question.
btw the B part of question is unanswered
 
I'l look into the B part now :)
I have added the answer for the B part of question.
 
Thanks i replaced the "ROOM_NAME" with one of my rooms but like this "LOTR" but it gives me 'TypeError: Cannot read property 'sockets' of undefined' should i pass the room as string? Thanks
 
5:00 PM
I've checked this code and it works. If you get that error it means there is no room "LOTR". Try : console.log(io.sockets.adapter.rooms). This shows all rooms.. See if the result contains your room.
And yes the room name should be a string: ["LOTR"]
 
It prints [object Object] as room name.would you like to enable the chat?
 
Hey i would suggest emitting the value to client side and consoling it there instead of consoling it in server. That way even if results are objects you can open and view it
 
5:17 PM
Hey man
 
hey
 
so when i run this code 'if(io.sockets.adapter.rooms) {
console.log(io.sockets.adapter.rooms);
} else {
console.log('NO ROOMS');
}'
I get this output '
ID: /#AFZFXNy7r1hERSOzAAAA
{ '/#AFZFXNy7r1hERSOzAAAA': Room { sockets: { '/#AFZFXNy7r1hERSOzAAAA': true }, length: 1 } }
the ID part is for another block of code
room name is actually LOTR
 
I think the room is not being created then
wait a sec. i"ll show you how its supposed to be
 
but i can show message to only users that are in the same room
 
{
'/#ZFmODqalwTfHZ3riAAAA': { sockets: { '/#ZFmODqalwTfHZ3riAAAA': true }, length: 1 },
NewRoom:{ sockets:
{ '/#ZFmODqalwTfHZ3riAAAA': true,
'/#S2vHpKp9amyk03PBAAAC': true },
length: 2 },
'/#S2vHpKp9amyk03PBAAAC': { sockets: { '/#S2vHpKp9amyk03PBAAAC': true }, length: 1 }
}
See the second object NewRoom. Thats a room i created
 
5:31 PM
hmm what did you used? because the whole code is by this course udemy.com/the-complete-node-js-developer-course and i'm sure it's right.
 
What version of socket.io are you using?
 
"socket.io": "^1.3.7"
 
ok. Try upgrading to the latest version. 1.4.5. Probably structure of io object is different in 1.3.7
 
npm-check-updates ?
 
That would work but better change only socketio. Coz its possible changing all libraries might give you new issues in some other code you did.
npm update socket.io
 
5:44 PM
it doesn't update the socket.io
never mind man you helped a lot.i will check it later
bat i would be more than happy to add you on something like slack
this is our site
 
sure man :)
I'l check it out
 
what's your id there? :)
 
I was just checking out ur site. wats it about
 
We do node.js projects with hapi.i we can give you some projects to do if you like.i'm more on business part of the company but trying to learn node
btw i did updated the socket but still getting looks like it worked ^_^ ID: /#kcOBS4wC084T9Aj6AAAC
{ '/#HidElM7S0Cjk2R9cAAAB': Room { sockets: { '/#HidElM7S0Cjk2R9cAAAB': true }, length: 1 },
LOTR: Room { sockets: { '/#HidElM7S0Cjk2R9cAAAB': true }, length: 1 },
'/#kcOBS4wC084T9Aj6AAAC': Room { sockets: { '/#kcOBS4wC084T9Aj6AAAC': true }, length: 1 } }
thank you soooo much <3
 
6:06 PM
Thats all right. Just happy to help :) I'm already working somewhere n kinda have my hands full right now on a project :) But i'd like to do some small projects if you have. Please contact me at 'k.nijeesh91@gmail.com'. And feel free to ask for any help you need. I'll be happy to help however i can. :)
 
6:17 PM
That's awesome man.don't forget your id on slack.my email is mojtabaghavidel@gmail.com so you know it's me when i contact
 

last day (14 days later) »