Skip to content

Instantly share code, notes, and snippets.

@having-fun-coding
Created May 20, 2015 18:48
Show Gist options
  • Select an option

  • Save having-fun-coding/bd43a0f1004d13260a69 to your computer and use it in GitHub Desktop.

Select an option

Save having-fun-coding/bd43a0f1004d13260a69 to your computer and use it in GitHub Desktop.
Limiting Questions Stored | Real Time Web with Node.js
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var redis = require('redis');
var redisClient = redis.createClient();
io.sockets.on('connection', function(client) {
redisClient.lrange("questions", 0, -1, function(err, questions) {
questions.forEach(function(question) {
client.emit("question", question);
});
});
client.on('answer', function(question, answer) {
client.broadcast.emit('answer', question, answer);
});
client.on('question', function(question) {
if(!client.question_asked) {
client.question_asked = true;
client.broadcast.emit('question', question);
redisClient.lpush("questions", question, function(){
redisClient.ltrim("questions", 0, 19);
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment