Skip to content

Instantly share code, notes, and snippets.

@natestarner
Created March 31, 2012 01:44
Show Gist options
  • Save natestarner/2258561 to your computer and use it in GitHub Desktop.
Save natestarner/2258561 to your computer and use it in GitHub Desktop.
Socket.io and nginx workaround
<!-- include that socket client that socket.io serves out -->
<script src="//yourdomain.com:8080/socket.io/socket.io.js"></script>
<script type="text/javascript">
//connect your client to the socket server. Make sure that you specify
//the port here. It needs to be the same as what nodejs is listening on
var chat = io.connect("yourdomain.com:8080");
//add all your chat.on custom events next
</script>
/* most reverse proxies struggle working with websockets
* Many fixes require additional modules or tweaks to
* the proxy (nginx, HAproxy, etc)
* this is a pure javascript solution to get it working
*
* The ideal workflow is this:
*
* nginx listens on port 80
* nodejs listens on port 8080
* socket.io listens on port 8080 through nodejs app
* nginx proxies any requests on port 80 over to nodejs on 8080
* socket.io client connects directly to node/socket server on port 8080
* and communicates directly without involvement of a reverse proxy
*/
//create the node server, have socket.io listen on it
var app = express.createServer()
, io = socketio.listen(app);
//have node listen on a port other than 80
app.listen(8080);
//set up socket,io to do whatever you want
io.configure(function(){
io.set("authorization", function(handshakeData, accept){
//authorization code here
});
//accept connection from anywhere, this is default
io.set("origins","*:*");
});
//socket connection
io.on('connection', function (socket) {
//add all of your socket.on custom events here
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment