Dynamic Namespaces

5,854 views
Skip to first unread message

Travis

unread,
Jun 19, 2011, 8:05:37 AM6/19/11
to Socket.IO
It appears that with the 0.7 release it will be possible to namespace
sockets, as in the README example:

var chat = io
.of('/chat');
.on('connection', function (socket) {...});

var news = io
.of('/news');
.on('connection', function (socket) {...});

In this case, however, the server expects preset namespaces. My
question is: whether it will be possible to dynamically create
namespaces, for instance, representing the primary key of some object
- an example would be creating one namespace per forum in a message
board app, or one namespace per project in a project management app.
I'm not sure if this is already possible, or how it would best work.
My instinct was to try something like the following before posting
here for help:

Client:

var namespace = 123; // would normally come from url, e.g. /
mysocketpage?id=123
var socket = io.connect('/'+namespace);

Server:

io.sockets.on('connection', function (socket) {
var namespace = socket.namespace.name;

io.of( namespace ).on('connection', function (socket) {...});
});

Guillermo Rauch

unread,
Jun 19, 2011, 12:15:00 PM6/19/11
to sock...@googlegroups.com
Although this is technically possible, for most people needs I created the concept of "rooms", which are (implementation wise) subchannels of the namespaces.

If you search for `.join` in the test/ subdirectory you'll find a few examples of this.
This is going to be documented pre-release.

--
Guillermo Rauch
LearnBoost CTO
http://devthought.com

Kenrick Beckett

unread,
Jul 14, 2011, 6:31:29 PM7/14/11
to sock...@googlegroups.com
Just where i wanna be :),

i was thinking about  doing the same in terms of rooms however i dont see much documentation on it, 
i wanted to ask is there a way to set the .join parameter dynamically based on what the socket post, like in:

 Client: 

var roomid = A65767S;
var socket = io.connect('/'+roomid); 


Server: 

io.sockets.on('connection', function (socket) { 
    socket.join(socket.roomid);
    
});

How do you send the room id from the client and how to catch it on the server


Gavin Gilmour

unread,
Jul 31, 2011, 1:45:48 PM7/31/11
to sock...@googlegroups.com
Don't suppose you ever got a solution to this Kenrick?

Similar situation myself, want to dynamically generate a namespace depending on a particular 'post' someone is in, but can't find any way to dynamically generate the id as you've said.

Cheers.

3rdEden

unread,
Jul 31, 2011, 2:35:56 PM7/31/11
to Socket.IO
Once we support querystrings inside the io.connect this would be easy.
The first bit of this is already landed in the `master` repo and
should be available
in the next release (https://github.com/LearnBoost/socket.io/commit/
a1797ccd4b2bf3d03cede66eb81ef30abe261429)

The client side part is stilling staging as a pull request:
https://github.com/LearnBoost/socket.io-client/pull/228
but hopefully that would land soon as well :)

On Jul 15, 12:31 am, Kenrick Beckett <kenrick.beck...@gmail.com>
wrote:

Gavin Gilmour

unread,
Jul 31, 2011, 2:54:44 PM7/31/11
to sock...@googlegroups.com
Ahhh, awesome.

Eagerly awaiting!

Cheers for the quick response.

Daniel Sell

unread,
Aug 1, 2011, 12:44:43 AM8/1/11
to sock...@googlegroups.com
Hi Gavin,

Regarding dynamic namespaces, I previously implemented it by having the client ask the server for a namespace to join.  I don't know if it will help you in your situation or not.  I've attached a test example.

-Dan Sell
client.js
server.js

Gavin Gilmour

unread,
Aug 1, 2011, 1:42:50 PM8/1/11
to sock...@googlegroups.com
Dan,

That helps loads and works perfectly! Much appreciated for posting it.

Thanks again,
Gavin.
Attachments:
- client.js
- server.js

Reply all
Reply to author
Forward
0 new messages