2

Im starting out with node.js and socket.io.

I have 2 questions: - When a room is empty, is it automatically destoyed unbtil recreated? and if it is not destroyed automaticaly, does it take up much ressources on the server?

  • on the server side. is it the io server or the connected socket that should transmit the data?

    socket.emit('doSomething'); or io.emit('doSomething');

lolplayer101
  • 2,112
  • 2
  • 17
  • 26

1 Answers1

4

The room is automatically removed from the array and the nodeJSs' V8 Garbage Collector finishes the job of completely removing the room from ram. You don't have to worry about any of that. Remember that all users are automatically put on a room on joining the server ( the socket.id named room ). io.emit should be used when you want to send a message from the server to anyone and socket.emit should be used when you want to send a message only to the sender. More information can be found on this answer: https://stackoverflow.com/a/40829919/7868639

itsundefined
  • 1,409
  • 2
  • 12
  • 32
  • Thanks for the clarification about the usage of rooms, very well explained! but you tell me to use only the io.emit on the server side but in the link you have "socket.to('game').emit('message', 'enjoy the game');" but when i did this on the client side it said its not a function... and on the server side this works fine. – lolplayer101 Jun 13 '17 at 15:39
  • The link I left shows all available ways of communicating from the server to the client. There are usages for both socket.something and io.something. None of this is wrong. On the client side you always use socket.emit – itsundefined Jun 13 '17 at 15:46