On Wed, Jun 20, 2012 at 8:05 PM, Wahyudinata Setiawan
<
wahyu...@gmail.com> wrote:
> I know that sockjs supports multiplexing + prefix path (ex: '/echo') but how
> would you go about implementing dynamic rooms?
SockJS doesn't do multiplexing out of the box. The prefix ('/echo') is
an endpoint, and in theory you shouldn't open more than a single
SockJS connection per domain.
SockJS leaves multiplexing / rooms / channels up to the developer. That
raises a complexity when you're starting to write an app, but I believe in
long term grants you more flexibility.
I do have some thoughts on how channels should be implemented in
an ideal world, take a look at:
https://github.com/sockjs/websocket-multiplex
http://www.rabbitmq.com/blog/2012/02/23/how-to-compose-apps-using-websockets/
But treat it more like an inspiration rather than code ready to reuse.
You should really implement multiplexing yourself.
> the use case would be: User has a bunch of channels he can choose, upon
> clicking, he would join that channel.
>
> From what I have gathered so far, multiplexing channel names are hardcoded,
> the bob,ann example.
>
> Perhaps a custom solution on the user part? Such as listen on /chat prefix,
> and after connection:
>
> send a command sockjs.send({type:'command', 'user_join', 'channel_1'});
> server parses the command with maybe an if statement or an object =>
> function
> do a registerChannel(channel_name) , in this case would be "channel_1" or
> get from an existing channel
> at this point, client could then do multiplexer.channel(channel_name)
>
> Is this the way to go about it? Or is there a more elegant way of doing?
Whatever works for you :) SockJS is a transport layer - like native
WebSockets or TCP.
> Another question: I know in
socket.io I can do an emit (custom_event), does
> sockjs support a custom event like that?
SockJS is a transport layer. You can send a message, you can receive
a message. That's it more or less. If you want to have an api that
has an 'emit' method - feel free to write it on top of SockJS.
> Yet Another question: I tried making sockjs work, but i keep getting
> "WebSocket is closed before the connection is established" this is on latest
> stable release of Chrome.
I can think of two causes:
1) Aren't you closing the connection before it's established?
2) SockJS might fall back from native websockets, to different
tranports. In such case it will:
- get the data from /prefix/info url
- open native websocket connection
- if it didn't reply within a time limit - SockJS will close it
- and start using a fallback transport
Marek