jdne
jdne

Reputation: 23

SocketIO NodeJS Gamemode Type emit

I'm new to socketIO and my site is going to have 2 game modes, my plan was to make 2 scripts for each game mode, however thinking about it I would prefer to have one script do it all. I currently have it setup so the script emits to everyone connected. Is there a way to emit to users that are in a specific array or something...

I currently send data like this

io.emit("updateTimeLeft", runTime);

The thing that I'm confused about is, I'm doing theses on events in my script that are broadcasting to everyone. How would I get the specific sockets to send to...

Could I save the users socket in an array and then just list throw them and send the data I need? I don't know if this would work though. Any suggestions?

Upvotes: 0

Views: 36

Answers (1)

Guy S
Guy S

Reputation: 620

Sam is right. you should use rooms (which is a built-in solution in socket.io) for each 'group'.

Example code:

  socket.join('myroom');

From now on, you can emit events to all sockets that are in 'myroom' this way:

io.to('myroom').emit('some event'):

Upvotes: 1

Related Questions