Ask coding questions

← Back to all posts
Display all users in Socket.IO
ruiwenge2 (1107)

With Socket.IO, how can I make it so that once someone enters a room, it will display all the users currently in the room as well?

Answered by Coder100 (18882) [earned 5 cycles]
View Answer
Coder100 (18882)

that depends on language
either you can use the better node
or the bad python flask-socketio

the process is the same

socket.on connect -> ... append to array of users ... , socket.emit('users', ... array of users ...)

ruiwenge2 (1107)

@Coder100 I’m using node, is that for the client or backend?

Coder100 (18882)

frontend, you'll just do socket.on users and you'll get your data @ruiwenge2

Coder100 (18882)

by the way -- in append users to array -- just a reminder to maybe use an object instead --> that way you can also get socket id too @ruiwenge2

ruiwenge2 (1107)

@Coder100 wait I’m kind of confused, because this only shows the users that joined after the first user joined, but it doesn’t show all the users that already joined before

Coder100 (18882)

@ruiwenge2 uh
each time someone joins, you set a key to a value, right?

i made a mistake -- you were supposed to use io.emit instead of socket.emit sorry

ruiwenge2 (1107)

sorry for the pings.
@Coder100 and @candies, you guys might be able to help me