Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use port 80 for both HTTP and web socket traffic?

  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?

like image 865
Ender Avatar asked May 11 '11 19:05

Ender


3 Answers

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(){...}); }) 
like image 171
Detect Avatar answered Sep 22 '22 13:09

Detect


  • http://code.google.com/p/pywebsocket/
  • What popular webservers have support for HTML5 WebSocket?
  • https://serverfault.com/questions/201825/how-would-i-configure-a-websocket-server-to-run-alongside-a-webserver

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

like image 26
jgauffin Avatar answered Sep 26 '22 13:09

jgauffin


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!

like image 40
Eliasz Kubala Avatar answered Sep 24 '22 13:09

Eliasz Kubala