[Node.js]33. Level 7: Persisting Questions
Let's go back to our live-moderation app and add some persistence, first to the questions people ask.
Use the lpush
command to add new questions to the list named questions
. Do this inside thequestion
listener.
var express = require('express');
var app = express.createServer();
var socket = require('socket.io');
var io = socket.listen(app); var redis = require('redis');
var redisClient = redis.createClient(); io.sockets.on('connection', function(client) {
client.on('answer', function(question, answer) {
client.broadcast.emit('answer', question, answer);
}); client.on('question', function(question) {
client.get('question_asked', function(asked) {
if(!asked) {
client.set('question_asked', true);
client.broadcast.emit('question', question); // add the question to the list here
redisClient.lpush('questions', question);
}
});
});
});
Now that we have questions stored in redis, let's emit them whenever a new client connects to the server through socket.io.
Use the lrange
command to retrieve an array of questions that represent the questions
list in redis. Inside of the lrange
callback, use forEach
to loop through each question and emit
it on the client. Remember, don't use broadcast.emit
because we only want to send the questions to the client that is connecting to the server.
var express = require('express');
var app = express.createServer();
var socket = require('socket.io');
var io = socket.listen(app); var redis = require('redis');
var redisClient = redis.createClient(); io.sockets.on('connection', function(client) {
client.on('answer', function(question, answer) {
client.broadcast.emit('answer', question, answer);
}); redisClient.lrange('questions', 0, -1, function(err, messages){
messages.forEach(function(message){
client.emit('question', message);
});
}); client.on('question', function(question) {
client.get('question_asked', function(asked) {
if(!asked) {
client.set('question_asked', true);
client.broadcast.emit('question', question); redisClient.lpush("questions", question);
}
});
});
});
Great work! One last thing though, since every time a new question comes in we store it in thequestions
list, we might run into a problem where there are just too many questions stored in that list.
Add a callback to the lpush
command, and inside that callback use the ltrim
command to make sure the questions
list always has at most 20 items.
最新文章
- 先说IEnumerable,我们每天用的foreach你真的懂它吗?
- Linux网络管理1---(Linux配置IP地址,ifconfig、配置文件)
- notepad++快捷键大全
- 五步搞定Android开发环境部署
- 下列哪项不属于jdk1.6垃圾收集器?
- Android系统自带样式(android:theme)详解-(转)
- 15个关于Chrome的开发必备小技巧
- Hololens开发笔记之Gesture手势识别(单击,双击)
- db2日期和时间常用汇总
- PHP四种传参方式
- cocos2d-x3.0 SpriteFrameCache
- 浅析指针(pointer)与引用(reference)
- nyoj_323:Drainage Ditches(网络流入门)
- UITableView 的使用小点
- Java 面向对象的基本特征
- git代理配置
- Pyjwt ,python jwt ,jwt
- ②泡茶看<;数据结构>;,喜欢看源码-栈ADT
- HDU3306 Another kind of Fibonacci 矩阵
- LEO原创-FMX之你不知道的ARC
热门文章
- [BZOJ5305][HAOI2018]苹果树(DP)
- Azure ServiceBus的消息中带有@strin3http//schemas.microsoft.com/2003/10/Serialization/�
- 在Hexo中渲染MathJax数学公式
- 【对比分析五】CSS阻塞和JS阻塞
- Codeforces Round #350 (Div. 2) D1. Magic Powder - 1 二分
- Codeforces Round #287 (Div. 2) C. Guess Your Way Out! 水题
- Python学习笔记(一):Python基础学习
- 华为S5300系列交换机V100R006SPH019升级补丁
- Iterative (non-recursive) Quick Sort
- hybrid App h5二级页面返回的时候保持与一级页面浏览的位置一致