TomSawyer TomSawyer -4 years ago 146
Node.js Question

How rooms work on socket.io?

I've read socket.io document but its poor.
I have few questions like this:


  1. How can i define property for one room like:
    room.isPlaying
    ,
    room.createdDate
    ...

  2. Where the rooms data store? In the memory? Will
    memory be auto release after noone left on that room? Because if there are too much empty room created will consume server memory

  3. Now, i'm using object to store room data and handle the empty room:
    delete when no-one left. Does it the same with using default
    socket.io
    room?


Answer Source

How can i define property for one room like: room.isPlaying, room.createdDate

socket.io does not offer you access to a publicly available object for each room. You can create your own object for each room and keep them in your own Map object. So, whenever you add someone to a room, you see if you already have a room object for them in your Map and, if not, you create one. Then you can keep room state on that object.

Where the rooms data store? In the memory? Will memory be auto release after noone left on that room? Because if there are too much empty room created will consume server memory

socket.io has it's own state information that keeps track of what connections are in a given room. It does not document that state, but you can study the source code or study state in a debugger and find where everything is store.

For example:

io.nsps['/'].adapter.rooms

is an object whose properties are a list of all active rooms in the / namespace. Or, you can use this to get an array of all active rooms:

Object.keys(io.nsps['/'].adapter.rooms)

Will memory be auto release after noone left on that room? Because if there are too much empty room created will consume server memory

Yes, memory will be released when a room is empty.

Now, i'm using object to store room data and handle the empty room: delete when no-one left. Does it the same with using default socket.io room?

socket.io keeps the data in memory. It will clean up when a room becomes empty.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download