Skip to content

Instantly share code, notes, and snippets.

@miyagawa
Created September 19, 2011 20:20
Show Gist options
  • Save miyagawa/1227494 to your computer and use it in GitHub Desktop.
Save miyagawa/1227494 to your computer and use it in GitHub Desktop.
monkeypatch socket.io to work with nginx with buffering on (e.g. dotcloud)
monkeypatch = (io) ->
# Thanks sugyan++ for the Socket.IO 0.7 workaround: http://d.hatena.ne.jp/sugyan/20110813/1313206163
io.configure ->
io.set "transports", ["xhr-polling"]
io.set "polling duration", 10
path = require('path')
HTTPPolling = require(path.join(path.dirname(require.resolve('socket.io')), 'lib', 'transports', 'http-polling'))
XHRPolling = require(path.join(path.dirname(require.resolve('socket.io')), 'lib', 'transports', 'xhr-polling'))
XHRPolling.prototype.doWrite = (data) ->
HTTPPolling.prototype.doWrite.call(@)
origin = @req.headers.origin
headers =
'Content-Type': 'text/plain; charset=UTF-8'
'Content-Length': if data is undefined then 0 else Buffer.byteLength(data)
if origin
# https://developer.mozilla.org/En/HTTP_Access_Control
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Credentials'] = 'true' if @req.headers.cookie
@response.writeHead 200, headers
@response.write data
exports.monkeypatch = monkeypatch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment