Skip to content

Instantly share code, notes, and snippets.

@Roach
Created July 17, 2014 00:04
Show Gist options
  • Save Roach/589100c182be2628eadd to your computer and use it in GitHub Desktop.
Save Roach/589100c182be2628eadd to your computer and use it in GitHub Desktop.
user-stream.js
response.on('data', function(chunk) {
data += chunk.toString('utf8');
//is heartbeat?
if (data == '\r\n') {
stream.emit('heartbeat');
return;
}
var index, json;
while((index = data.indexOf('\r\n')) > -1) {
json = data.slice(0, index);
data = data.slice(index + 2);
if(json.length > 0) {
try {
stream.emit('data', JSON.parse(json));
} catch(e) {
stream.emit('garbage', data);
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment