32
  1. I'm building a site that uses web sockets (technically Flash sockets) in order to provide real-time communication.
  2. I want to be able to support people behind corporate/academic firewalls that block everything except port 80
  3. I'd like to be able to run the site off of a single machine

Previously, I've been using Apache for HTTP serving combined with some python listening on a high-numbered socket for the websocket stuff, but that obviously won't work here.

I can always move the websocket stuff to a separate server, but I'd like to avoid paying for a second VPS (and have to talk to the database over the network instead of locally). Is there a good way to do this (nodejs, nginx, ..?), or is it not worth the headache?

Ender
  • 27,153
  • 7
  • 30
  • 34
  • possible duplicate of [What popular webservers have support for HTML5 WebSocket?](http://stackoverflow.com/questions/2924991/what-popular-webservers-have-support-for-html5-websocket) – jgauffin May 11 '11 at 19:39

6 Answers6

16

YES, by using node.js. Express or connect for the HTTP file serving and socket.io for the WebSocket stuff.

Example:

var express = require("express");
var app = express.createServer(); 

app.get('/', function(req, res){ 
    res.redirect("/index.html");
}); 

app.configure(function(){
  app.use(express.static(__dirname + '/public'));
});

app.listen(80); 

var io = require('socket.io'); 
var socket = io.listen(app); 
socket.on('connection', function(client){ 
  client.on('message', function(){...});
})
Detect
  • 2,049
  • 1
  • 12
  • 21
8

Another possibility is to use mod_proxy in apache to redirect the requests to a websocket server.

Community
  • 1
  • 1
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • Note that **pywebsocket** is not meant for production, rather only for testing (client implementation) - at which it is really good though btw. – Levite Jun 18 '14 at 06:57
5

Of course you can do this.

Firstly you have to check your Apache version. You should have 2.4+ version. I will show you command for my server on Ubuntu 14.4.

Secondly, turn on necessary apache modules:

a2enmod proxy
a2enmod proxy_http
a2enmod proxy_wstunnel

Open conf of your domain, in my case that was a path to file:

/etc/apache2/sites-available/myDomain.pl.conf

Next, append this code

<VirtualHost>

.
.
.

RewriteEngine on

RewriteCond %{QUERY_STRING} transport=polling
RewriteRule /(.*)$ http://localhost:3001/$1 [P]

ProxyRequests off
ProxyPass /socket.io ws://localhost:3001/socket.io
ProxyPassReverse /socket.io ws://localhost:3001/socket.io

ProxyPass        /socket.io http://localhost:3001/socket.io
ProxyPassReverse /socket.io http://localhost:3001/socket.io
</VirtualHost>

Finaly restart your apache

service apache2 restart

Have fun!

Eliasz Kubala
  • 3,836
  • 1
  • 23
  • 28
3

For secure websocket, all I needed to do was to add these three lines to my https virthualhost :

<VirtualHost *:443>

    [...https config...]

    SSLProxyEngine On
    ProxyPass "/web_socket"  "wss://localhost:7300/web_socket"
    ProxyPassReverse "/web_socket"  "wss://localhost:7300/web_socket"

</VirtualHost>

And enable the mod_proxy_wstunnel apache module (I'm running apache 2.4.10) :

a2enmod proxy_wstunnel
Teriblus
  • 789
  • 6
  • 17
0

GlassFish (and grizzly) support both HTTP and websockets traffic on the same port if a java server is an option.

cheeser
  • 16
-5

The short answer is no, because you can only have one process listening on one port. You could try using port 443, since that is not going to be blocked either, as long as you also do not use https.

nycynik
  • 7,371
  • 8
  • 62
  • 87
  • 1
    -1: First of all multiple process **can** listen on the same port in some cases (forking: for examples Apache spawns a process per request). Secondly serving WebSockets can be done within one process, i.e. a client opens TCP and depending on whether he sends (standard) HTTP or WebSocket it handles it differently. – freakish Oct 31 '13 at 23:06
  • he specified apache and python. I was only trying to answer that, if your using apache and python, I do not know of a way to have both of those processes listening on the same port at the same time. – nycynik Nov 05 '13 at 21:20
  • 11
    @freakish Technically multiple processes are **not** listening on the same port. One process is listening and when it receives something it handles that processes forks to handle the request and goes back to listening. In short, only one process is ever listening. Everything else you've said is completely correct. – CrazyCasta Apr 11 '14 at 05:12