0

I am using socket.io and am looking for a way for the client to be able to specify which room it would like to connect to, but I can't figure out a good way to make this happen.

On the server I have:

io.sockets.on 'connection', (socket)->
    console.log 'A socket connected.'
    socket.join('my_room')

On the client I have:

io = io.connect("http://domain.com")

What I would like to do is get some initial state in the first connection on the client so that the server knows which room it should try and connect the person to.

So something like this on the client:

io = io.connect("http://domain.com", room_1)

Then the server would have some thing like this:

io.sockets.on 'connection', (socket, data)->
    console.log 'A socket with connected!'
    socket.join(data)

Is this possible to do with room? Is there a better way? I looked into namespaces but rooms more closely fit what I am trying to do.

Alexis
  • 23,545
  • 19
  • 104
  • 143
  • 2
    IMHO using socket.io you don't need to be savvy on your messages since you are using one already established connection... so you could just send a message when your connection is established. – drinchev Jan 11 '13 at 07:17

1 Answers1

4

I agree with drinchev. You should just send the data on the connect event on the client side.

io = io.connect 'http://domain.com'

io.on 'connect', () ->
    socket.emit('join-room', room_1)

Another option is to use socket.io namespaces. This will allow you to avoid the round trip, but there are some differences between namespaces and channels and you will have to determine which makes more sense for your app.

io = io.connect 'http://domain.com/room_1'

The accepted answer here has a good rundown of the differences between channels and namespaces. socket.io rooms or namespacing?

Community
  • 1
  • 1
Timothy Strimple
  • 22,920
  • 6
  • 69
  • 76