Last active
April 30, 2017 22:13
-
-
Save cantremember/4a2995259fe8b438426ae164fca9b1d8 to your computer and use it in GitHub Desktop.
Socket.io Server messaging using Binary Mode
This file contains hidden or 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
/** | |
* 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