Switching to Websockets... Handshake fails...

660 views
Skip to first unread message

Nicolas

unread,
May 6, 2016, 5:17:15 PM5/6/16
to APE Project
After using the default transport mode for years, I am trying to switch to Websockets. I suspect that this might improve the situation with APE being not fully reliable in certain circumstances. Sometimes, commands to APE stop being received (when the user is still connected to the Internet, I know this because they can send me logs via AJAX after the APE connection goes wrong). In these situations, resending the command does not help. APE just does not receive from the client.

So in short, I want to try Websockets and see if it helps.

After including the appropriate transport JS file in the config as well as switch APE.Config.transport to 6, I get an error in the handshake.


ape_jsf.min:973 WebSocket connection to 'ws://1.ape.local.ape-project.org/6/' failed: Error during WebSocket handshake: 'Upgrade' header is missing

Error on this line:
},initWs:function(){this.ws=new WebSocket((this.ape.options.secure?"wss":"ws")+"://"+this.ape.options.frequency+"."+this.ape.options.server+"/"+this.ape.options.transport+"/");

Does it ring a bell?

Do I need to do something on the APE server side? Or cannot it work at all because I have some weird settings there? (I see a thread where Pablo Tejada talks about this possibility briefly).

My Apache config for APE is below. Maybe the Proxy bits are the cause of the problem?

<VirtualHost *:80>

    ServerName ape_on_optiplex_9020
    ServerAlias ape.local.ape-project.org
    ServerAlias *.ape.local.ape-project.org

        # Everything expires after 10 months except php files that are not cached
        ExpiresActive On
        ExpiresDefault A2592000
        #10 months
        ExpiresByType text/php A0

    ProxyRequests Off
    ProxyPreserveHost On
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
   ProxyPass / http://localhost:6969/
   ProxyPassReverse / http://localhost:6969/
    <Location />
        Order allow,deny
        Allow from all
    </Location>

</VirtualHost>

Nicolas

unread,
May 6, 2016, 5:23:25 PM5/6/16
to APE Project
On my staging server, I get a different error, but on the same line:

WebSocket connection to 'ws://1.staging.hiddenurl.com/6/' failed: Invalid frame header

Louis Charette

unread,
May 6, 2016, 5:25:22 PM5/6/16
to ape-p...@googlegroups.com
Hi!

Indeed, it's probably proxy related. I remember something about Apache failing to proxy websocket causing me to use HaProxy instead of Apache proxy/reverseproxy for ape (there a wiki article on github about HaProxy). That and I think Apache need a special line for websocket proxy.

 Try connecting to the raw url instead with the 6969 port, you'll see if it works at all. 

I know also websocket support on the server side was introduced in one of the latest version (in case you're using pre 1.1.2 or something version). 

  - Louis
--
--
You received this message because you are subscribed to the Google
Groups "APE Project" group.
To post to this group, send email to ape-p...@googlegroups.com
To unsubscribe from this group, send email to
ape-project...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/ape-project?hl=en
---
APE Project (Ajax Push Engine)
Official website : http://www.ape-project.org/
Git Hub : http://github.com/APE-Project/

---
You received this message because you are subscribed to the Google Groups "APE Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ape-project...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Envoyé de mon iPhone

Happy Meeple

unread,
May 6, 2016, 5:36:25 PM5/6/16
to APE Project
Many thanks Louis!

I will probably look into this.

I did not intend to spend a lot of time on this issue... So many other things to do :)



  Happy Meeple


Nicolas Guibert
Games in Mind Ltd
Managing director
7 Spencer Walk
London NW3 1QZ



Pablo Tejada

unread,
May 6, 2016, 6:50:02 PM5/6/16
to APE Project

Hey,

This is most likely a proxy issue like Luis suggests. I had a similar issue with my hosting provider because they use nginx proxy servers that did not supported websockets. My solution was to make the custom application port open/public. Within the year they upgraded to a nginx version that is suppose to support websocket connections but I have not tried it yet.

The only pros I see to using a proxy server rather than a direct connection to APE is if the proxy provides a  secure TLS connection.

Sincerely,

Pablo Tejada

From Mobile

Louis Charette

unread,
May 7, 2016, 1:32:34 AM5/7/16
to ape-p...@googlegroups.com
...and if the proxy allow connection to ape using port 80 (alongside Apache) for users who have port 6969 blocked.

Nicolas

unread,
May 9, 2016, 10:07:42 AM5/9/16
to APE Project
Yes indeed Louis, that was the reason why I had to proxy. When I launched my online board gaming platform, I quickly realized that some users could not connect. I then had to make sure everyone accessed APE via port 80.

I don't know what is easier:

- recompile Apache to get a working mod_proxy_wstunnel and an easy configuration as a result
- use HAProxy
- any other solution...

Guys, if you could edit previous posts so that my email address does not show up, that would be great! I am not sure the option is available though.

Nicolas.

Nicolas

unread,
May 9, 2016, 10:44:09 AM5/9/16
to APE Project
I am now looking at HAProxy. It seems to be the easiest way.

I am little bit confused about what to do with the Apache previous config. You will see my current Apache config (before using HAProxy.

I am pretty sure that I must amend the virtualhost dealing with to remove the Proxy lines. If HAProxy does the task now. Surely Apache will not do it anymore.

In order to do so, I simply removed all lines related to Proxy in the second bit of the Apache config. Unfortunately, then http://ape.local.ape-project.org/ can't be reached. 
I then tried to remove the 2nd virtual host altogher. But it is not an improvement. Then ape.local.ape-project.org si redirected to ape.local.ape-project.org/en/ as per the first virtual host settings.

So is it correct in removing the proxy lines in the 2nd virtual host (and in that case, it is just my Haproxy config that is wrong) or is there anything else to do?

Thanks for your help!


<VirtualHost *:80>


DocumentRoot "/mnt/hgfs/HM/"
#/var/www/TestAPE2/"

RewriteEngine on
RewriteLog "/var/www/apache_log/rewrite_hm_80.log"
RewriteLogLevel 8

# Add / at the end of URL that don't have it (www.happymeeple.com/fr => www.happymeeple.com/fr/)
RewriteRule ^/([a-z]{2})$ $1 [R=301,L]

# Exclude some directories from all rewriting
RewriteRule ^/(phpmyadmin|phpmyadmin_test|games|media|forum)($|/) - [L]

        # Rewrite the / default to /en/index.php
        RewriteRule ^/$ /en/ [R=301,L]

# All URLs that do not have a "." and at least one directory behind the  in them are considered for the new architecture (folder architecture) and are redirected to /info/direct_to.php page
RewriteCond %{REQUEST_FILENAME} !\.
        RewriteCond %{REQUEST_FILENAME} !^/js/
    RewriteRule ^/([a-z]{2})/([^/]+)/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?$ /info/direct_to.php?f2=$2&f3=$3&f4=$4&f5=$5&f6=$6&f7=$7&lang=$1 [NC,L,QSA]

# Rewrite only PHP files
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteRule ^/([a-z]{2})/(.*)$ /$2 [NC,L,QSA] # If this is put in .htaccess, it works withtout the leading slash (/) (!!!!!!!) Here is what it will look like: #RewriteRule ^([a-z]{2}/)(.*)$ $2?lang=fe [NC,L]


# Rewrite all but PHP,CSS,JS files
RewriteCond %{REQUEST_FILENAME} !.*\.php$
        RewriteCond %{REQUEST_FILENAME} !^/js/
        RewriteCond %{REQUEST_FILENAME} !^/css/
# We get rid of the language attribute
RewriteRule ^/([a-z]{2})/(.*)$ /$2 [NC,L,QSA]


# Rewrite only PHP files
RewriteCond %{REQUEST_FILENAME} \.php$
# Old URLs that did not have a language directory will now be redirected to the URL with directory "en" (English)
RewriteRule ^/(.*)$ /en/$1 [R=301] # QSA est fait automatiquement ici ???

php_value include_path /mnt/hgfs/HM/inc
#php_value include_path ".:/var/www/TestAPE2/inc"

ErrorDocument 404 /en/warning/page-does-not-exist/

</VirtualHost>

Nicolas

unread,
May 9, 2016, 11:10:30 AM5/9/16
to APE Project
I am still not sure how to amend the Apache setup but in the meantime, I realized that Haproxy was not actually started.

So I try to start Haproxy. But now I run into at least 2 issues.

1) on config option fails with the text:
unknown option http-server-close
That does not make sense. I can't find anything on the web related to this.

2) If I comment the option, I get the following error which does not look good either :(
 Starting frontend public: cannot bind socket

Help appreciated :)


Louis Charette

unread,
May 9, 2016, 11:17:04 AM5/9/16
to ape-p...@googlegroups.com
Hi,

You can have a look at this. It could be outdated if there's a newer version of HaProxy around, but it's still a good place to start: https://github.com/APE-Project/APE_Server/wiki/APE-Server-on-port-80-with-HAProxy

What you want is to have Apache listening on port 8080 (or whatever you want) for your standard website need and APE on 6969. Then you tell HaProxy that if the connexion is a websocket type or comes from the specified subdomain, it should redirect it to APE and everything else to Apache. So then you don't need an apache domain or proxy seting for Ape anymore. The subdomain is handled by HaProxy (who is on port 80).

For the error part, HaProxy cannot bind the port (80) because Apache is probably still using it.

   - Louis

--
--
You received this message because you are subscribed to the Google
Groups "APE Project" group.
To post to this group, send email to ape-p...@googlegroups.com
To unsubscribe from this group, send email to
ape-project...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/ape-project?hl=en
---
APE Project (Ajax Push Engine)
Official website : http://www.ape-project.org/
Git Hub : http://github.com/APE-Project/

---
You received this message because you are subscribed to the Google Groups "APE Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ape-project...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Louis

Nicolas

unread,
May 9, 2016, 11:28:53 AM5/9/16
to APE Project
Merci !

It is so precious to have you around.

I am indeed following the tutorial you mention. And as it looks easy I am all the more surprized that there are so many issues going on.

So regarding my Apache config, I understand I can get rid of the second virtual host, is that right? I don't understand why you mention "APE on 6969 when you are talking about Apache.
FYI, as I said in a previous post. I use proxy to redirect 80 to 6969. 

As far as the first errror is concerned, I suspect that I installed an outdated version  of Haproxy that does not support http-server-close

As far as the second error is concerned, I am a bit puzzled again. If I remove the 2nd virtual host in my Apache config, then the first one kicks in and so I suppose HaProxy will always think that Apache is using it?? I probably look completely lost to you here. :)

Nicolas

unread,
May 9, 2016, 11:42:07 AM5/9/16
to APE Project
I stopped Apache. Haproxy could start. Now Apache does not want to start:
)Address already in use: make_sock: could not bind to address [::]:80

So I undersstand that Haproxy takes all incoming things from port 80???

Isn't there a way for Haproxy to deal with things from ape.local-ape-project.org and Apache to deal the rest of the things going to 80?

That's what I have in my prod configuration (with the virtual hosts). Does it have to change??? I am sure not.

Nicolas.



On Monday, May 9, 2016 at 5:17:04 PM UTC+2, Louis Charette wrote:

Louis Charette

unread,
May 9, 2016, 12:05:45 PM5/9/16
to ape-p...@googlegroups.com
The thing is ou can't have two thing running on the same port. It's like telephone extensions in a office. Right now, when people call your office (server) at the office number (port 80), they are dispatched directly to Apache which transfert the call (proxy) to APE (at extention 6969) if Apache see that the call is for APE.

HaProxy act as the receptionist. When someone call the office (server) on the office number (80), the fisrt person they get to is the receptionist (HaProxy) who dispatch the call to APE (at extention 6969) if the call is for APE (Either a websocket connexion or a specific subdomain) OR Apache (now moved at extention 8080) if the call is not for APE.

It goes like this (see attached file if now displayed right under).
Images intégrées 1
So you basically need to relocate Apache on port 8080 (or anything you want). This needs to be done for all the virtualhosts and there is also a global config I think for it (google it).


(And the nice thing about HaProxy: if you want to scale your office, it really act as a receptionist. If Apache is not avaiable for some reason or is overloaded, HA can take care of loadbalacing between multiple (apache) server. You also could, technically, even have APE on a different host with this).

Nicolas

unread,
May 9, 2016, 12:19:21 PM5/9/16
to APE Project
OK. Louis this makes sense! Perfectly clear now.

I managed to install a newer version of HaProxy. I am not sure though that I am closer to a solution.

The config file is completely different to the one from the example. I fear that even if I manage to make this work, I migh run later on issues with keep-alive or things like that that will affect my users without me realizing for a long time...

global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
user haproxy
group haproxy
daemon

defaults
log global
mode http
option httplog
option dontlognull
        contimeout 5000
        clitimeout 50000
        srvtimeout 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http

Nicolas

unread,
May 9, 2016, 1:37:18 PM5/9/16
to APE Project
OK. We have a first victory. It seems to be working. 

Haproxy running.
Apache configured for 8080 port now.
APE correctly setup.
And the whole board gaming platform now works with Websockets on my dev machine (I needed to logout first though, it was mentionned somewhere there was the need to delete cookies, not sure how to do this once at the moment of the platform upgrade but I'll figure it out, maybe restarting APE which I did actually was enough).

Thanks very much for your help Louis. I'll now try to do the same on my staging server. I now see (I did not remember) that it has a different configuration than my dev machine (one separate IP for APE on the same port as Apache, so in theory all I need is to setup HaProxy and tell bind it to the APE IP on port 80, right?).

Thanks again!



Louis Charette

unread,
May 9, 2016, 1:39:33 PM5/9/16
to ape-p...@googlegroups.com
Yeah that should work. 

I'll update the wiki with my receptionist thing for posterity ^.^
--
--
You received this message because you are subscribed to the Google
Groups "APE Project" group.
To post to this group, send email to ape-p...@googlegroups.com
To unsubscribe from this group, send email to
ape-project...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/ape-project?hl=en
---
APE Project (Ajax Push Engine)
Official website : http://www.ape-project.org/
Git Hub : http://github.com/APE-Project/

---
You received this message because you are subscribed to the Google Groups "APE Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ape-project...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Envoyé de mon iPhone

Louis Charette

unread,
Sep 24, 2016, 1:56:07 PM9/24/16
to APE Project
@Nicolas : Did you get it to work on secure web socket (wss)? I'm switching to ssl for the main website and it complains about ws not being secure and can't get wss to work with HaProxy for now.

Nicolas

unread,
Sep 24, 2016, 5:29:00 PM9/24/16
to APE Project
Hi Louis,

I do not work with SSL at all.

As far as I can remember (unfortunately, my memory is not so good for those things), managed to make it work with HaProxy, but later realized that I did not actually need it on my production server (because the config is different from my local machine). 

And later I reverted back to the default method because websockets generated issues with what I was trying to do to solve connection issues. As far as I can remember Websockets was duplicating messages or something like that when I was trying to resend them which made it impractical. As Websockets had not improved the situation on the stability side, I decided it was better to revert and solve cheaply the issue I had with the new lines of code.

My answer will probably not answer your question unfortunately :(

Louis Charette

unread,
Sep 24, 2016, 5:35:22 PM9/24/16
to ape-p...@googlegroups.com
Hi Nicolas,

Thanks for the quick reply! I’m not even sure why I use websocket over long pooling… I barely use APE anymore, but that one chat system would be fun to keep. 
I’ll see what I can find. Might be an area where socketIO have the same problem.

Thanks and take care ;)

Louis Charette

unread,
Sep 24, 2016, 8:25:16 PM9/24/16
to APE Project
For future reference, I managed to get it working *somwhow* using Apache. It handle websocket, but not long pooling, so... I disabled longpooling... Pretty much everything (post IE9) support websocket now anyway.

In my apache ssl vhost:

ProxyPass /ape/ ws://localhost:6969/

ProxyPassReverse /ape/ ws://localhost:6969/


Note "ws" and not "wss" in apache file ;)


Connect using "domain.com/ape/" and "secure: true" in ApePubSub. 


   - Louis

Le samedi 24 septembre 2016 17:35:22 UTC-4, Louis Charette a écrit :
Hi Nicolas,

Thanks for the quick reply! I’m not even sure why I use websocket over long pooling… I barely use APE anymore, but that one chat system would be fun to keep. 
I’ll see what I can find. Might be an area where socketIO have the same problem.

Thanks and take care ;)
Le 24 sept. 2016 à 17:29, Nicolas <ioa.g...@gmail.com> a écrit :

Hi Louis,

I do not work with SSL at all.

As far as I can remember (unfortunately, my memory is not so good for those things), managed to make it work with HaProxy, but later realized that I did not actually need it on my production server (because the config is different from my local machine). 

And later I reverted back to the default method because websockets generated issues with what I was trying to do to solve connection issues. As far as I can remember Websockets was duplicating messages or something like that when I was trying to resend them which made it impractical. As Websockets had not improved the situation on the stability side, I decided it was better to revert and solve cheaply the issue I had with the new lines of code.

My answer will probably not answer your question unfortunately :(




On Saturday, September 24, 2016 at 7:56:07 PM UTC+2, Louis Charette wrote:
@Nicolas : Did you get it to work on secure web socket (wss)? I'm switching to ssl for the main website and it complains about ws not being secure and can't get wss to work with HaProxy for now.

--
--
You received this message because you are subscribed to the Google
Groups "APE Project" group.
To post to this group, send email to ape-p...@googlegroups.com
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/ape-project?hl=en
---
APE Project (Ajax Push Engine)
Official website : http://www.ape-project.org/
Git Hub : http://github.com/APE-Project/

---
You received this message because you are subscribed to the Google Groups "APE Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ape-project+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages