Created
March 31, 2012 01:44
-
-
Save natestarner/2258561 to your computer and use it in GitHub Desktop.
Socket.io and nginx workaround
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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