Created
May 20, 2015 18:48
-
-
Save having-fun-coding/bd43a0f1004d13260a69 to your computer and use it in GitHub Desktop.
Limiting Questions Stored | Real Time Web with Node.js
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
| 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