Skip to content

Instantly share code, notes, and snippets.

@Mumakil
Created June 16, 2015 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mumakil/898ced59c287e10b05e9 to your computer and use it in GitHub Desktop.
Save Mumakil/898ced59c287e10b05e9 to your computer and use it in GitHub Desktop.
Streaming api cleanup example
class Stream extends process.EventEmitter
constructor: (@request, @response, @channels, @user) ->
# other initializing code
get: ->
# start streaming messages in response to a get request
# ...
# Normal cleanup after stream is closed
@response.on 'finish', @cleanup
@response.on 'close', @cleanup
# Check that we didn't miss a close event
@aliveCheck = setInterval(@ensureRequestIsAlive, Stream.ALIVE_CHECK_INTERVAL)
ensureRequestIsAlive: =>
if @request.connection.destroyed
stats.increment('disconnect-dead-socket')
@cleanup()
cleanup: ->
# do required cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment