Node JS, Socket.IO Rooms and namespaces

Asked

Viewed 674 times

2

I’m developing a map-based android app, which will create chat rooms in some predetermined places and places where users need them.

I did some research with mechanisms that I could use in my application and came across the concept of namespaces and Rooms. From what I understand the Rooms are only open when you have at least one socket connected (correct ?)

What are the concepts, similarities and differences between Rooms and Namespaces?

  • Your question may run the risk of being closed because the answers may be based on opinions. So I suggest you change the question to "recommendation" para minha aplicaçao o que voces recomendam, for something like Quais os conceitos, semelhanças e diferenças entre Rooms e Namespaces. PS: I’m also curious about the answers, Nodejs is something I intend to master soon.

  • I would actually like to "hear" even the opinions , after all now that I have been able to use both, kind of I’m still in doubt about which to use ... both work ... however I believe that the Rooms will be more viable for me in specific.... but I’ll change thanks for the tip

  • @Kaduamaral as Voce was also interested in you know I found a very good explanation in the English OS https://stackoverflow.com/questions/10930286/socket-io-rooms-or-namespacing&#Xa

  • Show Osvaldo, I will translate and post here to help other people who need...

1 answer

2


This is what namespaces and Rooms has in common (socket.io v0.9.8 - note that v1.0 has been completely rewritten, so some things may have changed):

  • Both namespaces io.of('/nsp') and Rooms socket.join('room') are created on the server side
  • Multiple namespaces and multiple Rooms share the same connection (Websocket)
  • The server transmit messages only to customers connected to a nsp/room, i.e. the filter is not only on the client

As differences:

  • Namespaces are connected to the customer using io.connect(urlAndNsp) (the customer will be added to the namespace only if already existing on the server)
  • Rooms can only "log in" from the server side (Even creating a server-side API allows the client to enter directly)
  • Namespaces may have protected authorisation
  • Rooms does not have authorization support, but a custom authorization can be added, easy-to-create API on the server, in case someone is in multiples Rooms
  • Rooms are part of a namespace (global default namespace')
  • Namespaces are always allocated to the global scope

Not to confuse the concept with the name (room or namespace), I will use compartment to refer to the concept, and the other two names for the imlpementação of the concept. So:

  • If you need authorization per compartment, namespaces should be the easiest way to go
  • If you want to rank compartments in layers (maximum 2 layers), use a namespace/room combo
  • If your client-side app consists of different parts that (do not care about compartments, but) need to be separated from each other, use namespaces.

A last example would be a large client application where different modules, perhaps developed separately (e.g., third parties), each using socket.io independently, are being used in the same application and want to share a single network connection.

Not having really compared this, it seems to me if you just need simple compartments in your project to separate and group messages, anyone will be fine.

I’m not sure if that answers your question, but the research that led to that answer at least helped me to see clearer.


This answer is a free translation of Response by Eugene Beresovsky to a similar question in Stackoverflow

Browser other questions tagged

You are not signed in. Login or sign up in order to post.