Using Websocket on Apache Server

Setting up a websocket on Apache?

I can't answer all questions, but I will do my best.

As you already know, WS is only a persistent full-duplex TCP connection with framed messages where the initial handshaking is HTTP-like. You need some server that's listening for incoming WS requests and that binds a handler to them.

Now it might be possible with Apache HTTP Server, and I've seen some examples, but there's no official support and it gets complicated. What would Apache do? Where would be your handler? There's a module that forwards incoming WS requests to an external shared library, but this is not necessary with the other great tools to work with WS.

WS server trends now include: Autobahn (Python) and Socket.IO (Node.js = JavaScript on the server). The latter also supports other hackish "persistent" connections like long polling and all the COMET stuff. There are other little known WS server frameworks like Ratchet (PHP, if you're only familiar with that).

In any case, you will need to listen on a port, and of course that port cannot be the same as the Apache HTTP Server already running on your machine (default = 80). You could use something like 8080, but even if this particular one is a popular choice, some firewalls might still block it since it's not supposed to be Web traffic. This is why many people choose 443, which is the HTTP Secure port that, for obvious reasons, firewalls do not block. If you're not using SSL, you can use 80 for HTTP and 443 for WS. The WS server doesn't need to be secure; we're just using the port.

Edit: According to Iharob Al Asimi, the previous paragraph is wrong. I have no time to investigate this, so please see his work for more details.

About the protocol, as Wikipedia shows, it looks like this:

Client sends:

GET /mychat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat
Sec-WebSocket-Version: 13
Origin: http://example.com

Server replies:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat

and keeps the connection alive. If you can implement this handshaking and the basic message framing (encapsulating each message with a small header describing it), then you can use any client-side language you want. JavaScript is only used in Web browsers because it's built-in.

As you can see, the default "request method" is an initial HTTP GET, although this is not really HTTP and looses everything in common with HTTP after this handshaking. I guess servers that do not support

Upgrade: websocket
Connection: Upgrade

will reply with an error or with a page content.

Using WebSocket on Apache server

One path is to use an independent installed web sockets server.

For PHP you can try:
http://code.google.com/p/phpwebsocket/ or http://github.com/Devristo/phpws/

There are some other projects which you can try as well.

Basically, you need to upload, unpack and start running the process.

On the frontend, you'll have javascript connecting to the server on the specific port.

Most websocket servers have a demo which echoes back whatever it hears, so this is a good place to write some test code. You may even find a rudimentary chat implementation.

The tricky part is to monitor the web socket server and to make sure it runs smoothly and continuously.

Try to test on as many browsers/devices as possible as this will decide on which websocket server implementation you choose. There are old and new protocols you have to watch out for.

WebSockets and Apache proxy: how to configure mod_proxy_wstunnel?

I finally managed to do it, thanks to this topic. TODO:

1) Have Apache 2.4 installed (doesn't work with 2.2), and do:

a2enmod proxy
a2enmod proxy_http
a2enmod proxy_wstunnel

2) Have nodejs running on port 3001

3) Do this in the Apache config

<VirtualHost *:80>
ServerName example.com

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/socket.io [NC]
RewriteCond %{QUERY_STRING} transport=websocket [NC]
RewriteRule /(.*) ws://localhost:3001/$1 [P,L]

ProxyPass / http://localhost:3001/
ProxyPassReverse / http://localhost:3001/
</VirtualHost>

Note: if you have more than one service on the same server that uses websockets, you might want to do this to separate them.

Apache websocket setup

You javascript code should utilize the address ws://socket.localhost/ws2, since this is what you have configured in the Apache proxy configuration.

Do you actually have a websocket server running at ws://localhost:8546? You don't mention that in your description.

Config file for websockets on an Apache server

There were errors in my config file. I ran apachectl -S to find what they were. Here is the working config file for anyone else struggling with this like I was:

<Directory "/Users/User/Sites/ProjectFolder">
AddLanguage en .en
AllowOverride All
Options Indexes MultiViews FollowSymLinks
Require all granted
</Directory>

ProxyRequests Off
RewriteEngine On
RewriteCond %{HTTP:Connection} Upgrade [NC]
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteRule /(.*) ws://127.0.0.1:8080/$1 [P,L]
<VirtualHost localhost:8080>
ProxyPass / ws://localhost:8080/
ProxyPassReverse / ws://localhost:8080/
</VirtualHost>

If you are using Ratchet make sure that your composer.json is set up correctly with a valid version. Then upgrade it by running php composer upgrade. Finally, make sure to reset the apache server with sudo apachectl restart.

Overhead of WebSockets on Apache Server

1) If once the WebSocket connection is made with server, then does it remain active like Long Polling with Server or what is the mechanism behind it? I mean how does WebSocket maintain its relation with server does it keep polling with server and hence there is always a connection between Client and Server?

Yes, it remains active and there is always a connection between Client and Server. However, the client needs to maintain the connection and on connection exceptions. In this case, it is your javascript code.

2) If your answer to above question is yes then what network/IO overhead would be on server side if I use websockets because there is a continuous connection between Client and Server. And imagine if there are hundreds of thousands of clients online at a time what load will it create on Server Network or IO?

WebSocket connections are handled on TCP/IP level and by definition, they are not resource consuming operations when there is no data going through the TCP tunnel. So rather than worrying about your CPU and memory conception, you need to worry about the limit on the number of connections. Consider using a load balancer for your socket connections and utilize multiple servers if you are expecting more than 10000 concurrent connections.

3) What is the Best approach to use WebSockets when using Apache + PHP on server? Any good article on this where I can study how to communicate with Apache Server using WebSockets? I found this question, but it doesn't answer well Using WebSocket on Apache server. In this question, it limits the experts to not to include any answer which has sysadmin tools, while I am asking for it if any required.

For such a use case of yours - getting the status of the server, I would suggest using a message broker rather than load this simple operation to Apache.

Please consider looking at mosquitto, hivemq or rabbitmq

All of them are supporting WebSockets and all of them have their pros and cons. Do the small proof of concepts over them and choose what is best for you.



Related Topics



Leave a reply



Submit