Trying to use raw websockets with sockjs-node

1,358 views
Skip to first unread message

Tim Fox

unread,
Apr 24, 2012, 12:41:13 PM4/24/12
to sockjs
I'm trying to use sockjs-node as a simple raw websockets echo server.

I've started the echo example from the distribution

https://github.com/sockjs/sockjs-node/tree/master/examples/echo

And I'm launching a websockets client at it. The client is attempting to
connect at /echo/websocket.

However, instead of the expected websocket handshake response, I get a
404 from the server and the server logs this out:

tim@Ethel:~/projects/sockjs-node/examples/echo$ node server.js
The "sys" module is now called "util". It should have a similar interface.
SockJS v0.3.1 bound to "/echo"
[*] Listening on localhost:8080
GET /echo/websocket 2ms (unfinished)

Any ideas?

Cheers

--
Tim Fox

Vert.x - effortless polyglot asynchronous application development
http://vertx.io
twitter:@timfox

Marek Majkowski

unread,
Apr 24, 2012, 12:46:23 PM4/24/12
to timv...@gmail.com, sockjs
On Tue, Apr 24, 2012 at 17:41, Tim Fox <timv...@gmail.com> wrote:
> I'm trying to use sockjs-node as a simple raw websockets echo server.
>
> I've started the echo example from the distribution
>
> https://github.com/sockjs/sockjs-node/tree/master/examples/echo
>
> And I'm launching a websockets client at it. The client is attempting to
> connect at /echo/websocket.
>
> However, instead of the expected websocket handshake response, I get a 404
> from the server and the server logs this out:
>
> tim@Ethel:~/projects/sockjs-node/examples/echo$ node server.js
> The "sys" module is now called "util". It should have a similar interface.
> SockJS v0.3.1 bound to "/echo"
>  [*] Listening on localhost:8080
> GET /echo/websocket 2ms (unfinished)

From the command line:

$ cd sockjs-node
$ npm install --dev
$ make test_server

From the browser:
> ws = new WebSocket('ws://localhost:8081/echo/websocket');
> ws.onmessage = function(e){console.log('message', e.data)};
> ws.send('a')
message a

That works for me.

Marek

Tim Fox

unread,
Apr 24, 2012, 1:03:30 PM4/24/12
to Marek Majkowski, sockjs

I'm not using a browser, I'm using a websockets client.

It's sending a GET request to /echo/websocket

> Marek

Tim Fox

unread,
Apr 24, 2012, 1:07:36 PM4/24/12
to Marek Majkowski, sockjs
On 24/04/12 17:46, Marek Majkowski wrote:
> On Tue, Apr 24, 2012 at 17:41, Tim Fox<timv...@gmail.com> wrote:
>> I'm trying to use sockjs-node as a simple raw websockets echo server.
>>
>> I've started the echo example from the distribution
>>
>> https://github.com/sockjs/sockjs-node/tree/master/examples/echo
>>
>> And I'm launching a websockets client at it. The client is attempting to
>> connect at /echo/websocket.
>>
>> However, instead of the expected websocket handshake response, I get a 404
>> from the server and the server logs this out:
>>
>> tim@Ethel:~/projects/sockjs-node/examples/echo$ node server.js
>> The "sys" module is now called "util". It should have a similar interface.
>> SockJS v0.3.1 bound to "/echo"
>> [*] Listening on localhost:8080
>> GET /echo/websocket 2ms (unfinished)
> From the command line:
>
> $ cd sockjs-node
> $ npm install --dev
> $ make test_server

Hmm, I'm running from the echo example from the examples, as per the README

cd sockjs-node/examples/echo
npm install
node server.js

I tried to also run from sockjs-node dir as you suggested, but this
gives me an error:

tim@Ethel:~/projects/sockjs-node$ npm install --dev

tim@Ethel:~/projects/sockjs-node$ make test_server
node examples/test_server/server.js

node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'sockjs'
at Function._resolveFilename (module.js:334:11)
at Function._load (module.js:279:25)
at Module.require (module.js:357:17)
at require (module.js:368:17)
at Object.<anonymous>
(/home/tim/projects/sockjs-node/examples/test_server/sockjs_app.js:1:76)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Module.require (module.js:357:17)
make: *** [test_server] Error 1

> From the browser:
>> ws = new WebSocket('ws://localhost:8081/echo/websocket');
>> ws.onmessage = function(e){console.log('message', e.data)};
>> ws.send('a')
> message a
>
> That works for me.
>
> Marek

Tim Fox

unread,
Apr 24, 2012, 1:19:07 PM4/24/12
to Marek Majkowski, sockjs
I should also add... the client works fine against the vert.x sockjs
server implementation, with the path /echo/websocket.

The only thing I can think of is perhaps the raw websocket transport is
not enabled on the sockjs-node server?

Tim Fox

unread,
Apr 24, 2012, 1:38:50 PM4/24/12
to Marek Majkowski, sockjs
More info:

It's actually returning a 400 (not 404, my mistake), with status message
"Bad Request"

I grep'd through the sockjs-node source and can't find any "Bad Request"
so I guess this comes from the websockets library you use...

Marek Majkowski

unread,
Apr 24, 2012, 1:52:51 PM4/24/12
to Tim Fox, sockjs
On Tue, Apr 24, 2012 at 18:38, Tim Fox <timv...@gmail.com> wrote:
> More info:
>
> It's actually returning a 400 (not 404, my mistake), with status message
> "Bad Request"
>
> I grep'd through the sockjs-node source and can't find any "Bad Request" so
> I guess this comes from the websockets library you use...

Tim,

You can't just issue a GET request, websockets handshake is more complicated.
You need more details, here's an example:
https://github.com/sockjs/sockjs-protocol/blob/master/sockjs-protocol-0.3.py#L604

Additionally, the raw websockets interface is intended to be used from
RFC6455 clients,
here's the relevant check:
https://github.com/sockjs/sockjs-node/blob/master/src/trans-websocket.coffee#L47

Cheers,
Marek

Marek Majkowski

unread,
Apr 24, 2012, 1:54:14 PM4/24/12
to Tim Fox, sockjs
> node examples/test_server/server.js
>
> node.js:201
>        throw e; // process.nextTick error, or 'error' event on first tick
>              ^
> Error: Cannot find module 'sockjs'

Yes, I was overoptimistic, you also need:

$ ln -s .. node_modules/sockjs

As suggested in sockjs-node readme.

Marek

Tim Fox

unread,
Apr 24, 2012, 1:59:02 PM4/24/12
to Marek Majkowski, sockjs
On 24/04/12 18:52, Marek Majkowski wrote:
> On Tue, Apr 24, 2012 at 18:38, Tim Fox<timv...@gmail.com> wrote:
>> More info:
>>
>> It's actually returning a 400 (not 404, my mistake), with status message
>> "Bad Request"
>>
>> I grep'd through the sockjs-node source and can't find any "Bad Request" so
>> I guess this comes from the websockets library you use...
> Tim,
>
> You can't just issue a GET request, websockets handshake is more complicated.
> You need more details, here's an example:
> https://github.com/sockjs/sockjs-protocol/blob/master/sockjs-protocol-0.3.py#L604

Yes, obviously I understand that. (I have written a working websockets
client). :)

I am just telling you the GET request that the client is sending.

Anyway... I have got it working.

The problem seems to be in the versions of websockets that sockjs-node
understands.

Couldn't get it to work with Hybi00 or 08, but I can get it to work with
Hybi17 if I change the client to send the version 13.

Tim Fox

unread,
Apr 24, 2012, 2:01:28 PM4/24/12
to Marek Majkowski, sockjs
On 24/04/12 18:52, Marek Majkowski wrote:
> On Tue, Apr 24, 2012 at 18:38, Tim Fox<timv...@gmail.com> wrote:
>> More info:
>>
>> It's actually returning a 400 (not 404, my mistake), with status message
>> "Bad Request"
>>
>> I grep'd through the sockjs-node source and can't find any "Bad Request" so
>> I guess this comes from the websockets library you use...
> Tim,
>
> You can't just issue a GET request, websockets handshake is more complicated.
> You need more details, here's an example:
> https://github.com/sockjs/sockjs-protocol/blob/master/sockjs-protocol-0.3.py#L604
>
> Additionally, the raw websockets interface is intended to be used from
> RFC6455 clients,
> here's the relevant check:
> https://github.com/sockjs/sockjs-node/blob/master/src/trans-websocket.coffee#L47

This makes sense - I got it to work by sending the version number 13.
Can you explain a but why you only accept 8 and 13 as valid versions?

I'm not sure I really understand this

Marek Majkowski

unread,
Apr 24, 2012, 2:25:04 PM4/24/12
to Tim Fox, sockjs
> This makes sense - I got it to work by sending the version number 13. Can
> you explain a but why you only accept 8 and 13 as valid versions?
>
> I'm not sure I really understand this

The original idea was to support only the 13, ie RFC6455, but
it was virtually impossible to test that - no python client supported
that. So I added '8' as a temporary measure.

I should revisit and remove the '8'.

Marek

Reply all
Reply to author
Forward
0 new messages