yii-node-socket icon indicating copy to clipboard operation
yii-node-socket copied to clipboard

Setup With Multi-domain WebApp

Open SaqibZafar opened this issue 10 years ago • 9 comments

Hello,

First off, excellent work on the extension, does exactly what i was looking for. But i am having trouble setting this up on my current app. It supports wildcard sub-domains for users. Meaning it would create a sub-domain for each user when he/she signs up. I am using Yii 1 currently that goes with your tag 2.0.0. The problem is that a socket is connecting, the log shown below gives details that its not sending expected cookie with the request.

info: socket.io started
Listening site.dev:3001
Set origin: site.dev:* *:*
debug: served static content /socket.io.js
debug: served static content /socket.io.js
debug: client authorized
info: handshake authorized tl2FmZm168Sw6OAJOevM
debug: setting request GET /socket.io/1/websocket/tl2FmZm168Sw6OAJOevM
debug: set heartbeat interval for client tl2FmZm168Sw6OAJOevM
debug: client authorized for 
debug: websocket writing 1::
debug: client unauthorized for /client
warn: handshake error NO COOKIE TRANSMITTED for /client
debug: websocket writing 7::/client:undefined
debug: emitting heartbeat for client tl2FmZm168Sw6OAJOevM
debug: websocket writing 2::
debug: set heartbeat timeout for client tl2FmZm168Sw6OAJOevM
debug: got heartbeat packet
debug: cleared heartbeat timeout for client tl2FmZm168Sw6OAJOevM
debug: set heartbeat interval for client tl2FmZm168Sw6OAJOevM
debug: emitting heartbeat for client tl2FmZm168Sw6OAJOevM
debug: websocket writing 2::
debug: set heartbeat timeout for client tl2FmZm168Sw6OAJOevM
debug: got heartbeat packet
debug: cleared heartbeat timeout for client tl2FmZm168Sw6OAJOevM
debug: set heartbeat interval for client tl2FmZm168Sw6OAJOevM
debug: emitting heartbeat for client tl2FmZm168Sw6OAJOevM
debug: websocket writing 2::
debug: set heartbeat timeout for client tl2FmZm168Sw6OAJOevM
debug: got heartbeat packet
debug: cleared heartbeat timeout for client tl2FmZm168Sw6OAJOevM
debug: set heartbeat interval for client tl2FmZm168Sw6OAJOevM
debug: emitting heartbeat for client tl2FmZm168Sw6OAJOevM
debug: websocket writing 2::
debug: set heartbeat timeout for client tl2FmZm168Sw6OAJOevM
debug: got heartbeat packet
debug: cleared heartbeat timeout for client tl2FmZm168Sw6OAJOevM
debug: set heartbeat interval for client tl2FmZm168Sw6OAJOevM
debug: emitting heartbeat for client tl2FmZm168Sw6OAJOevM
debug: websocket writing 2::
debug: set heartbeat timeout for client tl2FmZm168Sw6OAJOevM
debug: got heartbeat packet
debug: cleared heartbeat timeout for client tl2FmZm168Sw6OAJOevM
debug: set heartbeat interval for client tl2FmZm168Sw6OAJOevM
debug: emitting heartbeat for client tl2FmZm168Sw6OAJOevM
debug: websocket writing 2::
debug: set heartbeat timeout for client tl2FmZm168Sw6OAJOevM
debug: got heartbeat packet
debug: cleared heartbeat timeout for client tl2FmZm168Sw6OAJOevM
debug: set heartbeat interval for client tl2FmZm168Sw6OAJOevM
debug: emitting heartbeat for client tl2FmZm168Sw6OAJOevM

console config:-

'nodeSocket' => array(
            'class' => 'application.extensions.yii-node-socket.lib.php.NodeSocket',
            'host' => 'site.dev',
            'port' => 3001,
            'origin' => '*:*',
        )

client config:-

'nodeSocket' => array(
            'class' => 'application.extensions.yii-node-socket.lib.php.NodeSocket',
            'host' => 'demo.site.dev',
            'port' => 3001,
            'origin' => '*:*',
        ),

the console shows that socket is trying to connect, but there is no connection. screenshot_9

If i change my client config to

'host' => 'demo.site.dev',

it works perfectly. But since the domain part will be dynamic i would like this to work on multiple domains with host as site.dev. i guess the socket is not authorizing and hence no connection, any suggestions?

SaqibZafar avatar Jun 14 '15 18:06 SaqibZafar

ok, got something figured out, cookie that was being used was configured by domain name of 'demo.site.dev', that cause problems. I used a custom cookie and socket has connected. Thanks anyways :+1:

SaqibZafar avatar Jun 14 '15 22:06 SaqibZafar

hi, good work))

oncesk avatar Jun 15 '15 08:06 oncesk

@SaqibZafar Solving same problem now. Can you please share your implementation of multi-domain sockets?

berdof avatar Jul 05 '15 13:07 berdof

@berdof i used following settings in my config:- 'nodeSocket' => array( 'class' => 'application.extensions.yii-node-socket.lib.php.NodeSocket', 'host' => 'mysite.dev', 'port' => 3001, 'sessionVarName' => 'sockssid', 'origin' => '*:*', ), i am enabling all domains to disable cors, although you should not do this due to security reasons. i have added my own sockssid variable that i use instead of PHPSSID, i create this as a cookie in my useridentity after each login, this will enable you to connect to socket.io.

                // for socket.io authentication
                $cookie = new CHttpCookie('sockssid', session_id());
                $cookie->domain = '.mysite.dev';
                Yii::app()->request->cookies['sockssid'] = $cookie;

hope this helps :)

SaqibZafar avatar Jul 05 '15 16:07 SaqibZafar

hi, as i can see you just need to work with single nodejs server instance but many php backend servers?

oncesk avatar Jul 06 '15 09:07 oncesk

@oncesk I need a very simple implementation. Actually I need a separate logic. So I need to start sockets server on one domain and connect to it on another. @SaqibZafar where have you placed authentication logic in php ? So as far as I understand I just need to change handshakeData.cookie['PHPSESSID'] to handshakeData.cookie['sockssid'] and my cookies will be shared between domains .

berdof avatar Jul 06 '15 09:07 berdof

yes, its right, and you can set top level domain name in you javascript

// on somedomain.topleveldomain.com you can set
document.domain = 'topleveldomain.com';

and now if you start you nodejs server on topleveldomain.com cookie should be received by the server

for handshaking with session variable name you can use sessionVarName in configuration as you wrote below

oncesk avatar Jul 06 '15 09:07 oncesk

i am using default authentication from CUserIdentity for a user, then creating a Cookie from php session and passing that to socket.io config.

On Mon, Jul 6, 2015 at 2:34 PM, oncesk [email protected] wrote:

yes, its right, and you can set top level domain name in you javascript

// on somedomain.topleveldomain.com you can setdocument.domain = 'topleveldomain.com';

and now if you start you nodejs server on topleveldomain.com cookie should be received by the server

for handshaking with session variable name you can use sessionVarName in configuration as you wrote below

— Reply to this email directly or view it on GitHub https://github.com/oncesk/yii-node-socket/issues/50#issuecomment-118786596 .

SaqibZafar avatar Jul 06 '15 09:07 SaqibZafar

i do not fully understand which kind you problem, does problem actual ? :)

if you could not authenticate on nodejs server can you show me your nodejs logs

oncesk avatar Jul 06 '15 10:07 oncesk