dynamic rooms/channels

1,849 views
Skip to first unread message

Wahyudinata Setiawan

unread,
Jun 20, 2012, 3:05:19 PM6/20/12
to soc...@googlegroups.com
I know that sockjs supports multiplexing + prefix path (ex: '/echo') but how would you go about implementing dynamic rooms?
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: 
  1. send a command sockjs.send({type:'command', 'user_join', 'channel_1'});
  2. server parses the command with maybe an if statement or an object => function
  3. do a registerChannel(channel_name) , in this case would be "channel_1" or get from an existing channel
  4. 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? 

Another question: I know in socket.io I can do an emit (custom_event), does sockjs support a custom event like that?

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. 


Wahyudinata Setiawan

unread,
Jun 20, 2012, 3:07:36 PM6/20/12
to soc...@googlegroups.com
oh one more thing, where would I find more tutorials about sockjs? Socket.io has a lot of docs (but after deeper googling, it turns out there are problems regarding socket.io) I tried searching for tutorials but I  keep getting information about socks, yes socks, for  shoes socks :D

Marek Majkowski

unread,
Jun 20, 2012, 3:34:03 PM6/20/12
to wahyu...@gmail.com, soc...@googlegroups.com
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

Marek Majkowski

unread,
Jun 20, 2012, 3:34:46 PM6/20/12
to wahyu...@gmail.com, soc...@googlegroups.com

Wahyudinata Setiawan

unread,
Jun 20, 2012, 4:41:53 PM6/20/12
to soc...@googlegroups.com, wahyu...@gmail.com
The docs on usage is rather sparse, maybe it's just me. So far what I have gathered from the example, sockjs supports:
onopen, onclose, onmessage but after digging deeper in the groups I have found a heartbeat event? Is there an example for that? Are there more events that I'm missing?


On Wednesday, June 20, 2012 12:34:46 PM UTC-7, majek wrote:
On Wed, Jun 20, 2012 at 8:07 PM, Wahyudinata Setiawan

Marek Majkowski

unread,
Jun 20, 2012, 6:10:50 PM6/20/12
to wahyu...@gmail.com, soc...@googlegroups.com
On Wed, Jun 20, 2012 at 9:41 PM, Wahyudinata Setiawan
<wahyu...@gmail.com> wrote:
> The docs on usage is rather sparse, maybe it's just me.

Well, the underlying idea is that SockJS looks like a websocket object,
so you should take a look at websocket API.

> So far what I have
> gathered from the example, sockjs supports:
> onopen, onclose, onmessage

That's it! You also have 'send' call.

> but after digging deeper in the groups I have
> found a heartbeat event? Is there an example for that? Are there more events
> that I'm missing?

Nope. Heartbeat is actually a non-standard hack. We'll include a
proper heartbeats support in next sockjs version.
(proper support - as per: handling the case of unplugged
network cable)

Marek

Wahyudinata Setiawan

unread,
Jun 20, 2012, 6:45:10 PM6/20/12
to soc...@googlegroups.com, wahyu...@gmail.com


Thank you for your prompt answers. You will end up being pestered by my questions again in the future :)




On Wednesday, June 20, 2012 3:10:50 PM UTC-7, majek wrote:
On Wed, Jun 20, 2012 at 9:41 PM, Wahyudinata Setiawan

Marek Majkowski

unread,
Jun 20, 2012, 6:54:03 PM6/20/12
to wahyu...@gmail.com, soc...@googlegroups.com
On Wed, Jun 20, 2012 at 11:45 PM, Wahyudinata Setiawan
<wahyu...@gmail.com> wrote:
>
>
> Thank you for your prompt answers. You will end up being pestered by my
> questions again in the future :)

No worries, it's always interesting to hear opinions/problems reported
by new users - you can only learn single piece of software once.

You can also try joining #sockjs IRC channel on freenode.

Marek

Wahyudinata Setiawan

unread,
Jun 21, 2012, 3:21:01 AM6/21/12
to soc...@googlegroups.com, wahyu...@gmail.com
I'm still getting WebSocket is closed before the connection is established. 

before I can even do anything, do I need to close something before I use it? Per your recommendation, I am following : https://github.com/sockjs/sockjs-node/blob/master/examples/express/index.html

this is my app.js (using express):

var sockjs = require('sockjs');
var sockjs_opts = {sockjs_url: "http://cdn.sockjs.org/sockjs-0.3.min.js", disconnect_delay:100000};
var sockjs_echo = sockjs.createServer(sockjs_opts);
sockjs_echo.on('connection', function(conn) {
    conn.on('data', function(message) {
        conn.write(message);
    });
});
sockjs_echo.installHandlers(app, {prefix:'/rt'});

this is client:

App.get_socket = function(){
    var socket = App._socket
    if (socket){
        return socket;
    }
    socket = new SockJS('http://localhost/rt', null, {debug:true});
    App._socket = socket;
    return socket;
}

var socket = App.get_socket();

for the debug messages:
  1. Resource interpreted as Script but transferred with MIME type text/html: "http://localhost/js/app/event.js". steal.js:1519
Opening transport: websocket url:http://localhost/rt/777/rj6vo11q RTO:206 sockjs-0.3.min.js:27
WebSocket is closed before the connection is established. :1
Closed transport: websocket SimpleEvent(type=close, code=2007, reason=Transport timeouted, wasClean=false) sockjs-0.3.min.js:27
Opening transport: xhr-streaming url:http://localhost/rt/777/t4vfanzh RTO:206

I am confused, I was able to get socket.io up and running the first try (if im not mistaken also uses xhr streaming) so im not sure what's happening.


On Wednesday, June 20, 2012 3:54:03 PM UTC-7, majek wrote:
On Wed, Jun 20, 2012 at 11:45 PM, Wahyudinata Setiawan
>
>

Wahyudinata Setiawan

unread,
Jun 21, 2012, 3:44:55 AM6/21/12
to soc...@googlegroups.com
another update: I tried the same thing on Firefox, it works great. I got connected and everything works fine, so it's a browser thing I assume?

here is my chrome version: 19.0.1084.56 m

Marek Majkowski

unread,
Jun 21, 2012, 5:12:19 AM6/21/12
to wahyu...@gmail.com, soc...@googlegroups.com
On Thu, Jun 21, 2012 at 8:44 AM, Wahyudinata Setiawan
<wahyu...@gmail.com> wrote:
> another update: I tried the same thing on Firefox, it works great. I got
> connected and everything works fine, so it's a browser thing I assume?
>
> here is my chrome version: 19.0.1084.56 m

I have the same version. Take a look at the test site:

https://sockjs.popcnt.org/example-cursors.html

choose 'websocket' protocol, and press 'connect'. If you see a red rectangle -
it's all working fine. It does for me.

I won't have time today to try to run your code, I'll try it later.

Marek

Wahyudinata Setiawan

unread,
Jun 21, 2012, 5:59:58 AM6/21/12
to soc...@googlegroups.com, wahyu...@gmail.com
it shows a rectangle for me too on chrome, I am not sure what is causing my code to not work though since it works fine on FF

Marek Majkowski

unread,
Jun 21, 2012, 6:24:21 AM6/21/12
to wahyu...@gmail.com, soc...@googlegroups.com
Can you show me a screenshot of the network tab from chrome inspector?

Marek

Marek Majkowski

unread,
Jun 21, 2012, 6:59:03 AM6/21/12
to wahyu...@gmail.com, soc...@googlegroups.com
Question: are you running your site from file:// or http:// url?

Marek

Wahyudinata Setiawan

unread,
Jun 21, 2012, 2:05:35 PM6/21/12
to soc...@googlegroups.com, wahyu...@gmail.com
not it's served from node.js/express backend
here is the chrome network tab screenshot:

I also exported the HAR which you can view on Fiddler2 (the online HAR viewer for some reason  didnt work): 

I should mention that I use steal.js to load the javascript files asynchronously, to make sure that doesnt cause the problem, I tried loading sockjs using <script> and it still doesnt work on Chrome.

Marek Majkowski

unread,
Jun 22, 2012, 10:36:50 AM6/22/12
to wahyu...@gmail.com, soc...@googlegroups.com
Hi,

I finally had time to look at the code you attached. I made it runnable:
https://gist.github.com/2973054

It all works fine for me. Can you download the code and try
if it works for you?

Thanks for the screenshots of the chrome network tab, they look
normal. In first one I can see normal html/js downloaded,
on the second sockjs connects to /info
and later falls back to 'xhr_streaming' transport. The request
to xhr_streaming seems to be running for few minutes - all good.

At the top of the first shot there is connection to sockjs websocket
endpoint - chrome tends to rearrange ws connections in this view.

I have two questions:
1) Is sockjs not working? What you pasted from console.log seems
to suggest that SockJS falls back from websockets to
xhr-streaming successfully.

2) What load balancer / proxy are you using? I see you
run your site from localhost:80, so I guess there is
a proxy between browser and the app. Maybe the proxy
doesn't forward websockets?

Cheers,
Marek

Wahyudinata Setiawan

unread,
Jun 22, 2012, 4:55:02 PM6/22/12
to soc...@googlegroups.com, wahyu...@gmail.com
1. SockJS does indeed work, but, the onclose event is fired before the onopen, and this only happens in Chrome. After several googling, apparently Chrome is just chatty? I am not sure. But the onopen is called and I can send and receive normally. So it goes like: onclose, onopen, (at this stage i can do send and receive normally)

2. I am not using any, after googling, apparently anti virus might have some effect? I use microsoft security essential, no firewal, no proxy.


On Friday, June 22, 2012 7:36:50 AM UTC-7, majek wrote:
On Thu, Jun 21, 2012 at 7:05 PM, Wahyudinata Setiawan

Marek Majkowski

unread,
Jun 26, 2012, 6:42:27 PM6/26/12
to wahyu...@gmail.com, soc...@googlegroups.com
On Fri, Jun 22, 2012 at 9:55 PM, Wahyudinata Setiawan
<wahyu...@gmail.com> wrote:
> 1. SockJS does indeed work, but, the onclose event is fired before the
> onopen, and this only happens in Chrome. After several googling, apparently
> Chrome is just chatty? I am not sure. But the onopen is called and I can
> send and receive normally. So it goes like: onclose, onopen, (at this stage
> i can do send and receive normally)

SockJS will fire onclose exactly once per connection / session. You must
have some reconnection logic. If not - please prepare a test case, so that
I could reproduce it.

> 2. I am not using any, after googling, apparently anti virus might have some
> effect? I use microsoft security essential, no firewal, no proxy.

Interesting. Can you serve stuff from node directly on port 80?

Marek

julienp...@gmail.com

unread,
Mar 5, 2014, 7:14:00 PM3/5/14
to soc...@googlegroups.com, wahyu...@gmail.com
So I almost have the code for this dynamic chatroom business: http://www.instabid.org/server.js
I didn't find anything better than creating an array of multiplexed room.
The major issue/bug is that the conn.on('data' event doubles up its action everytime somebody creates a new chatroom.
I would love some help with this. I don't understand the gap between the ann/bob example and the code provided on https://github.com/sockjs/websocket-multiplex/blob/master/multiplex_server.js
jayjaychicago[a]yahoo.com 
 

Hassan Butt

unread,
Jun 13, 2014, 2:27:21 AM6/13/14
to soc...@googlegroups.com, wahyu...@gmail.com

Hi,
Can you please let me know that, this repository "SocketRocket" supports chatting between two friends, As I've integrated this in my XCode Project, and its a broadcast communication. Do you have any idea about this? How Can I enable chating between two devices/friends?

Thanks in advance... :)

Reply all
Reply to author
Forward
0 new messages