Skip to content

Instantly share code, notes, and snippets.

@cantremember
Last active April 30, 2017 22:13
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 cantremember/4a2995259fe8b438426ae164fca9b1d8 to your computer and use it in GitHub Desktop.
Save cantremember/4a2995259fe8b438426ae164fca9b1d8 to your computer and use it in GitHub Desktop.
Socket.io Server messaging using Binary Mode
/**
* Redis => Server => Socket.io
*/
function onRedisMessage(channel, message) {
var json;
try {
json = JSON.parse(message);
}
catch (err) {
// ...
}
// ... some filtering
this.messages.push(message);
this.jsons.push(json);
}
function getPayload() {
// super-cheap JSON Array construction
return new Buffer([ '[', this.messages.join(','), ']' ].join(''));
}
function emitInterval(io) {
var payload = this.getPayload();
var redis_channel_id = this.redis_channel_id;
this.sockets.forEach(function(socket) {
io.to(socket).emit(redis_channel_id, payload);
});
this.clearPayload();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment