Remotely connecting to socket.io instance? Possible?

1,001 views
Skip to first unread message

andy matthews

unread,
Sep 21, 2011, 10:03:33 AM9/21/11
to sock...@googlegroups.com
I'm sure this is a completely n00b question, but here goes.

I'm learning Node and my first test app is a chat app written using Socket.io (my code: https://github.com/commadelimited/Chat/). I've got it running on a Joyent smart machine at http://commadelimited.no.de/. It works great, but I'd like to skip hosting the HTML code at Joyent and run it somewhere else: localhost, another server, mobile app, etc.

My question is this: can I expect to run the socket server on Joyent, but hit that server/port from another website?

Daniel Shaw

unread,
Sep 21, 2011, 1:22:55 PM9/21/11
to sock...@googlegroups.com
Yes. That's basically how all our production systems work.

- @dshaw

pdeschen

unread,
Sep 21, 2011, 4:24:19 PM9/21/11
to Socket.IO
Since you can't access a websocket resource on another domain (e.g.,
joyent) because of same origin policy restrictions [1], you would
either need a proxy in front of both joyent and your html site [2] or
use jsonp polling within socket.io [3].

[1] http://en.wikipedia.org/wiki/Same_origin_policy
[2] https://github.com/LearnBoost/socket.io/wiki/Nginx-and-Socket.io
[3] http://showmetheco.de/articles/2011/8/socket-io-for-backend-developers.html#Cross-Domain%20connections

@pdeschen

Shripad K

unread,
Sep 21, 2011, 8:09:51 PM9/21/11
to sock...@googlegroups.com
You can access a Websocket resource cross domain.
My app depends on the very fact that you can use Websockets cross domain :)
My app depends on the very fact that you can use Websockets cross domain :)

Shripad K

unread,
Sep 21, 2011, 8:10:43 PM9/21/11
to sock...@googlegroups.com
On Thu, Sep 22, 2011 at 5:39 AM, Shripad K <assortme...@gmail.com> wrote:
You can access a Websocket resource cross domain.
My app depends on the very fact that you can use Websockets cross domain :)
My app depends on the very fact that you can use Websockets cross domain :)

whoops sorry for the double lines!

andy matthews

unread,
Sep 22, 2011, 3:01:16 PM9/22/11
to sock...@googlegroups.com
Shripad...

I've got my code hosted on Github:

Do you see anything out of the ordinary?

pdeschen

unread,
Sep 22, 2011, 3:26:10 PM9/22/11
to Socket.IO
Oh. Nice then. I was under the false impression that it was not
possible! Thanks for the info!

Cheers

@pdeschen

On Sep 21, 8:09 pm, Shripad K <assortmentofso...@gmail.com> wrote:
> You can access a Websocket resource cross domain.
> My app depends on the very fact that you can use Websockets cross domain :)
> My app depends on the very fact that you can use Websockets cross domain :)
>
>
> On Thu, Sep 22, 2011 at 1:54 AM, pdeschen <pdesc...@gmail.com> wrote:
> > Since you can't access a websocket resource on another domain (e.g.,
> > joyent) because of same origin policy restrictions [1], you would
> > either need a proxy in front of both joyent and your html site [2] or
> > use jsonp polling within socket.io [3].
>
> > [1]http://en.wikipedia.org/wiki/Same_origin_policy
> > [2]https://github.com/LearnBoost/socket.io/wiki/Nginx-and-Socket.io
> > [3]
> >http://showmetheco.de/articles/2011/8/socket-io-for-backend-developer...
>
> > @pdeschen
>
> > On Sep 21, 1:22 pm, Daniel Shaw <ds...@dshaw.com> wrote:
> > > Yes. That's basically how all our production systems work.
>
> > > - @dshaw
>
> > > On Wed, Sep 21, 2011 at 7:03 AM, andy matthews <a...@commadelimited.com>
> > wrote:
> > > > I'm sure this is a completely n00b question, but here goes.
> > > > I'm learning Node and my first test app is a chat app written using
> > > > Socket.io (my code:https://github.com/commadelimited/Chat/). I've got
> > it
> > > > running on a Joyent smart machine athttp://commadelimited.no.de/. It

Shripad K

unread,
Sep 23, 2011, 4:17:38 AM9/23/11
to sock...@googlegroups.com
Yes. Unfortunately this has not been documented anywhere. You need to just pass the entire URI as a string. The port would be parsed from the URI string itself.



should be:

var socket = io.connect('http://commadelimited.no.de/80')

Also something to wary about:
The code reads: uri.port || document.location.port

So if your app is running on port 3000 (localhost), it would assume that the remote port is also 3000.
(Since your original code does not pass a port, it would have assumed the local port to be the same as the remote port.
However, the socket.io server isn't listening on that particular port)

Best practice is to pass the entire URI.

Hope this helps :)

Shripad K 

andy matthews

unread,
Sep 23, 2011, 11:09:40 AM9/23/11
to sock...@googlegroups.com
Awesome. I'll change it and test this afternoon! Thank you so much!

Shripad K

unread,
Sep 24, 2011, 6:12:46 AM9/24/11
to sock...@googlegroups.com
Sure :) Also it should be:

var socket = io.connect('http://commadelimited.no.de:80')
Reply all
Reply to author
Forward
0 new messages